1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /*
- * Copyright [2022] [https://www.xiaonuo.vip]
- *
- * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
- *
- * 1.请不要删除和修改根目录下的LICENSE文件。
- * 2.请不要删除和修改Snowy源码头部的版权声明。
- * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
- * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
- * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
- * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
- */
- package vip.xiaonuo.common.cache;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.map.MapUtil;
- import cn.hutool.core.util.StrUtil;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import java.util.stream.Collectors;
- /**
- * 通用Redis缓存操作器
- *
- * @author xuyuxiang
- * @date 2022/6/21 16:00
- **/
- @Component
- public class CommonCacheOperator {
- /** 所有缓存Key的前缀 */
- private static final String CACHE_KEY_PREFIX = "Cache:";
- @Resource
- private RedisTemplate<String, Object> redisTemplate;
- public void put(String key, Object value) {
- redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).set(value);
- }
- public void put(String key, Object value, long timeoutSeconds) {
- redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).set(value, timeoutSeconds, TimeUnit.SECONDS);
- }
- public Object get(String key) {
- return redisTemplate.boundValueOps(CACHE_KEY_PREFIX + key).get();
- }
- public void remove(String... key) {
- ArrayList<String> keys = CollectionUtil.toList(key);
- List<String> withPrefixKeys = keys.stream().map(i -> CACHE_KEY_PREFIX + i).collect(Collectors.toList());
- redisTemplate.delete(withPrefixKeys);
- }
- public Collection<String> getAllKeys() {
- Set<String> keys = redisTemplate.keys(CACHE_KEY_PREFIX + "*");
- if (keys != null) {
- // 去掉缓存key的common prefix前缀
- return keys.stream().map(key -> StrUtil.removePrefix(key, CACHE_KEY_PREFIX)).collect(Collectors.toSet());
- } else {
- return CollectionUtil.newHashSet();
- }
- }
- public Collection<Object> getAllValues() {
- Set<String> keys = redisTemplate.keys(CACHE_KEY_PREFIX + "*");
- if (keys != null) {
- return redisTemplate.opsForValue().multiGet(keys);
- } else {
- return CollectionUtil.newArrayList();
- }
- }
- public Map<String, Object> getAllKeyValues() {
- Collection<String> allKeys = this.getAllKeys();
- HashMap<String, Object> results = MapUtil.newHashMap();
- for (String key : allKeys) {
- results.put(key, this.get(key));
- }
- return results;
- }
- }
|