工具类系列-StringSimpleRedisUtil

来源:互联网 发布:sql 求平均值 编辑:程序博客网 时间:2024/05/01 17:55
public class StringSimpleRedisUtil {

    private static StringRedisTemplate stringRedisTemplate = SpringContext.getApplicationContext().getBean(
            "stringRedisTemplate", StringRedisTemplate.class);

    /**
     *
     * @param key 需实现方法入参
     * @param clazz  序列化/反序列化的类
     * @param redisKey 需要存取的redis KEY
     * @param timeout redis超时
     * @param function 数据源方法
     * @param <K> 入参
     * @param <V> 出参
     * @return
     */
    public static <K, V> V getCachedData(K key, Class<V> clazz, String redisKey, Integer timeout,
            Function<K, V> function) {
        boolean redisValidFlag = true;
        String storeValue;
        V storeObject = null;
        try {
            BoundValueOperations<String, String> op = stringRedisTemplate.boundValueOps(redisKey);
            storeValue = op.get();
            if (StringUtils.isNotBlank(storeValue)) {
                if(!clazz.equals(String.class)){
                    storeObject = JsonExceptionUtil.toBean(storeValue, clazz);
                }else {
                    storeObject = (V) storeValue;
                }
            } else {
                storeObject = function.apply(key);
                if(!clazz.equals(String.class)){
                    storeValue = JsonExceptionUtil.toJsonString(storeObject);
                }else {
                    storeValue = (String) storeObject;
                }
                if (StringUtils.isNotBlank(storeValue)) {
                    op.set(storeValue);
                    if (op.getExpire() < 0) {
                        op.expire(timeout, TimeUnit.MINUTES);
                    }
                }
            }
        } catch (Exception e) {
            redisValidFlag = false;
        }
        if (!redisValidFlag) {
            storeObject = function.apply(key);
        }
        return storeObject;
    }

    /**
     * 删除key
     * @param key
     */
    public static void deleteKey(String key){
        try {
            stringRedisTemplate.delete(key);
        } catch (Exception e) {

        }
    }

    /**
     * 更新key过期时间
     * @param key
     */
    public static Boolean updateKeyExpire(String key,Integer timeOut){
        try {
            return stringRedisTemplate.expire(key,timeOut, TimeUnit.MINUTES);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取key内容
     * @param key
     */
    public static String getKeyContent(String key) {
        try {
            BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
            return stringStringBoundValueOperations.get();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 设置key内容
     * @param key
     */
    public static void setKeyContent(String key,String content) {
        try {
            BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
            stringStringBoundValueOperations.set(content);
        } catch (Exception e) {

        }
    }

    /**
     * 批量删除key
     * @param pattern
     */
    public static void deleteKeyByPattern(String pattern){
        Set<String> keys = stringRedisTemplate.keys(pattern);
        if (!keys.isEmpty()) {
            stringRedisTemplate.delete(keys);
        }
    }

}

0 0
原创粉丝点击