zoukankan      html  css  js  c++  java
  • SpringBoot 配置Redis

    application.properties 文件内容

     1 #Redis数据库索引(默认为0)
     2 spring.redis.database=0
     3 #Redis服务器地址
     4 spring.redis.host=localhost
     5 #Redis服务器连接端口
     6 spring.redis.port=6379
     7 #Redis服务器连接密码(默认为空)
     8 spring.redis.password=
     9 #连接池最大连接数(使用负值表示没有限制)
    10 spring.redis.jedis.pool.max-active=200
    11 #连接池最大阻塞等待时间(使用负值表示没有限制)
    12 spring.redis.lettuce.pool.max-wait=-1
    13 #连接池中的最大空闲连接
    14 spring.redis.jedis.pool.max-idle=10
    15 #连接池中的最小空闲连接
    16 spring.redis.jedis.pool.min-idle=0
    17 #连接超时时间(毫秒)
    18 spring.redis.timeout=1000

    接下来我们自己创建一个RedisTemplate实例,不用SpringBoot自动为我们创建的那个

     1 import com.fasterxml.jackson.annotation.JsonAutoDetect;
     2 import com.fasterxml.jackson.annotation.PropertyAccessor;
     3 import com.fasterxml.jackson.databind.ObjectMapper;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.Configuration;
     6 import org.springframework.data.redis.connection.RedisConnectionFactory;
     7 import org.springframework.data.redis.core.RedisTemplate;
     8 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
     9 import org.springframework.data.redis.serializer.StringRedisSerializer;
    10 
    11 @Configuration
    12 public class RedisConfig {
    13     @Bean
    14     @SuppressWarnings("all")
    15     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    16         RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    17         template.setConnectionFactory(factory);
    18         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    19         ObjectMapper om = new ObjectMapper();
    20         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    21         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    22         jackson2JsonRedisSerializer.setObjectMapper(om);
    23         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    24         // key采用String的序列化方式
    25         template.setKeySerializer(stringRedisSerializer);
    26         // hash的key也采用String的序列化方式
    27         template.setHashKeySerializer(stringRedisSerializer);
    28         // value序列化方式采用jackson
    29         template.setValueSerializer(jackson2JsonRedisSerializer);
    30         // hash的value序列化方式采用jackson
    31         template.setHashValueSerializer(jackson2JsonRedisSerializer);
    32         template.afterPropertiesSet();
    33         return template;
    34     }
    35 }
      1 import org.springframework.beans.factory.annotation.Autowired;
      2 import org.springframework.data.redis.core.RedisTemplate;
      3 import org.springframework.stereotype.Component;
      4 import org.springframework.util.CollectionUtils;
      5 import java.util.List;
      6 import java.util.Map;
      7 import java.util.Set;
      8 import java.util.concurrent.TimeUnit;
      9 
     10 @Component
     11 public class RedisUtil {
     12     /**
     13      * Redis工具类
     14      * @author ZENG.XIAO.YAN
     15      * @date 年6月7日
     16      */
     17     @Autowired
     18     private RedisTemplate<String, Object> redisTemplate;
     19     // =============================common============================
     20     /**
     21      * 指定缓存失效时间
     22      *
     23      * @param key  键
     24      * @param time 时间(秒)
     25      * @return
     26      */
     27     public boolean expire(String key, long time) {
     28         try {
     29             if (time > 0) {
     30                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
     31             }
     32             return true;
     33         } catch (Exception e) {
     34             e.printStackTrace();
     35             return false;
     36         }
     37     }
     38 
     39     /**
     40      * 根据key 获取过期时间
     41      * @param key 键 不能为null
     42      * @return 时间(秒) 返回0代表为永久有效
     43      */
     44     public long getExpire(String key) {
     45         return redisTemplate.getExpire(key, TimeUnit.SECONDS);
     46     }
     47 
     48     /**
     49      * 判断key是否存在
     50      * @param key 键
     51      * @return true 存在 false不存在
     52      */
     53     public boolean hasKey(String key) {
     54         try {
     55             return redisTemplate.hasKey(key);
     56         } catch (Exception e) {
     57             e.printStackTrace();
     58             return false;
     59         }
     60     }
     61     /**
     62      * 删除缓存
     63      * @param key 可以传一个值 或多个
     64      */
     65     @SuppressWarnings("unchecked")
     66     public void del(String... key) {
     67         if ((key != null) && (key.length > 0)) {
     68             if (key.length == 1) {
     69                 redisTemplate.delete(key[0]);
     70             } else {
     71                 redisTemplate.delete(CollectionUtils.arrayToList(key));
     72             }
     73         }
     74     }
     75     // ============================String=============================
     76 
     77     /**
     78      * 普通缓存获取
     79      * @param key 键
     80      * @return 81      */
     82     public Object get(String key) {
     83         return (key == null) ? null : redisTemplate.opsForValue().get(key);
     84     }
     85     /**
     86      * 普通缓存放入
     87      * @param key   键
     88      * @param value 值
     89      * @return true成功 false失败
     90      */
     91     public boolean set(String key, Object value) {
     92         try {
     93             redisTemplate.opsForValue().set(key, value);
     94 
     95             return true;
     96         } catch (Exception e) {
     97             e.printStackTrace();
     98             return false;
     99         }
    100     }
    101     /**
    102      * 普通缓存放入并设置时间
    103      * @param key   键
    104      * @param value 值
    105      * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
    106      * @return true成功 false 失败
    107      */
    108     public boolean set(String key, Object value, long time) {
    109         try {
    110             if (time > 0) {
    111                 redisTemplate.opsForValue()
    112                         .set(key, value, time, TimeUnit.SECONDS);
    113             } else {
    114                 set(key, value);
    115             }
    116             return true;
    117         } catch (Exception e) {
    118             e.printStackTrace();
    119             return false;
    120         }
    121     }
    122     /**
    123      * 递增
    124      * @param key   键
    125      * @param delta 要增加几(大于0)
    126      * @return
    127      */
    128     public long incr(String key, long delta) {
    129         if (delta < 0) {
    130             throw new RuntimeException("递增因子必须大于0");
    131         }
    132         return redisTemplate.opsForValue().increment(key, delta);
    133     }
    134     /**
    135      * 递减
    136      * @param key   键
    137      * @param delta 要减少几(小于0)
    138      * @return
    139      */
    140     public long decr(String key, long delta) {
    141         if (delta < 0) {
    142             throw new RuntimeException("递减因子必须大于0");
    143         }
    144         return redisTemplate.opsForValue().increment(key, -delta);
    145     }
    146     // ================================Map=================================
    147     /**
    148      * HashGet
    149      * @param key  键 不能为null
    150      * @param item 项 不能为null
    151      * @return152      */
    153     public Object hget(String key, String item) {
    154         return redisTemplate.opsForHash().get(key, item);
    155     }
    156     /**
    157      * 获取hashKey对应的所有键值
    158      *
    159      * @param key 键
    160      * @return 对应的多个键值
    161      */
    162     public Map<Object, Object> hmget(String key) {
    163         return redisTemplate.opsForHash().entries(key);
    164     }
    165 
    166     /**
    167      * HashSet
    168      * @param key 键
    169      * @param map 对应多个键值
    170      * @return true 成功 false 失败
    171      */
    172     public boolean hmset(String key, Map<String, Object> map) {
    173         try {
    174             redisTemplate.opsForHash().putAll(key, map);
    175             return true;
    176         } catch (Exception e) {
    177             e.printStackTrace();
    178             return false;
    179         }
    180     }
    181     /**
    182      * HashSet 并设置时间
    183      * @param key  键
    184      * @param map  对应多个键值
    185      * @param time 时间(秒)
    186      * @return true成功 false失败
    187      */
    188     public boolean hmset(String key, Map<String, Object> map, long time) {
    189         try {
    190             redisTemplate.opsForHash().putAll(key, map);
    191 
    192             if (time > 0) {
    193                 expire(key, time);
    194             }
    195             return true;
    196         } catch (Exception e) {
    197             e.printStackTrace();
    198             return false;
    199         }
    200     }
    201     /**
    202      * 向一张hash表中放入数据,如果不存在将创建
    203      * @param key   键
    204      * @param item  项
    205      * @param value 值
    206      * @return true 成功 false失败
    207      */
    208     public boolean hset(String key, String item, Object value) {
    209         try {
    210             redisTemplate.opsForHash().put(key, item, value);
    211             return true;
    212         } catch (Exception e) {
    213             e.printStackTrace();
    214 
    215             return false;
    216         }
    217     }
    218     /**
    219      * 向一张hash表中放入数据,如果不存在将创建
    220      * @param key   键
    221      * @param item  项
    222      * @param value 值
    223      * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
    224      * @return true 成功 false失败
    225      */
    226     public boolean hset(String key, String item, Object value, long time) {
    227         try {
    228             redisTemplate.opsForHash().put(key, item, value);
    229             if (time > 0) {
    230                 expire(key, time);
    231             }
    232             return true;
    233         } catch (Exception e) {
    234             e.printStackTrace();
    235             return false;
    236         }
    237     }
    238     /**
    239      * 删除hash表中的值
    240      * @param key  键 不能为null
    241      * @param item 项 可以使多个 不能为null
    242      */
    243     public void hdel(String key, Object... item) {
    244         redisTemplate.opsForHash().delete(key, item);
    245     }
    246     /**
    247      * 判断hash表中是否有该项的值
    248      * @param key  键 不能为null
    249      * @param item 项 不能为null
    250      * @return true 存在 false不存在
    251      */
    252     public boolean hHasKey(String key, String item) {
    253         return redisTemplate.opsForHash().hasKey(key, item);
    254     }
    255     /**
    256      * hash递增 如果不存在,就会创建一个 并把新增后的值返回
    257      * @param key  键
    258      * @param item 项
    259      * @param by   要增加几(大于0)
    260      * @return
    261      */
    262     public double hincr(String key, String item, double by) {
    263         return redisTemplate.opsForHash().increment(key, item, by);
    264     }
    265     /**
    266      * hash递减
    267      * @param key  键
    268      * @param item 项
    269      * @param by   要减少记(小于0)
    270      * @return
    271      */
    272     public double hdecr(String key, String item, double by) {
    273         return redisTemplate.opsForHash().increment(key, item, -by);
    274     }
    275     // ============================set=============================
    276     /**
    277      * 根据key获取Set中的所有值
    278      * @param key 键
    279      * @return
    280      */
    281     public Set<Object> sGet(String key) {
    282         try {
    283             return redisTemplate.opsForSet().members(key);
    284         } catch (Exception e) {
    285             e.printStackTrace();
    286             return null;
    287         }
    288     }
    289     /**
    290      * 根据value从一个set中查询,是否存在
    291      * @param key   键
    292      * @param value 值
    293      * @return true 存在 false不存在
    294      */
    295     public boolean sHasKey(String key, Object value) {
    296         try {
    297             return redisTemplate.opsForSet().isMember(key, value);
    298         } catch (Exception e) {
    299             e.printStackTrace();
    300             return false;
    301         }
    302     }
    303     /**
    304      * 将数据放入set缓存
    305      * @param key    键
    306      * @param values 值 可以是多个
    307      * @return 成功个数
    308      */
    309     public long sSet(String key, Object... values) {
    310         try {
    311             return redisTemplate.opsForSet().add(key, values);
    312         } catch (Exception e) {
    313             e.printStackTrace();
    314             return 0;
    315         }
    316     }
    317     /**
    318      * 将set数据放入缓存
    319      * @param key    键
    320      * @param time   时间(秒)
    321      * @param values 值 可以是多个
    322      * @return 成功个数
    323      */
    324     public long sSetAndTime(String key, long time, Object... values) {
    325         try {
    326             Long count = redisTemplate.opsForSet().add(key, values);
    327 
    328             if (time > 0) {
    329                 expire(key, time);
    330             }
    331             return count;
    332         } catch (Exception e) {
    333             e.printStackTrace();
    334             return 0;
    335         }
    336     }
    337     /**
    338      * 获取set缓存的长度
    339      *
    340      * @param key 键
    341      * @return
    342      */
    343     public long sGetSetSize(String key) {
    344         try {
    345             return redisTemplate.opsForSet().size(key);
    346         } catch (Exception e) {
    347             e.printStackTrace();
    348             return 0;
    349         }
    350     }
    351     /**
    352      * 移除值为value的
    353      * @param key    键
    354      * @param values 值 可以是多个
    355      * @return 移除的个数
    356      */
    357     public long setRemove(String key, Object... values) {
    358         try {
    359             Long count = redisTemplate.opsForSet().remove(key, values);
    360 
    361             return count;
    362         } catch (Exception e) {
    363             e.printStackTrace();
    364             return 0;
    365         }
    366     }
    367     // ===============================list=================================
    368 
    369     /**
    370      * 获取list缓存的内容
    371      * @param key   键
    372      * @param start 开始
    373      * @param end   结束 0 到 -1代表所有值
    374      * @return
    375      */
    376     public List<Object> lGet(String key, long start, long end) {
    377         try {
    378             return redisTemplate.opsForList().range(key, start, end);
    379         } catch (Exception e) {
    380             e.printStackTrace();
    381             return null;
    382         }
    383     }
    384     /**
    385      * 获取list缓存的长度
    386      *
    387      * @param key 键
    388      * @return
    389      */
    390     public long lGetListSize(String key) {
    391         try {
    392             return redisTemplate.opsForList().size(key);
    393         } catch (Exception e) {
    394             e.printStackTrace();
    395             return 0;
    396         }
    397     }
    398     /**
    399      * 通过索引 获取list中的值
    400      * @param key   键
    401      * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
    402      * @return
    403      */
    404     public Object lGetIndex(String key, long index) {
    405         try {
    406             return redisTemplate.opsForList().index(key, index);
    407         } catch (Exception e) {
    408             e.printStackTrace();
    409             return null;
    410         }
    411     }
    412     /**
    413      * 将list放入缓存
    414      * @param key   键
    415      * @param value 值
    416      * @param time  时间(秒)
    417      * @return
    418      */
    419     public boolean lSet(String key, Object value) {
    420         try {
    421             redisTemplate.opsForList().rightPush(key, value);
    422             return true;
    423         } catch (Exception e) {
    424             e.printStackTrace();
    425             return false;
    426         }
    427     }
    428     /**
    429      * 将list放入缓存
    430      * @param key   键
    431      * @param value 值
    432      * @param time  时间(秒)
    433      * @return
    434      */
    435     public boolean lSet(String key, Object value, long time) {
    436         try {
    437             redisTemplate.opsForList().rightPush(key, value);
    438             if (time > 0) {
    439                 expire(key, time);
    440             }
    441             return true;
    442         } catch (Exception e) {
    443             e.printStackTrace();
    444             return false;
    445         }
    446     }
    447     /**
    448      * 将list放入缓存
    449      *
    450      * @param key   键
    451      * @param value 值
    452      * @param time  时间(秒)
    453      * @return
    454      */
    455     public boolean lSet(String key, List<Object> value) {
    456         try {
    457             redisTemplate.opsForList().rightPushAll(key, value);
    458             return true;
    459         } catch (Exception e) {
    460             e.printStackTrace();
    461             return false;
    462         }
    463     }
    464 
    465     /**
    466      * 将list放入缓存
    467      * @param key   键
    468      * @param value 值
    469      * @param time  时间(秒)
    470      * @return
    471      */
    472     public boolean lSet(String key, List<Object> value, long time) {
    473         try {
    474             redisTemplate.opsForList().rightPushAll(key, value);
    475             if (time > 0) {
    476                 expire(key, time);
    477             }
    478             return true;
    479         } catch (Exception e) {
    480             e.printStackTrace();
    481             return false;
    482         }
    483     }
    484     /**
    485      * 根据索引修改list中的某条数据
    486      * @param key   键
    487      * @param index 索引
    488      * @param value 值
    489      * @return
    490      */
    491     public boolean lUpdateIndex(String key, long index, Object value) {
    492         try {
    493             redisTemplate.opsForList().set(key, index, value);
    494             return true;
    495         } catch (Exception e) {
    496             e.printStackTrace();
    497 
    498             return false;
    499         }
    500     }
    501     /**
    502      * 移除N个值为value
    503      * @param key   键
    504      * @param count 移除多少个
    505      * @param value 值
    506      * @return 移除的个数
    507      */
    508     public long lRemove(String key, long count, Object value) {
    509         try {
    510             Long remove = redisTemplate.opsForList().remove(key, count, value);
    511 
    512             return remove;
    513         } catch (Exception e) {
    514             e.printStackTrace();
    515             return 0;
    516         }
    517     }
    518 }
  • 相关阅读:
    Vue 2.x windows环境下安装
    VSCODE官网下载缓慢或下载失败 解决办法
    angular cli 降级
    Win10 VS2019 设置 以管理员身份运行
    XSHELL 连接 阿里云ECS实例
    Chrome浏览器跨域设置
    DBeaver 执行 mysql 多条语句报错
    DBeaver 连接MySql 8.0 报错 Public Key Retrieval is not allowed
    DBeaver 连接MySql 8.0报错 Unable to load authentication plugin 'caching_sha2_password'
    Linux系统分区
  • 原文地址:https://www.cnblogs.com/flay/p/10691789.html
Copyright © 2011-2022 走看看