使用示例
@Service
@AllArgsConstructor
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {
private final RedisTemplate redisTemplate;
boolean saveOrUpdateRedis(QuestionVo data){
RedisUtils<String, QuestionVo> redisUtils = new RedisUtils<String, QuestionVo>(RedisConstants.QUESTION_KEY, redisTemplate);
return redisUtils.saveOrUpdateToRedis(data, QuestionVo::getId);
}
boolean saveOrUpdateRedis(List<QuestionVo> list){
RedisUtils<String, QuestionVo> redisUtils = new RedisUtils<String, QuestionVo>(RedisConstants.QUESTION_KEY, redisTemplate);
return redisUtils.saveOrUpdateCollectionToRedis(list, QuestionVo::getId);
}
QuestionVo fetchData(Object key){
RedisUtils<String, QuestionVo> redisUtils = new RedisUtils<String, QuestionVo>(RedisConstants.QUESTION_KEY, redisTemplate);
return redisUtils.fetchData(key);
}
QuestionVo deleteData(Object key){
RedisUtils<String, QuestionVo> redisUtils = new RedisUtils<String, QuestionVo>(RedisConstants.QUESTION_KEY, redisTemplate);
return redisUtils.delete(key);
}
boolean deleteBatch(Collection<QuestionVo> collection){
RedisUtils<String, QuestionVo> redisUtils = new RedisUtils<String, QuestionVo>(RedisConstants.QUESTION_KEY, redisTemplate);
return redisUtils.deleteBatch(collection, QuestionVo::getId);
}
}
工具类
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
public class RedisUtils<K, V> {
private Integer size = 1000;
private final K KEY;
private final RedisTemplate<K, V> redisTemplate;
public RedisUtils(K key, RedisTemplate<K, V> redisTemplate) {
this.KEY = key;
this.redisTemplate = redisTemplate;
}
public RedisUtils(K key, RedisTemplate<K, V> redisTemplate, Integer size) {
this.KEY = key;
this.size = size;
this.redisTemplate = redisTemplate;
}
public boolean saveOrUpdateToRedis(V data, Function<V, ?> function) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(KEY);
if (map.size() > size) {
map.clear();
}
map.put(function.apply(data), data);
redisTemplate.opsForHash().putAll(KEY, map);
return true;
}
public boolean saveOrUpdateCollectionToRedis(Collection<V> collection, Function<V, ?> function) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(KEY);
if(map.size() + collection.size() >= this.size){
map.clear();
}
redisTemplate.opsForHash().putAll(KEY, map);
collection.forEach(data -> {
map.put(function.apply(data), data);
});
redisTemplate.opsForHash().putAll(KEY, map);
return true;
}
public V fetchData(Object key) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(KEY);
Object data = map.get(key);
if (null != data) {
return (V) data;
}
return null;
}
public V delete(Object key) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(KEY);
Object data = map.get(key);
if (null != data) {
Object object = map.remove(key);
redisTemplate.opsForHash().putAll(KEY, map);
return (V) object;
}
return null;
}
public boolean deleteBatch(Collection<V> collection, Function<V, ?> function) {
AtomicBoolean result = new AtomicBoolean(false);
Map<Object, Object> map = redisTemplate.opsForHash().entries(KEY);
collection.forEach(data -> {
Object key = function.apply(data);
Object obj = map.get(key);
if (null != obj) {
result.set(true);
map.remove(key);
}
});
if (result.get()) {
redisTemplate.opsForHash().putAll(KEY, map);
}
return result.get();
}
public <D> boolean saveOrUpdateDataToRedis(D data, Function<D, ?> function) {
V v = redisTemplate.opsForList().leftPop(KEY);
List<D> list = null;
if (null == v) {
list = new LinkedList<>();
} else {
list = (List<D>) v;
}
boolean flag = true;
for (int i = 0; i < list.size(); i++) {
if (function.apply(data).equals(function.apply(list.get(i)))) {
flag = false;
list.set(i, data);
}
}
if (flag) {
list.add(data);
}
if (list.size() >= size) {
list.subList(size / 2, list.size());
this.clear();
}
redisTemplate.opsForList().leftPush(KEY, (V) list);
return true;
}
public <D> boolean saveOrUpdateDataCollectionToRedis(Collection<D> collection, Function<D, ?> function) {
V v = redisTemplate.opsForList().leftPop(KEY);
List<D> list = null;
if (null == v) {
list = new LinkedList<>();
} else {
list = (List<D>) v;
}
for (int i = 0; i < list.size(); i++) {
D data = findCollection(function.apply(list.get(i)), collection, function);
if (null == data) {
list.add(list.get(i));
} else {
list.set(i, data);
}
}
redisTemplate.opsForList().leftPush(KEY, (V) list);
return true;
}
public <D> D fetchData(Object key, Function<D, ?> function) {
V v = redisTemplate.opsForList().leftPop(KEY);
return this.findCollection(key, (List<D>) v, function);
}
public <D> D delete(Object key, Function<D, ?> function) {
V v = redisTemplate.opsForList().leftPop(KEY);
List<D> list = (List<D>) v;
if (null == v) {
return null;
} else {
list = (List<D>) v;
}
D result = null;
for (int i = 0; i < list.size(); i++) {
if (function.apply(list.get(i)).equals(key)) {
result = list.remove(i);
break;
}
}
if (null != result) {
redisTemplate.opsForList().leftPush(KEY, (V) list);
}
return result;
}
public <D> boolean deleteList(Collection<D> collection, Function<D, ?> function) {
V v = redisTemplate.opsForList().leftPop(KEY);
List<D> list = (List<D>) v;
if (null == v) {
return false;
} else {
list = (List<D>) v;
}
boolean result = false;
for (int i = 0; i < list.size(); ) {
if (null != findCollection(function.apply(list.get(i)), collection, function)) {
result = true;
list.remove(i);
continue;
}
i++;
}
if (result) {
redisTemplate.opsForList().leftPush(KEY, (V) list);
}
return result;
}
private <D> D findCollection(Object key, Collection<D> collection, Function<D, ?> function) {
if (null == key || null == collection || collection.isEmpty()) {
return null;
}
AtomicReference<D> result = new AtomicReference<>(null);
collection.forEach(data -> {
if (key.equals(function.apply(data))) {
result.set(data);
}
});
return result.get();
}
public boolean clear() {
redisTemplate.delete(KEY);
return true;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}