一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - spring boot結合Redis實現工具類的方法示例

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

2021-06-17 11:02蘭茗翔 Java教程

這篇文章主要介紹了spring boot結合Redis實現工具類的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

自己整理了 spring boot 結合 redis 的工具類

引入依賴

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

加入配置

?
1
2
3
4
5
6
# redis數據庫索引(默認為0
spring.redis.database=0
# redis服務器地址
spring.redis.host=localhost
# redis服務器連接端口
spring.redis.port=6379

實現代碼

這里用到了 靜態類工具類中 如何使用 @autowired

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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

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

原文鏈接:https://segmentfault.com/a/1190000017131552

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人在线影院 | 超兴奋朋友的中文字幕下 | 亚洲日韩男人网在线 | 精品四虎国产在免费观看 | 国产91一区二区在线播放不卡 | 国产福利视频一区二区微拍视频 | 成人在线av视频 | 免费在线观看小视频 | 亚洲精品一区二区久久久久 | 99久久中文字幕伊人 | 亚洲激情综合 | 青青热久免费精品视频精品 | 亚洲国产天堂综合一区 | 日本不卡视频免费的 | 成年女人免费 | 精品国产免费第一区二区 | 九九99在线视频 | 国产精品麻豆 | 摸逼小说 | 欧美一区二区三区四区在线观看 | 欧美图片另类小说综合 | 亚洲一成人毛片 | 亚洲男人的天堂网站 | 天堂伊人网 | 无限时间看片在线观看 | 四虎影在线永久免费观看 | 亚洲第一男人网站 | 乌克兰成人性色生活片 | 免费在线中文字幕 | 美女逼逼喷水 | 91麻豆精品国产91久久久 | 91看片淫黄大片在看 | 亚洲国产精品久久无套麻豆 | 短篇最污的乱淫伦小说全集 | 亚洲精品色综合久久 | 日韩一级片在线免费观看 | 色综合亚洲天天综合网站 | 肉色欧美久久久久久久蜜桃 | 亚洲电影第1页 | 日本黄色大片免费观看 | 久草在在线免视频在线观看 |