RedisTemplate.opsForValue常用方法
- 简介
项目一般都会有缓存,常常使用redis来存取缓存(现在已不推荐使用session存储缓存),我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。 - RedisTemplate.opsForValue常用方法
set(K key, V value)
redisTemplate.opsForValue().set("stringValue","bbb");
get(Object key)
redisTemplate.opsForValue().get("stringValue");
append(K key, String value)
redisTemplate.opsForValue().append("stringValue","aaa");
get(K key, long start, long end)
redisTemplate.opsForValue().get("stringValue",0,3);
getAndSet(K key, V value)
String oldValue = redisTemplate.opsForValue().getAndSet("stringValue","ccc");
setBit(K key, long offset, boolean value)
redisTemplate.opsForValue().setBit("stringValue",1,false);
getBit(K key, long offset)
boolean bitBoolean = redisTemplate.opsForValue().getBit("stringValue",1);
size(K key)
Long stringValueLength = redisTemplate.opsForValue().size("stringValue");
increment(K key, long delta)
double stringValueLong = redisTemplate.opsForValue().increment("longValue",6);
setIfAbsent(K key, V value)
boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","fff");
set(K key, V value, long timeout, TimeUnit unit)
redisTemplate.opsForValue().set("timeOutValue","timeOut",5,TimeUnit.SECONDS);
set(K key, V value, long offset)
redisTemplate.opsForValue().set("absentValue","dd",1);
multiSet(Map<? extends K,? extends V> map)
Map valueMap = new HashMap(){{
put("valueMap1","map1");
put("valueMap2","map2");
put("valueMap3","map3");
}};
redisTemplate.opsForValue().multiSet(valueMap);