苍穹外卖--springCache-@CacheEvict使用

This commit is contained in:
2025-11-24 11:53:19 +08:00
parent 2125db64f8
commit d1291b7a6c

View File

@@ -4,6 +4,7 @@ import com.inmind.entity.User;
import com.inmind.mapper.UserMapper; import com.inmind.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@@ -35,11 +36,19 @@ public class UserController {
return user; return user;
} }
/*
需求在根据id删除用户时redis也删除对应的缓存数据@CacheEvict
*/
@CacheEvict(cacheNames = "userCache",key = "#id")//key的生成 userCache::#id
@DeleteMapping @DeleteMapping
public void deleteById(Long id){ public void deleteById(Long id){
userMapper.deleteById(id); userMapper.deleteById(id);
} }
//allEntries = true:以cacheNames开头的所有的键全部删除userCache::*
@CacheEvict(cacheNames = "userCache",allEntries = true)//key的生成 userCache::#id
@DeleteMapping("/delAll") @DeleteMapping("/delAll")
public void deleteAll(){ public void deleteAll(){
userMapper.deleteAll(); userMapper.deleteAll();