spring boot結合Redis實現工具類的方法示例

這篇文章主要介紹了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/vue-admin-java

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章