Redis发布与订阅以及日常存取源码分享

来源:互联网 发布:网络大电影 编辑:程序博客网 时间:2024/06/16 16:04

​​需要JAR包:

commons-pool2-2.4.2​ 、jedis-2.9.0


==============================发布与订阅==============================

​/**

 * 

 * 订阅者 (先启动订阅后,才能获取发布者发布的信息)

 * */

public class Redis_Subscribe {

static JedisPool pool;

static Jedis jedis;

public static void main(String[] args) {

// 初始化Redis连接池

pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");

// 从Redis连接池中获取一个连接

jedis = pool.getResource();

// Redis的密码,对应redis.windows.conf中的masterauth

jedis.auth("123456");

       JedisPubSub jedisPubSub=new JedisPubSub() {  

           @Override  

           public void onMessage(String channel, String message) {  

               super.onMessage(channel, message);  

               System.out.println(message);  

 

           }  

       };  

       jedis.subscribe(jedisPubSub,"JRedisChat"); 

}

}

/**

 * 

 * 发布者

 * */

public class Redis_Release {

static JedisPool pool;

static Jedis jedis;

public static void main(String[] args) {


// 初始化Redis连接池

pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");

// 从Redis连接池中获取一个连接

jedis = pool.getResource();

// Redis的密码,对应redis.windows.conf中的masterauth

jedis.auth("123456");

jedis.publish("JRedisChat","my name is chenLong");

jedis.publish("JRedisChat","my name is chenLong"); 

//        System.out.println(jedis.get("JRedisChat"));

}


}


============================日常存取=============================

​public class Zsgc_test {


public static void main(String[] args) {

start();

listTest();

}


static JedisPool pool;

static Jedis jedis;


public static void start() {


// 初始化Redis连接池

pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");

// 从Redis连接池中获取一个连接

jedis = pool.getResource();

// Redis的密码,对应redis.windows.conf中的masterauth

jedis.auth("123456");


}


/**

* 添加测试

*/


public static void putTest() {


jedis.set("user", "YoriChan");

System.out.println(jedis.get("user"));


// 输出结果:YoriChan


}


/**

* 覆盖测试

*/


public static void overWriteTest() {


jedis.set("user", "chanyulin");

System.out.println(jedis.get("user"));


// 输出结果:chanyulin


}


/**

* 追加测试

*/


public static void appendTest() {


jedis.append("user", "陈昱霖");

System.out.println(jedis.get("user"));


// 输出结果:chanyulin陈昱霖


}


/**

* 删除测试

*/


public static void deleteTest() {


jedis.del("user");

System.out.println(jedis.get("user"));


// 输出结果:null


}


/**

* List测试

*/


public static void listTest() {

List<TestEntity> artList = new ArrayList<TestEntity>();

for(int i=0;i<100000;i++){

TestEntity tn = new TestEntity();

tn.setId(1+i);

tn.setName("xxx"+i);

tn.setBz("哈喽,你好"+i);

artList.add(tn);

        }

        long start = System.currentTimeMillis();

        String key = "TestSetOpt";  

        jedis.set(key,JSON.toJSONString(artList)); 

        long stored = System.currentTimeMillis();

        System.out.println("redis写10万条数据耗时:" + (stored - start));

        

        System.out.println(jedis.get(key));

        jedis.del(key);

        System.out.println(jedis.get(key));

}

}​​​​