spring boot 結合Redis 實現工具類

本身整理了 spring boot 結合 Redis 的工具類

引入依賴

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

加入配置

# Redis數據庫索引(默認爲0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=localhost
# Redis服務器鏈接端口
spring.redis.port=6379

實現代碼

這裏用到了 靜態類工具類中 如何使用 @Autowired
package com.lmxdawn.api.common.utils;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 緩存操做類
 */
@Component
public class CacheUtils {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 維護一個本類的靜態變量
    private static CacheUtils cacheUtils;

    @PostConstruct
    public void init() {
        cacheUtils = this;
        cacheUtils.redisTemplate = this.redisTemplate;
    }

    /**
     * 將參數中的字符串值設置爲鍵的值,不設置過時時間
     * @param key
     * @param value 必需要實現 Serializable 接口
     */
    public static void set(String key, String value) {
        cacheUtils.redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 將參數中的字符串值設置爲鍵的值,設置過時時間
     * @param key
     * @param value 必需要實現 Serializable 接口
     * @param timeout
     */
    public static void set(String key, String value, Long timeout) {
        cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 獲取與指定鍵相關的值
     * @param key
     * @return
     */
    public static Object get(String key) {
        return cacheUtils.redisTemplate.opsForValue().get(key);
    }

    /**
     * 設置某個鍵的過時時間
     * @param key 鍵值
     * @param ttl 過時秒數
     */
    public static boolean expire(String key, Long ttl) {
        return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
    }

    /**
     * 判斷某個鍵是否存在
     * @param key 鍵值
     */
    public static boolean hasKey(String key) {
        return cacheUtils.redisTemplate.hasKey(key);
    }

    /**
     * 向集合添加元素
     * @param key
     * @param value
     * @return 返回值爲設置成功的value數
     */
    public static Long sAdd(String key, String... value) {
        return cacheUtils.redisTemplate.opsForSet().add(key, value);
    }

    /**
     * 獲取集合中的某個元素
     * @param key
     * @return 返回值爲redis中鍵值爲key的value的Set集合
     */
    public static Set<String> sGetMembers(String key) {
        return cacheUtils.redisTemplate.opsForSet().members(key);
    }

    /**
     * 將給定分數的指定成員添加到鍵中存儲的排序集合中
     * @param key
     * @param value
     * @param score
     * @return
     */
    public static Boolean zAdd(String key, String value, double score) {
        return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
    }

    /**
     * 返回指定排序集中給定成員的分數
     * @param key
     * @param value
     * @return
     */
    public static Double zScore(String key, String value) {
        return cacheUtils.redisTemplate.opsForZSet().score(key, value);
    }

    /**
     * 刪除指定的鍵
     * @param key
     * @return
     */
    public static Boolean delete(String key) {
        return cacheUtils.redisTemplate.delete(key);
    }

    /**
     * 刪除多個鍵
     * @param keys
     * @return
     */
    public static Long delete(Collection<String> keys) {
        return cacheUtils.redisTemplate.delete(keys);
    }
}

相關地址

GitHub 地址: https://github.com/lmxdawn/vu...vue

相關文章
相關標籤/搜索