Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7219f18

Browse files
committed
add redis examples
1 parent1c2f0e0 commit7219f18

File tree

10 files changed

+332
-0
lines changed

10 files changed

+332
-0
lines changed

‎3.x/spring-boot-redis/pom.xml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.neo</groupId>
7+
<artifactId>spring-boot-redis</artifactId>
8+
<version>3.0.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-redis</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>3.0.0</version>
18+
<relativePath/><!-- lookup parent from repository-->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>17</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-redis</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.apache.commons</groupId>
33+
<artifactId>commons-pool2</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-web</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.session</groupId>
41+
<artifactId>spring-session-data-redis</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.junit.vintage</groupId>
50+
<artifactId>junit-vintage-engine</artifactId>
51+
<scope>test</scope>
52+
<exclusions>
53+
<exclusion>
54+
<groupId>org.hamcrest</groupId>
55+
<artifactId>hamcrest-core</artifactId>
56+
</exclusion>
57+
</exclusions>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
71+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packagecom.neo;
2+
3+
importorg.springframework.boot.SpringApplication;
4+
importorg.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
publicclassRedisApplication {
8+
9+
publicstaticvoidmain(String[]args) {
10+
SpringApplication.run(RedisApplication.class,args);
11+
}
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.neo.config;
2+
3+
importjava.lang.reflect.Method;
4+
5+
importorg.springframework.cache.CacheManager;
6+
importorg.springframework.cache.annotation.CachingConfigurerSupport;
7+
importorg.springframework.cache.annotation.EnableCaching;
8+
importorg.springframework.cache.interceptor.KeyGenerator;
9+
importorg.springframework.context.annotation.Bean;
10+
importorg.springframework.context.annotation.Configuration;
11+
importorg.springframework.data.redis.cache.RedisCacheManager;
12+
importorg.springframework.data.redis.core.RedisTemplate;
13+
14+
15+
@Configuration
16+
@EnableCaching
17+
publicclassRedisConfigextendsCachingConfigurerSupport{
18+
19+
@Bean
20+
publicKeyGeneratorkeyGenerator() {
21+
returnnewKeyGenerator() {
22+
@Override
23+
publicObjectgenerate(Objecttarget,Methodmethod,Object...params) {
24+
StringBuildersb =newStringBuilder();
25+
sb.append(target.getClass().getName());
26+
sb.append(method.getName());
27+
for (Objectobj :params) {
28+
sb.append(obj.toString());
29+
}
30+
returnsb.toString();
31+
}
32+
};
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
packagecom.neo.config;
2+
3+
importorg.springframework.context.annotation.Configuration;
4+
importorg.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
5+
6+
@Configuration
7+
@EnableRedisHttpSession(maxInactiveIntervalInSeconds =86400*30)
8+
publicclassSessionConfig {
9+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
packagecom.neo.model;
2+
3+
importjava.io.Serializable;
4+
5+
6+
7+
publicclassUserimplementsSerializable {
8+
9+
privatestaticfinallongserialVersionUID =1L;
10+
privateLongid;
11+
privateStringuserName;
12+
privateStringpassword;
13+
privateStringemail;
14+
privateStringnickname;
15+
privateStringregTime;
16+
17+
publicUser() {
18+
super();
19+
}
20+
publicUser(Stringemail,Stringnickname,Stringpassword,StringuserName,StringregTime) {
21+
super();
22+
this.email =email;
23+
this.nickname =nickname;
24+
this.password =password;
25+
this.userName =userName;
26+
this.regTime =regTime;
27+
}
28+
29+
publicLonggetId() {
30+
returnid;
31+
}
32+
33+
publicvoidsetId(Longid) {
34+
this.id =id;
35+
}
36+
37+
publicStringgetUserName() {
38+
returnuserName;
39+
}
40+
41+
publicvoidsetUserName(StringuserName) {
42+
this.userName =userName;
43+
}
44+
45+
publicStringgetPassword() {
46+
returnpassword;
47+
}
48+
49+
publicvoidsetPassword(Stringpassword) {
50+
this.password =password;
51+
}
52+
53+
publicStringgetEmail() {
54+
returnemail;
55+
}
56+
57+
publicvoidsetEmail(Stringemail) {
58+
this.email =email;
59+
}
60+
61+
publicStringgetNickname() {
62+
returnnickname;
63+
}
64+
65+
publicvoidsetNickname(Stringnickname) {
66+
this.nickname =nickname;
67+
}
68+
69+
publicStringgetRegTime() {
70+
returnregTime;
71+
}
72+
73+
publicvoidsetRegTime(StringregTime) {
74+
this.regTime =regTime;
75+
}
76+
77+
@Override
78+
publicStringtoString() {
79+
return"User{" +
80+
"id=" +id +
81+
", userName='" +userName +'\'' +
82+
", password='" +password +'\'' +
83+
", email='" +email +'\'' +
84+
", nickname='" +nickname +'\'' +
85+
", regTime='" +regTime +'\'' +
86+
'}';
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
packagecom.neo.web;
2+
3+
importjakarta.servlet.http.HttpSession;
4+
importorg.springframework.cache.annotation.Cacheable;
5+
importorg.springframework.web.bind.annotation.RequestMapping;
6+
importorg.springframework.web.bind.annotation.RestController;
7+
8+
importcom.neo.model.User;
9+
10+
importjava.util.UUID;
11+
12+
@RestController
13+
publicclassUserController {
14+
15+
@RequestMapping("/getUser")
16+
@Cacheable(value="user-key")
17+
publicUsergetUser() {
18+
Useruser=newUser("aa@126.com","aa","aa123456","aa","123");
19+
System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
20+
returnuser;
21+
}
22+
23+
24+
@RequestMapping("/uid")
25+
Stringuid(HttpSessionsession) {
26+
UUIDuid = (UUID)session.getAttribute("uid");
27+
if (uid ==null) {
28+
uid =UUID.randomUUID();
29+
}
30+
session.setAttribute("uid",uid);
31+
returnsession.getId();
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# REDIS
2+
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09
3+
spring.redis.database=0
4+
# Redis\u670D\u52A1\u5668\u5730\u5740
5+
spring.redis.host=localhost
6+
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
7+
spring.redis.port=6379
8+
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
9+
spring.redis.password=
10+
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 8
11+
spring.redis.lettuce.pool.max-active=8
12+
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 -1
13+
spring.redis.lettuce.pool.max-wait=-1
14+
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 8
15+
spring.redis.lettuce.pool.max-idle=8
16+
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 0
17+
spring.redis.lettuce.pool.min-idle=0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagecom.neo;
2+
3+
importorg.junit.Test;
4+
importorg.junit.runner.RunWith;
5+
importorg.springframework.boot.test.context.SpringBootTest;
6+
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
importorg.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest
11+
publicclassRedisApplicationTests {
12+
13+
@Test
14+
publicvoidcontextLoads() {
15+
System.out.println("hello web");
16+
}
17+
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packagecom.neo;
2+
3+
importcom.neo.model.User;
4+
importorg.junit.Assert;
5+
importorg.junit.Test;
6+
importorg.junit.runner.RunWith;
7+
importorg.springframework.beans.factory.annotation.Autowired;
8+
importorg.springframework.boot.test.context.SpringBootTest;
9+
importorg.springframework.data.redis.core.RedisTemplate;
10+
importorg.springframework.data.redis.core.StringRedisTemplate;
11+
importorg.springframework.data.redis.core.ValueOperations;
12+
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
importorg.springframework.test.context.junit4.SpringRunner;
14+
15+
importjava.util.concurrent.TimeUnit;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest
19+
publicclassTestRedis {
20+
21+
@Autowired
22+
privateStringRedisTemplatestringRedisTemplate;
23+
24+
@Autowired
25+
privateRedisTemplateredisTemplate;
26+
27+
@Test
28+
publicvoidtest()throwsException {
29+
stringRedisTemplate.opsForValue().set("aaa","111");
30+
Assert.assertEquals("111",stringRedisTemplate.opsForValue().get("aaa"));
31+
}
32+
33+
@Test
34+
publicvoidtestObj()throwsException {
35+
Useruser=newUser("aa@126.com","aa","aa123456","aa","123");
36+
ValueOperations<String,User>operations=redisTemplate.opsForValue();
37+
operations.set("com.neox",user);
38+
operations.set("com.neo.f",user,1,TimeUnit.SECONDS);
39+
Thread.sleep(1000);
40+
//redisTemplate.delete("com.neo.f");
41+
booleanexists=redisTemplate.hasKey("com.neo.f");
42+
if(exists){
43+
System.out.println("exists is true");
44+
}else{
45+
System.out.println("exists is false");
46+
}
47+
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
48+
}
49+
}

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开
2424
-[spring-boot-hello](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-hello):Spring Boot 3.0 Hello World 示例
2525
-[spring-boot-helloworld](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-helloWorld):Spring Boot 3.0 Hello World Test 单元测试示例
2626
-[spring-boot-web](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-web):Spring Boot 3.0 web 示例
27+
-[spring-boot-redis](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-redis):Spring Boot 3.0 Redis 示例
2728

2829

2930
---

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp