原创

SpringBoot-@Caching-@CachePut-@CacheEvict-@EnableCaching-redis注解缓存

https://blog.csdn.net/m0_71890428/article/details/126440563
--------------------------------------------------------------------------------
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
--------------------------------------------------------------------------------
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
--------------------------------------------------------------------------------

package cn.jiangjiesheng.edu.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;


/**
* Redis 相关配置
*/
@Component
@Data
@RefreshScope
public class RedisConfig {

/**
* 设置序列化等缓存配置
*
* @return
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
// 设置序列化的方式
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(RedisSerializationContext
.SerializationPair
.fromSerializer(RedisSerializer.json()));
return redisCacheConfiguration;
}
}

--------------------------------------------------------------------------------

package cn.jiangjiesheng.edu.service.rediscache;

import cn.jiangjiesheng.edu.entity.user.UserInformation;
import cn.jiangjiesheng.edu.mapper.user.UserInformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.util.List;

// https://blog.csdn.net/m0_71890428/article/details/126440563
@Service
@CacheConfig(cacheNames = "cn.jiangjiesheng.edu.service.rediscache.RedisUserService" )
public class RedisUserService {

//另外paas中海油这个,按 : 号来
// @RedisClearCache(keys = {CacheConstants.ALL})

@Autowired
private UserInformationMapper userInformationMapper;

//'userAll' 表示的是常量,不加就是变量了
// 'userAll'+#user.id 表示常量加参数

// @CachePut(key = "#user.id")// 为添加的值指定id
@Caching(// 添加多个缓存操作
put = @CachePut(key = "#user.id"), //按这个key直接把入参user保存到redis,
evict = @CacheEvict(key = "'userAll'") //按这个key去失效
)
public UserInformation saveUser(UserInformation user) {
userInformationMapper.insert(user);
return user;
}

// @CachePut(key = "#user.id")
@Caching(
put = @CachePut(key = "#user.id"),
evict = @CacheEvict(key = "'userAll'")
)
public UserInformation updateUser(UserInformation user) {
userInformationMapper.updateByPrimaryKey(user);
return user;
}

// @CacheEvict(key = "#id")
@Caching(
evict = {@CacheEvict(key = "#id"), @CacheEvict(key = "'userAll'")}
)
public int removeUser(Integer id) {
return userInformationMapper.deleteByPrimaryKey(id);
}

@Cacheable(key = "#id")
public UserInformation findById(Integer id) {
return userInformationMapper.selectByPrimaryKey(id);
}

//@Cacheable(value = CacheConstants.GETDUPLICATEUSER, key = "'containerId:'+ #containerId", unless = "#result==null || #result.size() <= 0")
//这个表示缓存了key 为 'userAll' 的list结果
@Cacheable(key = "'userAll'", unless = "#result==null || #result.size() <= 0")
public List<UserInformation> findAll() {
return userInformationMapper.selectAll();
}
}

--------------------------------------------------------------------------------

package cn.jiangjiesheng.edu.controller.withoutLogin;

import cn.jiangjiesheng.edu.entity.user.UserInformation;
import cn.jiangjiesheng.edu.service.rediscache.RedisUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/withoutLogin/testRedisCache")
public class TestRedisCacheController {

@Autowired
private RedisUserService redisUserService;

@PostMapping("/saveUser")
public void saveUser(@RequestBody UserInformation user) {
redisUserService.saveUser(user);
}

@PostMapping("/updateUser")
public void updateUser(@RequestBody UserInformation user) {
redisUserService.updateUser(user);
}

@PostMapping("/removeUser")
public void removeUser(@RequestBody UserInformation user) {
redisUserService.removeUser(user.getId());
}

@GetMapping("/findById")
public UserInformation findById(Integer id) {
UserInformation byId = redisUserService.findById(id);
return byId;
}

@GetMapping("/findAll")
public List<UserInformation> findAll() {
return redisUserService.findAll();
}
}

正文到此结束
本文目录