Redis在springboot中的使用教程

时间:2022-09-13 12:12:03

依赖如下:

?
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 # 是否开启redis缓存 true开启  false关闭
  database: 0
  host: 47.104.208.124
  port: 6378
  password: lf.1228  # 密码(默认为空)
  timeout: 6000 # 连接超时时长(毫秒)
  pool:
    max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
    max-wait: -1   # 连接池最大阻塞等待时间(使用负值表示没有限制)
    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;
  /** 默认过期时长,单位:秒 */
  public final static long default_expire = 60 * 60 * 24;
  /** 不设置过期时长 */
  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转成json数据
   */
  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数据,转成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事务,序列化支持,操作redis方法

jedisconnectionfactory

所在包:org.springframework.data.redis.connection.jedis
作用:redis连接工厂类,创建redis连接池等

redisautoconfiguration

所在包:org.springframework.boot.autoconfigure.data.redis
作用:将redis配置文件相关信息注入工厂类

redisproperties

所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis连接基础类通过@configurationproperties注解将配置信息注入属性

总结

以上所述是小编给大家介绍的redis在springboot中的使用教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

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