1. 程式人生 > 資料庫 >SpringBoot2.0 整合 Redis 最全解

SpringBoot2.0 整合 Redis 最全解

程式交流微信群

新增微信 : 372787553 備註 csdn

SpringBoot 整合 Redis

SpringBoot 2.3.1

新增依賴

這裡我們採用的是 lettuce 所以必須引入commons-pool2

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
 <dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-pool2</artifactId>
 </dependency>

配置檔案

spring: 
  redis:
    host: 127.0.0.1
    port: 6379
    database: 0
#    password:
    timeout: 6000
# 連結池
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        max-wait: 0

自定義 RedisConfiguration

public class RedisConfiguration {

    private final LettuceConnectionFactory lettuceConnectionFactory;

    public RedisConfiguration(LettuceConnectionFactory lettuceConnectionFactory) {
        this.lettuceConnectionFactory = lettuceConnectionFactory;
    }

    @Bean("redisTemplate")
    @ConditionalOnProperty(name = "spring.redis.host", matchIfMissing = true)
    public RedisTemplate<String, Object> getSingleRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(redisObjectSerializer);
        redisTemplate.setHashValueSerializer(redisObjectSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public HashOperations<String, String, String> hashOperations(StringRedisTemplate stringRedisTemplate) {
        return stringRedisTemplate.opsForHash();
    }

    @Bean
    public ListOperations<String,Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    @Bean
    public SetOperations<String,Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ValueOperations<String,Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * redis工具類
     */
    @Bean("redisUtil")
    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate,
                               StringRedisTemplate stringRedisTemplate,
                               HashOperations<String, String, String> hashOperations,
                               ListOperations<String,Object> listOperations,
                               ZSetOperations<String,Object>zSetOperations,
                               SetOperations<String,Object> setOperations,
                               ValueOperations<String,Object> valueOperations) {
        return new RedisUtil(redisTemplate, stringRedisTemplate,hashOperations,listOperations,
                zSetOperations,setOperations,valueOperations);
    }
}

自定義 RedisUtil

這裡只截取了一部分,更多的原始碼請移步source-code 內的redis 目錄


    /**
     * 指定快取失效時間
     *
     * @param key  鍵
     * @param time 時間(秒)
     * @return
     */
    boolean expire(K key, long time);

    /**
     * 根據key 獲取過期時間
     *
     * @param key 鍵 不能為null
     * @return 時間(秒) 返回0代表為永久有效
     */
    long getExpire(K key);

    /**
     * 判斷key是否存在
     *
     * @param key 鍵
     * @return true 存在 false不存在
     */
    boolean hasKey(K key);

    /**
     * 刪除快取
     *
     * @param key 可以傳一個值 或多個
     */
    void del(K... key);

    /**
     * 普通快取獲取
     *
     * @param key 鍵
     * @return 值
     */
    V get(K key);

    /**
     * 普通快取放入
     *
     * @param key   鍵
     * @param value 值
     * @return true成功 false失敗
     */
    boolean set(K key, V value);

    /**
     * 普通快取放入並設定時間
     *
     * @param key   鍵
     * @param value 值
     * @param time  時間(秒) time要大於0 如果time小於等於0 將設定無限期
     * @return true成功 false 失敗
     */
    boolean set(K key, V value, long time);

    /**
     * 遞增
     *
     * @param key   鍵
     * @param delta 要增加幾(大於0)
     * @return
     */
    long incr(K key, long delta);

    /**
     * 遞減
     *
     * @param key   鍵
     * @param delta 要減少幾(小於0)
     * @return
     */
    long decr(K key, long delta);

    /**
     * HashGet
     *
     * @param key  鍵 不能為null
     * @param item 項 不能為null
     * @return 值
     */
    V hget(K key, K item);

    /**
     * 獲取hashKey對應的所有鍵值
     *
     * @param key 鍵
     * @return 對應的多個鍵值
     */
    Map<K, V> hmget(K key);

    /**
     * HashSet
     *
     * @param key 鍵
     * @param map 對應多個鍵值
     * @return true 成功 false 失敗
     */
    boolean hmset(K key, Map<K, V> map);

    /**
     * HashSet 並設定時間
     *
     * @param key  鍵
     * @param map  對應多個鍵值
     * @param time 時間(秒)
     * @return true成功 false失敗
     */
    boolean hmset(K key, Map<K, V> map, long time);

    /**
     * 向一張hash表中放入資料,如果不存在將建立
     *
     * @param key   鍵
     * @param item  項
     * @param value 值
     * @return true 成功 false失敗
     */
    boolean hset(K key, K item, V value);

    /**
     * 向一張hash表中放入資料,如果不存在將建立
     *
     * @param key   鍵
     * @param item  項
     * @param value 值
     * @param time  時間(秒) 注意:如果已存在的hash表有時間,這裡將會替換原有的時間
     * @return true 成功 false失敗
     */
    boolean hset(K key, K item, V value, long time);

    /**
     * 刪除hash表中的值
     *
     * @param key  鍵 不能為null
     * @param item 項 可以使多個 不能為null
     */
    void hdel(K key, V... item);

    /**
     * 判斷hash表中是否有該項的值
     *
     * @param key  鍵 不能為null
     * @param item 項 不能為null
     * @return true 存在 false不存在
     */
    boolean hHasKey(K key, K item);

    /**
     * hash遞增 如果不存在,就會建立一個 並把新增後的值返回
     *
     * @param key  鍵
     * @param item 項
     * @param by   要增加幾(大於0)
     * @return
     */
    double hincr(K key, K item, double by);

    /**
     * hash遞減
     *
     * @param key  鍵
     * @param item 項
     * @param by   要減少記(小於0)
     * @return
     */
    double hdecr(K key, K item, double by);

    /**
     * 根據key獲取Set中的所有值
     *
     * @param key 鍵
     * @return
     */
    Set<V> sGet(K key);

    /**
     * 根據value從一個set中查詢,是否存在
     *
     * @param key   鍵
     * @param value 值
     * @return true 存在 false不存在
     */
    boolean sHasKey(K key, V value);

    /**
     * 將資料放入set快取
     *
     * @param key    鍵
     * @param values 值 可以是多個
     * @return 成功個數
     */
    long sSet(K key, V... values);

    /**
     * 將set資料放入快取
     *
     * @param key    鍵
     * @param time   時間(秒)
     * @param values 值 可以是多個
     * @return 成功個數
     */
    long sSetAndTime(K key, long time, V... values);

    /**
     * 獲取set快取的長度
     *
     * @param key 鍵
     * @return
     */
    long sGetSetSize(K key);

    /**
     * 移除值為value的
     *
     * @param key    鍵
     * @param values 值 可以是多個
     * @return 移除的個數
     */
    long setRemove(K key, V... values);

    /**
     * 獲取list快取的內容
     *
     * @param key   鍵
     * @param start 開始
     * @param end   結束 0 到 -1代表所有值
     * @return
     */
    List<V> lGet(K key, long start, long end);

    /**
     * 獲取list快取的長度
     *
     * @param key 鍵
     * @return
     */
    long lGetListSize(K key);

    /**
     * 通過索引 獲取list中的值
     *
     * @param key   鍵
     * @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數第二個元素,依次類推
     * @return
     */
    V lGetIndex(K key, long index);

    /**
     * 將list放入快取
     *
     * @param key   鍵
     * @param value 值
     * @return
     */
    boolean lSet(K key, V value);

    /**
     * 將list放入快取
     *
     * @param key   鍵
     * @param value 值
     * @param time  時間(秒)
     * @return
     */
    boolean lSet(K key, V value, long time);

    /**
     * 將list放入快取
     *
     * @param key   鍵
     * @param value 值
     * @return
     */
    boolean lSet(K key, List<V> value);

    /**
     * 將list放入快取
     *
     * @param key   鍵
     * @param value 值
     * @param time  時間(秒)
     * @return
     */
    boolean lSet(K key, List<V> value, long time);

    /**
     * 根據索引修改list中的某條資料
     *
     * @param key   鍵
     * @param index 索引
     * @param value 值
     * @return
     */
    boolean lUpdateIndex(K key, long index, V value);

    /**
     * 移除N個值為value
     *
     * @param key   鍵
     * @param count 移除多少個
     * @param value 值
     * @return 移除的個數
     */
    long lRemove(K key, long count, V value);

    /**
     * 模糊查詢獲取key值
     *
     * @param pattern
     * @return
     */
    Set keys(K pattern);

    /**
     * 使用Redis的訊息佇列
     *
     * @param channel
     * @param message 訊息內容
     */
    void convertAndSend(K channel, V message);

    /**
     * 將資料新增到Redis的list中(從右邊新增)
     *
     * @param listKey
     * @param expireEnum 有效期的列舉類
     * @param values     待新增的資料
     */
    void addToListRight(K listKey, Status.ExpireEnum expireEnum, V... values);

    /**
     * 根據起始結束序號遍歷Redis中的list
     *
     * @param listKey
     * @param start   起始序號
     * @param end     結束序號
     * @return
     */
    List<V> rangeList(K listKey, long start, long end);

    /**
     * 彈出右邊的值 --- 並且移除這個值
     *
     * @param listKey
     */
    V rifhtPop(K listKey);

測試

@RestController
@RequestMapping(value = "/redis/")
public class RedisApiWeb {
    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("test")
    public void redisTest(){
        redisUtil.set("redis","Hello World");
        System.out.println(redisUtil.get("redis"));
        Map<String,Object> map = new HashMap<>();
        map.put("hash01","hash02");
        redisUtil.hmset("myHash",map,100);
        System.out.println(redisUtil.hmget("myHash"));
    }


}