代码中的redisClientTemplate 封装的redisClient,与Jedis中的jedis类似,详见上一篇博客:http://blog.csdn.net/lsblsb/article/details/53770983
我跑的单元测试:可以在tearDown的时候,清空reids数据,做测试使用,如果redis提供服务,那么可以删除对应的测试key的value值。
键操作
/**
* 键操作
*/
public void testKey() throws Exception {
String key = "keyname";
System.out.println("判断某个键是否存在:" + redisClientTemplate.exists(key));
System.out.println("新增<'keyname','zzh'>的键值对:" + redisClientTemplate.set(key, "niepeng"));
System.out.println("判断某个键是否存在:" + redisClientTemplate.exists(key));
System.out.println("新增<'psw','pwsvalue'>的键值对:" + redisClientTemplate.set("psw", "pswvalue"));
System.out.print("系统中所有的键如下:");
System.out.println("删除键psw:" + redisClientTemplate.del("psw"));
System.out.println("判断键psw是否存在:" + redisClientTemplate.exists("psw"));
System.out.println("设置键keyname的过期时间为5s:" + redisClientTemplate.expire(key, 5));
TimeUnit.SECONDS.sleep(2);
System.out.println("查看键keyname的剩余生存时间:" + redisClientTemplate.ttl(key));
System.out.println("移除键keyname的生存时间:" + redisClientTemplate.persist(key));
System.out.println("查看键keyname的剩余生存时间:" + redisClientTemplate.ttl(key));
System.out.println("查看键keyname所存储的值的类型:" + redisClientTemplate.type(key));
}
输出结果:
判断某个键是否存在:true
新增<'keyname','zzh'>的键值对:OK
判断某个键是否存在:true
新增<'psw','pwsvalue'>的键值对:OK
系统中所有的键如下:删除键psw:1
判断键psw是否存在:false
设置键keyname的过期时间为5s:1
查看键keyname的剩余生存时间:3
移除键keyname的生存时间:1
查看键keyname的剩余生存时间:-1
查看键keyname所存储的值的类型:string
字符串操作
/**
* 字符串操作
*/
public void testString() throws Exception {
System.out.println("===========增加数据===========");
System.out.println(redisClientTemplate.set("key1", "value1"));
System.out.println(redisClientTemplate.set("key2", "value2"));
System.out.println(redisClientTemplate.set("key3", "value3"));
System.out.println("删除键key2:" + redisClientTemplate.del("key2"));
System.out.println("获取键key2:" + redisClientTemplate.get("key2"));
System.out.println("修改key1:" + redisClientTemplate.set("key1", "value1Changed"));
System.out.println("获取key1的值:" + redisClientTemplate.get("key1"));
System.out.println("key3的值:" + redisClientTemplate.get("key3"));
System.out.println("在key3后面加入值:" + redisClientTemplate.append("key3", "End"));
System.out.println("key3的值:" + redisClientTemplate.get("key3"));
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(redisClientTemplate.setnx("key1", "value1"));
System.out.println(redisClientTemplate.setnx("key2", "value2"));
System.out.println(redisClientTemplate.setnx("key2", "value2-new"));
System.out.println("key1的值:"+redisClientTemplate.get("key1"));
System.out.println("key2的值:"+redisClientTemplate.get("key2"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(redisClientTemplate.setex("key3", 2, "value3"));
System.out.println(redisClientTemplate.get("key3"));
TimeUnit.SECONDS.sleep(3);
System.out.println(redisClientTemplate.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
// GETSET is an atomic set this value and return the old value command.
System.out.println(redisClientTemplate.getSet("key2", "key2GetSet"));
System.out.println(redisClientTemplate.get("key2"));
System.out.println("获得key2的值的字串:" + redisClientTemplate.getrange("key2", 2, 4));
}
输出结果:
===========增加数据===========
OK
OK
OK
删除键key2:1
获取键key2:null
修改key1:OK
获取key1的值:value1Changed
key3的值:value3
在key3后面加入值:9
key3的值:value3End
===========新增键值对防止覆盖原先值==============
0
1
0
key1的值:value1Changed
key2的值:value2
===========新增键值对并设置有效时间=============
OK
value3
null
===========获取原值,更新为新值==&