spring boot redis RedisTemplate 와 Value Operations 의 string - value 동작 redisdoc 문서 의 방법 을 참조 하여 만 듭 니 다. http://redisdoc.com/ 문서 의 string 과 대응 유 니 버 설 인터페이스 와 구현 클래스 쓰기 전체 데모 연결 / / 캐 시 와 직렬 화 작업 설정
@Configuration
@EnableCaching
public class Cache extends CachingConfigurerSupport {
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
// @SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//
//rcm.setDefaultExpiration(60);//
return rcm;
}
@Autowired
private RedisTemplate redisTemplate;
@Bean
public ValueOperations valueOperations(){
Jackson2JsonRedisSerializer
//
public interface RedisString{
//
public void set(String key,Object o);
public void set(String key,Object o,String P,Long time);
//psetex SEObjectEX , key
public void pSetEX(String key,Object o,long time);
//
public Object get(String key);
//
public void append(String key,Object o);
// 1
public void decr(String key);
// k
public void decrBy(String key,long k);
public void decrBy(String key,double k);
// 1
public void incr(String key);
// n
public void incrBy(String key,long k);
public void incrBy(String key,double k);
/* k*/
public void increment(String key,long k);
// K
public Boolean getBit(String key,long k);
// key
public String getRange(String key,long l,long r);
//
public Object getSet(String key,Object o);
// key
public List mGet(List list);
// key
public void mSet(Map map);
// key
public Boolean mSetNX(Map map);
//
public Boolean setBit(String key,long k,Boolean b);
//
public void setEX(String key,Object o,long time);
// key
public void setNX(String key,Object o,long time);
// k
public void setRange(String key,Object o,Long k);
// key
public long strlen(String key);
}
//
@Service
public class RedisStringImpl implements RedisString {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ValueOperations opsForValue;
// key
@Override
public void set(String key, Object o) {
opsForValue.set(key,o);
}
@Override
public void set(String key, Object o, String P, Long time) {
if (P.equals("EX")){ //
opsForValue.set(key,o,time, TimeUnit.SECONDS);
}else if(P.equals("PX")){ //
opsForValue.set(key,o,time,TimeUnit.MILLISECONDS);
}else if(P.equals("NX")){ //
if (opsForValue.get(key)==null) opsForValue.set(key,o);
}else if (P.equals("XX")){ //
if (opsForValue.get(key)!=null) opsForValue.set(key,o);
}
}
// key
@Override
public Object get(String key) {
return opsForValue.get(key);
}
//
@Override
public void append(String key,Object o) {
opsForValue.append(key,o.toString());
}
// 1
@Override
public void decr(String key) {
opsForValue.increment(key,-1);
}
// k
@Override
public void decrBy(String key, long k) {
opsForValue.increment(key,-k);
}
@Override
public void decrBy(String key, double k) {
opsForValue.increment(key,k);
}
// 1
@Override
public void incr(String key) {
opsForValue.increment(key,1);
}
// n
@Override
public void incrBy(String key, long k) {
opsForValue.increment(key,k);
}
@Override
public void incrBy(String key, double k) {
opsForValue.increment(key,k);
}
// k
@Override
public void increment(String key, long k) {
opsForValue.increment(key,-1);
}
// K
@Override
public Boolean getBit(String key, long k) {
return opsForValue.getBit(key,k);
}
@Override
public String getRange(String key, long l, long r) {
return opsForValue.get(key,l,r);
}
//
@Override
public Object getSet(String key,Object o) {
return opsForValue.getAndSet(key,o);
}
// key
@Override
public List mGet(List list) {
return opsForValue.multiGet(list);
}
// key
@Override
public void mSet(Map map) {
opsForValue.multiSet(map);
}
// key
@Override
public Boolean mSetNX(Map map) {
return opsForValue.multiSetIfAbsent(map);
}
//psetex SEObjectEX , key
@Override
public void pSetEX(String key, Object o, long time) {
opsForValue.set(key,o,time, TimeUnit.MILLISECONDS);
}
//
@Override
public Boolean setBit(String key, long k, Boolean b) {
return opsForValue.setBit(key,k,b);
}
//
@Override
public void setEX(String key,Object o, long time) {
opsForValue.set(key,o,time,TimeUnit.SECONDS);
}
// key
@Override
public void setNX(String key,Object o, long time) {
if (opsForValue.get(key)==null) opsForValue.set(key,o);
}
// k
@Override
public void setRange(String key, Object o, Long k) {
opsForValue.set(key,o,k);
}
// key
@Override
public long strlen(String key) {
return opsForValue.size(key);
}
}