All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.common.cache.CommonCacheOperator Maven / Gradle / Ivy

package com.nxyfan.framework.common.cache;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.nxyfan.framework.common.constant.CommonConstant;
import com.nxyfan.framework.common.exception.CommonException;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 通用Redis缓存操作器
 *
 * @author amour
 * @date 2022/6/21 16:00
 **/
@Component
public class CommonCacheOperator {

    @Resource
    private RedisTemplate redisTemplate;

    public void put(String key, Object value) {
        redisTemplate.boundValueOps(CommonConstant.REDIS_KEY_PREFIX_CACHE + key).set(value);
    }

    public void put(String key, Object value, long timeoutSeconds) {
        redisTemplate.boundValueOps(CommonConstant.REDIS_KEY_PREFIX_CACHE + key).set(value, timeoutSeconds, TimeUnit.SECONDS);
    }

    public Object get(String key) {
        return redisTemplate.boundValueOps(CommonConstant.REDIS_KEY_PREFIX_CACHE + key).get();
    }

    public Object get(String key, String prefix) {
        return redisTemplate.boundValueOps(prefix + key).get();
    }
    
    public void remove(String... key) {
        ArrayList keys = CollectionUtil.toList(key);
        List withPrefixKeys = keys.stream().map(i -> CommonConstant.REDIS_KEY_PREFIX_CACHE + i).collect(Collectors.toList());
        redisTemplate.delete(withPrefixKeys);
    }

    public Collection getAllKeys() {
        Set keys = redisTemplate.keys(CommonConstant.REDIS_KEY_PREFIX_CACHE + "*");
        if (keys != null) {
            // 去掉缓存key的common prefix前缀
            return keys.stream().map(key -> StrUtil.removePrefix(key, CommonConstant.REDIS_KEY_PREFIX_CACHE)).collect(Collectors.toSet());
        }else {
            return CollectionUtil.newHashSet();
        }
    }

    public Collection getAllKeys(String prefix) {
        Set keys = redisTemplate.keys(prefix + "*");
        if (keys != null) {
            // 去掉缓存key的common prefix前缀
            return keys.stream().map(key -> StrUtil.removePrefix(key, CommonConstant.REDIS_KEY_PREFIX_CACHE)).collect(Collectors.toSet());
        }else {
            return CollectionUtil.newHashSet();
        }
    }
    
    public Collection getAllValues() {
        Set keys = redisTemplate.keys(CommonConstant.REDIS_KEY_PREFIX_CACHE + "*");
        if (keys != null) {
            return redisTemplate.opsForValue().multiGet(keys);
        }else {
            return CollectionUtil.newArrayList();
        }
    }

    public Collection getAllValues(String prefix) {
        Set keys = redisTemplate.keys(prefix + "*");
        if (keys != null) {
            return redisTemplate.opsForValue().multiGet(keys);
        }else {
            return CollectionUtil.newArrayList();
        }
    }
    
    /**
    public Map getAllKeyValues() {
        Collection allKeys = this.getAllKeys();
        HashMap results = MapUtil.newHashMap();
        for(String key : allKeys) {
            results.put(key, this.get(key));
        }
        return results;
    }
    **/
    
    /**
     * 
     * Describe: 清空字典
     * Author: amour  
     * Create Time: 2022年11月8日 下午2:54:37
     */
    public void emptyDictionaryTrans() {
        Collection allKeys = this.getAllKeys(CommonConstant.REDIS_KEY_PREFIX_TRANS);
        redisTemplate.delete(allKeys);
    }
    
    /**
     * 
     * Describe: 清空配置
     * Author: Administrator  
     * Create Time: 2024年4月28日 上午8:57:31
     */
    public void emptySysConfig() {
        Collection allKeys = this.getAllKeys(CommonConstant.REDIS_KEY_PREFIX_CACHE);
        redisTemplate.delete(allKeys);
    }
    
    /**
     * 
     * Describe: 验证传入的字典编码在它的字典组里是否存在,不存在则抛出异常
     * Author: amour  
     * Create Time: 2022年11月8日 下午3:23:46   
     * @param dictGroupCode 字典分组编码
     * @param dictCode      字典编码
     * @param error         错误提示信息
     */
    public void validateDictCode(String dictGroupCode, String dictCode, String error) {
    	if(ObjectUtil.isNotEmpty(dictCode)) {
    		Object dictName = get(dictGroupCode + "_" + dictCode , CommonConstant.REDIS_KEY_PREFIX_TRANS);
    		if(ObjectUtil.isEmpty(dictName)) {
    			throw CommonException.bizException("不支持的" + error + ":{}", dictCode);
    		}
    	}
    }
    
    /**
     * 
     * Describe: 获取某个数据字典map
     * Author: Admin  
     * Create Time: 2023年7月11日 下午2:57:06   
     * @param groupCode 组编码
     * @return 数据字典map
     */
    public Map getDictMap(String groupCode) {
        Collection allKeys = this.getAllKeys(CommonConstant.REDIS_KEY_PREFIX_TRANS + groupCode);
        HashMap results = MapUtil.newHashMap();
        for(String key : allKeys) {
        	Object value = redisTemplate.boundValueOps(key).get();
    		key = StrUtil.removePrefix(key, CommonConstant.REDIS_KEY_PREFIX_TRANS + groupCode + "_");
            results.put(key, value);
        }
        return results;
    }
}