zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    extern “C”的作用详解
    const和typedef的常见用法详解
    虚函数、纯虚函数、虚函数与析构函数
    C++中四种类型转换方式
    面经中高频知识点归纳(一)
    leetcode Database4
    32位机和64位机下面各类型sizeof的大小
    Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
    字符串变量的定义与引用
    使用字符数组及相关函数,求3个国家名中字母顺序排在最前面的国家。
  • 原文地址:https://www.cnblogs.com/yangshixiong/p/12435765.html
Copyright © 2011-2022 走看看