-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.neo</groupId> | ||
<artifactId>spring-boot-redis</artifactId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-redis</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>17</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<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> | ||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
3.x/spring-boot-redis/src/main/java/com/neo/RedisApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class RedisApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(RedisApplication.class, args); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
3.x/spring-boot-redis/src/main/java/com/neo/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.neo.config; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.CachingConfigurerSupport; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.interceptor.KeyGenerator; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
|
||
@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(); | ||
} | ||
}; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
3.x/spring-boot-redis/src/main/java/com/neo/config/SessionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
|
||
@Configuration | ||
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) | ||
public class SessionConfig { | ||
} |
88 changes: 88 additions & 0 deletions
88
3.x/spring-boot-redis/src/main/java/com/neo/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.neo.model; | ||
|
||
import java.io.Serializable; | ||
|
||
|
||
|
||
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 + '\'' + | ||
'}'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
3.x/spring-boot-redis/src/main/java/com/neo/web/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.neo.web; | ||
|
||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.neo.model.User; | ||
|
||
import java.util.UUID; | ||
|
||
@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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
3.x/spring-boot-redis/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# REDIS | ||
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09 | ||
spring.redis.database=0 | ||
# Redis\u670D\u52A1\u5668\u5730\u5740 | ||
spring.redis.host=localhost | ||
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3 | ||
spring.redis.port=6379 | ||
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 | ||
spring.redis.password= | ||
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 8 | ||
spring.redis.lettuce.pool.max-active=8 | ||
# \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 | ||
spring.redis.lettuce.pool.max-wait=-1 | ||
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 8 | ||
spring.redis.lettuce.pool.max-idle=8 | ||
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 0 | ||
spring.redis.lettuce.pool.min-idle=0 |
18 changes: 18 additions & 0 deletions
18
3.x/spring-boot-redis/src/test/java/com/neo/RedisApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.neo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class RedisApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
System.out.println("hello web"); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
3.x/spring-boot-redis/src/test/java/com/neo/TestRedis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.neo; | ||
|
||
import com.neo.model.User; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class TestRedis { | ||
|
||
@Autowired | ||
private StringRedisTemplate stringRedisTemplate; | ||
|
||
@Autowired | ||
private RedisTemplate redisTemplate; | ||
|
||
@Test | ||
public void test() throws Exception { | ||
stringRedisTemplate.opsForValue().set("aaa", "111"); | ||
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); | ||
} | ||
|
||
@Test | ||
public void testObj() throws Exception { | ||
User user=new User("aa@126.com", "aa", "aa123456", "aa","123"); | ||
ValueOperations<String, User> operations=redisTemplate.opsForValue(); | ||
operations.set("com.neox", user); | ||
operations.set("com.neo.f", user,1, TimeUnit.SECONDS); | ||
Thread.sleep(1000); | ||
//redisTemplate.delete("com.neo.f"); | ||
boolean exists=redisTemplate.hasKey("com.neo.f"); | ||
if(exists){ | ||
System.out.println("exists is true"); | ||
}else{ | ||
System.out.println("exists is false"); | ||
} | ||
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters