• spring boot 整合redis之后报错


    spring boot2 整合redis,使用下述依赖

    implementation 'org.springframework.boot:spring-boot-starter-data-redis'

    但是在项目启动的时候,就会报错,

    Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

    错误很明显,使用lettuce,我们需要倒入依赖

    implementation 'org.apache.commons:commons-pool2:2.8.0'

    redis保存Sting 的工具类

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;
    
    import java.util.concurrent.TimeUnit;
    
    @Component
    public final class RedisUtil {
    
        public static final long expireTime = 7200L;
    
        @Autowired
        public static RedisTemplate<String, String> redisTemplate;
    
        /**
         * 指定缓存失效时间
         *
         * @param key  键
         * @param time 时间(秒)
         */
        public static boolean expire(String key, long time) {
            try {
                if (time > 0) {
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据key 获取过期时间
         *
         * @param key 键 不能为null
         * @return 时间(秒) 返回0代表为永久有效
         */
        public static long getExpire(String key) {
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        }
    
        /**
         * 判断key是否存在
         *
         * @param key 键
         * @return true 存在 false不存在
         */
        public static boolean hasKey(String key) {
            try {
                return redisTemplate.hasKey(key);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 删除缓存
         *
         * @param key 可以传一个值 或多个
         */
        @SuppressWarnings("unchecked")
        public static void del(String... key) {
            if (key != null && key.length > 0) {
                if (key.length == 1) {
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete(CollectionUtils.arrayToList(key));
                }
            }
        }
    
        /**
         * 普通缓存获取
         *
         * @param key 键
         * @return*/
        public static String get(String key) {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        }
    
    
        /**
         * 普通缓存放入并设置时间
         *
         * @param key   键
         * @param value 值
         * @return true成功 false 失败
         */
        public static boolean set(String key, String value) {
            try {
                redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    }
  • 相关阅读:
    JavaScript递归方法 生成 json tree 树形结构数据
    分布式系统唯一ID生成方案汇总
    Twitter-Snowflake,64位自增ID算法详解
    手机端页面自适应解决方案—rem布局
    vue.js之路由
    kafka数据迁移实践
    mysql查询时强制区分大小写
    js加密参数传给后台,后台解密base64
    Target runtime com.genuitec.runtime.generic.jee60 is not defined
    怎么在点击浏览器前进、后退键时刷新页面而不读取缓存
  • 原文地址:https://www.cnblogs.com/yangshixiong/p/12435765.html
Copyright © 2020-2023  润新知