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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語(yǔ)言|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Redis在springboot中的使用教程

Redis在springboot中的使用教程

2021-05-09 12:49野蠻-生長(zhǎng) Java教程

這篇文章主要介紹了Redis在springboot中的使用教程,本文實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下

依賴如下:

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

配置文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
redis:
  open: true # 是否開(kāi)啟redis緩存 true開(kāi)啟  false關(guān)閉
  database: 0
  host: 47.104.208.124
  port: 6378
  password: lf.1228  # 密碼(默認(rèn)為空)
  timeout: 6000 # 連接超時(shí)時(shí)長(zhǎng)(毫秒)
  pool:
    max-active: 1000 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
    max-wait: -1   # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
    max-idle: 10   # 連接池中的最大空閑連接
    min-idle: 5    # 連接池中的最小空閑連接

redisconfig類:

?
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
@configuration
public class redisconfig {
  @autowired
  private redisconnectionfactory factory;
  @bean
  public redistemplate<string, object> redistemplate() {
    redistemplate<string, object> redistemplate = new redistemplate<>();
    redistemplate.setkeyserializer(new stringredisserializer());
    redistemplate.sethashkeyserializer(new stringredisserializer());
    redistemplate.sethashvalueserializer(new stringredisserializer());
    redistemplate.setvalueserializer(new stringredisserializer());
    redistemplate.setconnectionfactory(factory);
    return redistemplate;
  }
  @bean
  public hashoperations<string, string, object> hashoperations(redistemplate<string, object> redistemplate) {
    return redistemplate.opsforhash();
  }
  @bean
  public valueoperations<string, string> valueoperations(redistemplate<string, string> redistemplate) {
    return redistemplate.opsforvalue();
  }
  @bean
  public listoperations<string, object> listoperations(redistemplate<string, object> redistemplate) {
    return redistemplate.opsforlist();
  }
  @bean
  public setoperations<string, object> setoperations(redistemplate<string, object> redistemplate) {
    return redistemplate.opsforset();
  }
  @bean
  public zsetoperations<string, object> zsetoperations(redistemplate<string, object> redistemplate) {
    return redistemplate.opsforzset();
  }
}

redisutils如下:

?
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
@component
public class redisutils {
  @autowired
  private redistemplate<string, object> redistemplate;
  @autowired
  private valueoperations<string, string> valueoperations;
  @autowired
  private hashoperations<string, string, object> hashoperations;
  @autowired
  private listoperations<string, object> listoperations;
  @autowired
  private setoperations<string, object> setoperations;
  @autowired
  private zsetoperations<string, object> zsetoperations;
  /** 默認(rèn)過(guò)期時(shí)長(zhǎng),單位:秒 */
  public final static long default_expire = 60 * 60 * 24;
  /** 不設(shè)置過(guò)期時(shí)長(zhǎng) */
  public final static long not_expire = -1;
  private final static gson gson = new gson();
  public void set(string key, object value, long expire){
    valueoperations.set(key, tojson(value));
    if(expire != not_expire){
      redistemplate.expire(key, expire, timeunit.seconds);
    }
  }
  public void set(string key, object value){
    set(key, value, default_expire);
  }
  public <t> t get(string key, class<t> clazz, long expire) {
    string value = valueoperations.get(key);
    if(expire != not_expire){
      redistemplate.expire(key, expire, timeunit.seconds);
    }
    return value == null ? null : fromjson(value, clazz);
  }
  public <t> t get(string key, class<t> clazz) {
    return get(key, clazz, not_expire);
  }
  public string get(string key, long expire) {
    string value = valueoperations.get(key);
    if(expire != not_expire){
      redistemplate.expire(key, expire, timeunit.seconds);
    }
    return value;
  }
  public string get(string key) {
    return get(key, not_expire);
  }
  public void delete(string key) {
    redistemplate.delete(key);
  }
  /**
   * object轉(zhuǎn)成json數(shù)據(jù)
   */
  private string tojson(object object){
    if(object instanceof integer || object instanceof long || object instanceof float ||
        object instanceof double || object instanceof boolean || object instanceof string){
      return string.valueof(object);
    }
    return gson.tojson(object);
  }
  /**
   * json數(shù)據(jù),轉(zhuǎn)成object
   */
  private <t> t fromjson(string json, class<t> clazz){
    return gson.fromjson(json, clazz);
  }
}

springboot如何封裝redis:

redistemplate

所在包: org.springframework.data.redis.core

作用:redis模板,redis事務(wù),序列化支持,操作redis方法

jedisconnectionfactory

所在包:org.springframework.data.redis.connection.jedis
作用:redis連接工廠類,創(chuàng)建redis連接池等

redisautoconfiguration

所在包:org.springframework.boot.autoconfigure.data.redis
作用:將redis配置文件相關(guān)信息注入工廠類

redisproperties

所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis連接基礎(chǔ)類通過(guò)@configurationproperties注解將配置信息注入屬性

總結(jié)

以上所述是小編給大家介紹的redis在springboot中的使用教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://blog.csdn.net/qq_39533847/article/details/80680873

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品天天看特色大片不卡 | 毛片在线网址 | 国产福利一区二区三区四区 | 国产精品va在线观看无 | 精品久久久久久久久免费影院 | 91九色porny国产美女一区 | 欧美色阁 | 处女摘花 | 色人阁小说| 国产精品成人亚洲 | 日韩 欧美 国产 亚洲 中文 | 91寡妇天天综合久久影院 | brazzersvideo欧美最新 | 视频一区久久 | 国产九九热视频 | 亚洲精品成人A8198A片漫画 | 亚洲一区二区三区深夜天堂 | 日韩精品一区二区三区视频 | 逼逼爱| 美女张开腿黄网站免费精品动漫 | 欧美乱强 | 99久久精彩视频 | 韩国日本在线观看 | 国产精品久久久久无毒 | 成人免费高清视频 | 国产免费专区 | 色综合亚洲精品激情狠狠 | 国产亚洲精品美女 | 日产欧产va高清 | 国产99精品免费视频看6 | 免费观看www视频 | 天天爽天天干天天操 | 91精品国产高清久久久久久91 | 欧美一级艳片视频免费观看 | 无码AV熟妇素人内射V在线 | xxx95日本老师xxx学生 | 亚洲AV午夜福利精品香蕉麻豆 | 千金肉奴隶免费观看 | 亚洲国产成人久久精品hezyo | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 猫咪maomiav永久网址 |