一、boundValueOps
简单的Key-Value
操作。
1、BoundValueOperations
BoundValueOperations
就是一个绑定key
的对象,我们可以通过这个对象来进行与key
相关的操作。
BoundValueOperations boundValueOps = redisTemplate.boundValueOps("lcy");
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("jyqc");
boundValueOps.set(1);
System.out.println(boundValueOps.get(