- * key和keyGenerator是互斥,如果同时制定会出异常
- * The key and keyGenerator parameters are mutually exclusive and an operation specifying both will result in an exception.
- *
- * @param id
- * @return
+ * 创建用户,同时使用新的返回值的替换缓存中的值
+ * 创建用户后会将allUsersCache缓存全部清空
*/
- @Cacheable(keyGenerator = "myKeyGenerator")
- public User queryUserById(int id) {
- logger.info("queryUserById,id={}", id);
- return userMapper.selectById(id);
- }
-
- /**
- * 每次执行都会执行方法,同时使用新的返回值的替换缓存中的值
- *
- * @param user
- */
- @CachePut(key = "#user.id")
- public void createUser(User user) {
+ @Caching(
+ put = {@CachePut(value = "userCache", key = "#user.id")},
+ evict = {@CacheEvict(value = "allUsersCache", allEntries = true)}
+ )
+ public User createUser(User user) {
logger.info("创建用户start..., user.id=" + user.getId());
userMapper.insert(user);
+ return user;
}
/**
- * 每次执行都会执行方法,同时使用新的返回值的替换缓存中的值
- *
- * @param user
+ * 更新用户,同时使用新的返回值的替换缓存中的值
+ * 更新用户后会将allUsersCache缓存全部清空
*/
- @CachePut(key = "#user.id")
- public void updateUser(User user) {
+ @Caching(
+ put = {@CachePut(value = "userCache", key = "#user.id")},
+ evict = {@CacheEvict(value = "allUsersCache", allEntries = true)}
+ )
+ public User updateUser(User user) {
logger.info("更新用户start...");
userMapper.updateById(user);
+ return user;
}
/**
- * 对符合key条件的记录从缓存中book1移除
+ * 对符合key条件的记录从缓存中移除
+ * 删除用户后会将allUsersCache缓存全部清空
*/
- @CacheEvict(key = "#id")
+ @Caching(
+ evict = {
+ @CacheEvict(value = "userCache", key = "#id"),
+ @CacheEvict(value = "allUsersCache", allEntries = true)
+ }
+ )
public void deleteById(int id) {
logger.info("删除用户start...");
userMapper.deleteById(id);
}
- /**
- * allEntries = true: 清空user1里的所有缓存
- */
- @CacheEvict(allEntries=true)
- public void clearUser1All(){
- logger.info("clearAll");
- }
-
}
diff --git a/springboot-cache/src/main/resources/application.yml b/springboot-cache/src/main/resources/application.yml
index 64f1182..6fd4aa3 100644
--- a/springboot-cache/src/main/resources/application.yml
+++ b/springboot-cache/src/main/resources/application.yml
@@ -10,9 +10,9 @@ spring:
profiles:
active: dev
datasource:
- url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
+ url: jdbc:mysql://123.207.66.156:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
username: root
- password: 123456
+ password: _EnZhi123
################### mybatis-plus配置 ###################
mybatis-plus:
@@ -42,10 +42,22 @@ spring:
profiles: dev
cache:
type: REDIS
+ redis:
+ cache-null-values: false
+ time-to-live: 600000ms
+ use-key-prefix: true
+ cache-names: userCache,allUsersCache
redis:
host: 123.207.66.156
port: 6379
database: 0
+ lettuce:
+ shutdown-timeout: 200ms
+ pool:
+ max-active: 7
+ max-idle: 7
+ min-idle: 2
+ max-wait: -1ms
logging:
level:
diff --git a/springboot-cache/src/test/java/com/xncoding/service/UserServiceTest.java b/springboot-cache/src/test/java/com/xncoding/service/UserServiceTest.java
index 6d167d7..113c36e 100644
--- a/springboot-cache/src/test/java/com/xncoding/service/UserServiceTest.java
+++ b/springboot-cache/src/test/java/com/xncoding/service/UserServiceTest.java
@@ -7,8 +7,11 @@ 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.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.transaction.annotation.Transactional;
+import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertEquals;
@@ -24,24 +27,41 @@ import static org.junit.Assert.assertNull;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
+@Transactional
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testCache() {
- int id = new Random().nextInt(100);
+ // 创建一个用户admin
+ int id = new Random().nextInt(1000);
User user = new User(id, "admin", "admin");
userService.createUser(user);
- User user1 = userService.getById(id); // 第1次访问
- assertEquals(user1.getPassword(), "admin");
- User user2 = userService.getById(id); // 第2次访问
- assertEquals(user2.getPassword(), "admin");
- User user3 = userService.queryUserById(id); // 第3次访问,使用自定义的KeyGenerator
+
+ // 再创建一个用户xiong
+ int id2 = new Random().nextInt(1000);
+ User user2 = new User(id2, "xiong", "neng");
+ userService.createUser(user2);
+
+ // 查询所有用户列表
+ List