我正在使用@Cacheable批注在Spring Boot中缓存CRUD请求。我使用Redis缓存。缓存成功完成。当我插入新项目或更新现有项目时,我想从Redis中退出或删除多个缓存。为此,我在Spring Boot中使用@CacheEvict标记,如下所示。但是,删除缓存并没有发生。这里有什么问题? (我是Spring Environment和Redis的初学者)
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ExampleDB
spring.datasource.username=root
spring.datasource.password=#########
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.cache.type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.lettuce.pool.max-active=7
spring.redis.lettuce.pool.max-idle=7
spring.redis.lettuce.pool.min-idle=2
UserController.java
package com.springResearch.redisCaching;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.List;
@RestController(value = "/")
public class UserController {
@Autowired
JdbcTemplate jdbcTemplate;
private static final Logger logger = LogManager.getLogger("Log4J2AsyncLogger");
@Caching(evict = { @CacheEvict(value = "getallusers"), @CacheEvict(value = "userbyid", key = "#id"), @CacheEvict(value = "getuserbyname")})
@PostMapping(value = "/insertuser")
public String insertUser(@RequestParam int id, @RequestParam String name, @RequestParam String address){
jdbcTemplate.update("insert into user(userId, username, address) values(?, ?, ?)", id, name, address);
return "Success";
}
@Caching(evict = { @CacheEvict(value = "getallusers"), @CacheEvict(value = "getuserbyid", key = "#id"), @CacheEvict(value = "getuserbyname")})
@PutMapping(value = "/updateuserbyid")
public String updateUserByID(@RequestParam int id, @RequestParam String name){
jdbcTemplate.update(
"update user set username = ? where userId = ?", name, id
);
return "User succesfully updated";
}
@Cacheable(value = "getuserbyid", key = "#id")
@GetMapping(value = "/getuserbyid")
public List<Map<String, Object>> getUserById(@RequestParam int id){
logger.info("Data queried from database");
return this.jdbcTemplate.queryForList(
"SELECT userId, username, address FROM user WHERE userId = ?", id
);
}
@Cacheable(value = "getuserbyname")
@GetMapping(value = "/getuserbyname")
public List<Map<String, Object>> getUserByName(@RequestParam String name){
logger.info("Data queried from database");
return this.jdbcTemplate.queryForList(
"SELECT userId, username, address FROM user WHERE username = ?", name
);
}
@Cacheable(value = "getallusers")
@GetMapping(value = "/getallusers")
public List<Map<String, Object>> getAllUsers(){
List<Map<String, Object>> result = this.jdbcTemplate.queryForList(
"SELECT * FROM user"
);
logger.info("Table user was queried from database");
return result;
}
}
在Redis中调用 updateUserByID 缓存之前
127.0.0.1:6379>键*
1) "getuserbyid::4"
2) "getuserbyname::sugan"
3) "getallusers::SimpleKey []"
在Redis中调用 updateUserByID 缓存后
127.0.0.1:6379>键*
1) "getuserbyid::4"
2) "getuserbyname::sugan"
3) "getallusers::SimpleKey []"
我想在调用 insertUser 或 updateUserByID
时删除上述缓存记录