package cn.autoform.web.controller.resumable.redis.impl;
|
|
import cn.autoform.util.tool.ObjectTool;
|
import cn.autoform.web.controller.resumable.redis.CacheTool;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.io.Serializable;
|
import java.util.Objects;
|
import java.util.Set;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
/**
|
* redis缓存实现
|
*/
|
@Slf4j
|
@Service("redisCache")
|
public class RedisCacheImpl implements CacheTool<Serializable> {
|
|
@Resource(name = "redisTemplate")
|
private RedisTemplate jedisCluster;
|
|
public boolean add(Serializable key, Serializable o) {
|
Objects.requireNonNull(key);
|
System.out.println(Objects.toString(key)+"---"+Objects.toString(o));
|
log.debug(String.format("添加%s_%s", Objects.toString(key), Objects.toString(o)));
|
System.out.println(String.format("添加%s_%s", Objects.toString(key), Objects.toString(o)));
|
jedisCluster.opsForValue().set(ObjectTool.objectToByte(filterKey(key)), ObjectTool.objectToByte(o));
|
return true;
|
}
|
|
@Override
|
public boolean addEx(Serializable key, Serializable value, int time) {
|
jedisCluster.opsForValue().set(ObjectTool.objectToByte(filterKey(key)), ObjectTool.objectToByte(value), time, TimeUnit.MILLISECONDS);
|
return true;
|
}
|
|
@Override
|
public long delete(Serializable key) {
|
jedisCluster.delete(ObjectTool.objectToByte(filterKey(key)));
|
return 1;
|
}
|
|
public Set<byte[]> keys(String pattern){
|
Set<Object> keys = jedisCluster.keys(ObjectTool.objectToByte(pattern));
|
return keys.stream()
|
.map(val->ObjectTool.objectToByte((Serializable) val))
|
.collect(Collectors.toSet());
|
}
|
|
@Override
|
public <T> T get(Serializable key, Class<T> clazz) {
|
//JedisCluster jedisCluster = (JedisCluster) ac.getBean("jedisCluster");
|
Objects.requireNonNull(key);
|
String strKey = filterKey(key);
|
byte[] data = (byte[])jedisCluster.opsForValue().get(ObjectTool.objectToByte(strKey));
|
if(data == null || data.length == 0){
|
return null;
|
}
|
return ObjectTool.byteToObject(data, clazz);
|
}
|
|
protected String filterKey(Object key) {
|
return key.toString().replace(".", "");
|
}
|
|
}
|