1.依赖与数据库设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-pool2</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.session</groupid>
<artifactid>spring-session-data-redis</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
spring.redis.database= 0
spring.redis.host=localhost
spring.redis.port= 6379
spring.redis.password= 123 #自己的密码
spring.redis.lettuce.pool.max-active= 8
spring.redis.lettuce.pool.max-wait=- 1
spring.redis.lettuce.pool.max-idle= 8
spring.redis.lettuce.pool.min-idle= 0
|
2.redis和session配置
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
|
@configuration
@enablecaching
public class redisconfig extends cachingconfigurersupport{
@bean
public keygenerator keygenerator() {
return new keygenerator() {
@override
public object generate(object target, method method, object... params) {
stringbuilder sb = new stringbuilder();
sb.append(target.getclass().getname());
sb.append(method.getname());
for (object obj : params) {
sb.append(obj.tostring());
}
return sb.tostring();
}
};
}
}
@configuration
@enableredishttpsession (maxinactiveintervalinseconds = 86400 * 30 )
public class sessionconfig {
}
|
3.实体与controller层
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
|
public class user implements serializable {
private static final long serialversionuid = 1l;
private long id;
private string username;
private string password;
private string email;
private string nickname;
private string regtime;
public user() {
super ();
}
public user(string email, string nickname, string password, string username, string regtime) {
super ();
this .email = email;
this .nickname = nickname;
this .password = password;
this .username = username;
this .regtime = regtime;
}
public long getid() {
return id;
}
public void setid( long id) {
this .id = id;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this .username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this .password = password;
}
public string getemail() {
return email;
}
public void setemail(string email) {
this .email = email;
}
public string getnickname() {
return nickname;
}
public void setnickname(string nickname) {
this .nickname = nickname;
}
public string getregtime() {
return regtime;
}
public void setregtime(string regtime) {
this .regtime = regtime;
}
@override
public string tostring() {
return "user{" +
"id=" + id +
", username='" + username + '\ '' +
", password='" + password + '\ '' +
", email='" + email + '\ '' +
", nickname='" + nickname + '\ '' +
", regtime='" + regtime + '\ '' +
'}' ;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@restcontroller
public class usercontroller {
@requestmapping ( "/getuser" )
@cacheable (value= "user-key" )
public user getuser() {
user user= new user( "aa@126.com" , "aa" , "aa123456" , "aa" , "123" );
system.out.println( "测试缓存" );
return user;
}
@requestmapping ( "/uid" )
string uid(httpsession session) {
uuid uid = (uuid) session.getattribute( "uid" );
if (uid == null ) {
uid = uuid.randomuuid();
}
session.setattribute( "uid" , uid);
return session.getid();
}
}
|
4.运行
1
2
3
4
5
6
7
|
@springbootapplication
public class redisapplication {
public static void main(string[] args) {
springapplication.run(redisapplication. class , args);
}
}
|
运行结果:
同时也可以用专门的图形界面工具查看:
到此这篇关于springboot使用redis作缓存使用入门的文章就介绍到这了,更多相关springboot redis缓存内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_41358574/article/details/119070010