工具类系列-StringSimpleRedisUtil

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值