一、boundValueOps
简单的Key-Value操作。
1、BoundValueOperations
BoundValueOperations就是一个绑定key的对象,我们可以通过这个对象来进行与key相关的操作。
# 绑定一个key对象Fetter
BoundValueOperations boundValueOps = redisTemplate.boundValueOps("Fetter");
2、set(V value)
给绑定键重新设置值(如果没有值,则会添加这个值)。
boundValueOps.set("李明");
3、get()
获取绑定键的值。
String str = (String) boundValueOps.get();
System.out.println(str);
4、set(V value, long timeout, TimeUnit unit)
给绑定键设置新值并设置过期时间
boundValueOps.set("九月清晨",30, TimeUnit.SECONDS);
5、getAndSet(V value)
如果有这个值则获取没有则设置
String oldValue = (String) boundValueOps.getAndSet("西游");
String newValue = (String) boundValueOps.get();
6、increment(double delta)和increment(long delta)
它是Redis的自增长键,前提是绑定值的类型是double或long类型。increment是单线程的,所以它是安全的。
BoundValueOperations boundValueOps = redisTemplate.boundValueOps("xzz");
boundValueOps.set(1);
System.out.println(boundValueOps.get());
boundValueOps.increment(1);
System.out.println(boundValueOps.get());
boundValueOps.increment(1);
System.out.println(boundValueOps.get());
注意!
使用该方法,需要注意:需要使用StringRedisSerializer序列化器才能使用increment方法,否则会报错。
7、其他不常用的方法
其他不常用的方法可以参考:boundValueOps的使用
所有的键值对都可以通过delete删除
1、删除一个key
//删除一个Key
redisTemplate.delete("hashKey");
2、删除多个Key - 可以是集合、数组、多参数
// 删除多个Key - 可以是集合、数组、多参数
redisTemplate.delete(list);