CommonCacheOperator.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright [2022] [https://www.xiaonuo.vip]
  3. *
  4. * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
  5. *
  6. * 1.请不要删除和修改根目录下的LICENSE文件。
  7. * 2.请不要删除和修改Snowy源码头部的版权声明。
  8. * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
  9. * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
  10. * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  11. * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  12. */
  13. package vip.xiaonuo.common.cache;
  14. import cn.hutool.core.collection.CollectionUtil;
  15. import cn.hutool.core.map.MapUtil;
  16. import cn.hutool.core.util.StrUtil;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.stereotype.Component;
  19. import javax.annotation.Resource;
  20. import java.util.*;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.stream.Collectors;
  23. /**
  24. * 通用Redis缓存操作器
  25. *
  26. * @author xuyuxiang
  27. * @date 2022/6/21 16:00
  28. **/
  29. @Component
  30. public class CommonCacheOperator {
  31. /** 所有缓存Key的前缀 */
  32. private static final String CACHE_KEY_PREFIX = "Cache:";
  33. @Resource
  34. private RedisTemplate<String, Object> redisTemplate;
  35. public void put(String key, Object value) {
  36. redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).set(value);
  37. }
  38. public void put(String key, Object value, long timeoutSeconds) {
  39. redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).set(value, timeoutSeconds, TimeUnit.SECONDS);
  40. }
  41. public Object get(String key) {
  42. return redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).get();
  43. }
  44. public void remove(String... key) {
  45. ArrayList<String> keys = CollectionUtil.toList(key);
  46. List<String> withPrefixKeys = keys.stream().map(i -> CACHE_KEY_PREFIX + i).collect(Collectors.toList());
  47. redisTemplate.delete(withPrefixKeys);
  48. }
  49. public Collection<String> getAllKeys() {
  50. Set<String> keys = redisTemplate.keys(CACHE_KEY_PREFIX + "*");
  51. if (keys != null) {
  52. // 去掉缓存key的common prefix前缀
  53. return keys.stream().map(key -> StrUtil.removePrefix(key, CACHE_KEY_PREFIX)).collect(Collectors.toSet());
  54. } else {
  55. return CollectionUtil.newHashSet();
  56. }
  57. }
  58. public Collection<Object> getAllValues() {
  59. Set<String> keys = redisTemplate.keys(CACHE_KEY_PREFIX + "*");
  60. if (keys != null) {
  61. return redisTemplate.opsForValue().multiGet(keys);
  62. } else {
  63. return CollectionUtil.newArrayList();
  64. }
  65. }
  66. public Map<String, Object> getAllKeyValues() {
  67. Collection<String> allKeys = this.getAllKeys();
  68. HashMap<String, Object> results = MapUtil.newHashMap();
  69. for (String key : allKeys) {
  70. results.put(key, this.get(key));
  71. }
  72. return results;
  73. }
  74. }