一、RedisCache使用演示
Redis是一个key-value存储系统,在web应用上被广泛应用,这里就不对其过多描述了。
本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
使用RedisCache作为缓存,我们先引入相关依赖。
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--redis依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
然后SpringBoot配置文件中,对redis进行配置。
- server:
- port: 10900
-
- spring:
- profiles:
- active: dev
- redis:
- host: localhost #redis服务器地址
- port: 6379 #redis端口
- password: 1234 #redis密码
- timeout: 60000 #连接超时时间
- database: 0 #数据库索引,默认为0
SpringBoot中使用Redis,可以通过Spring Cache的注解,也可以使用RedisTemplate来实现,大部分情况下,因为注解存在一定局限性不够灵活,一般实际开发中都是使用的RedisTemplate。
附上CacheConfig注入RedisTemplate,如果不需要使用RedisTemplate,直接将@EnableCaching注解加在SpringBoot启动类上即可。
- @Configuration
- @EnableCaching
- public class CacheConfig {
-
- @Bean
- public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
- redisTemplate.setConnectionFactory(connectionFactory);
-
- // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
- Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
-
- ObjectMapper mapper = new ObjectMapper();
- mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
- serializer.setObjectMapper(mapper);
-
- redisTemplate.setValueSerializer(serializer);
- // 使用StringRedisSerializer来序列化和反序列化redis的key值
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
- }
这样就可以开始使用RedisCache了,测试代码与Spring Boot集成Spring Cache一致。
CacheApi接口调用类,方便调用进行测试。
- @RestController
- @RequestMapping("cache")
- public class CacheApi {
-
- @Autowired
- private CacheService cacheService;
-
- @GetMapping("get")
- public User get(@RequestParam int id){
- return cacheService.get(id);
- }
-
- @PostMapping("set")
- public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
- User u = new User(code, name);
- return cacheService.set(id, u);
- }
-
- @DeleteMapping("del")
- public void del(@RequestParam int id){
- cacheService.del(id);
- }
-
- }
CacheService缓存业务处理类,添加缓存,更新缓存和删除。
- @Slf4j
- @Service
- public class CacheService {
-
- private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
- {
- for (int i = 1; i < 100 ; i++) {
- User u = new User("code" + i, "name" + i);
- put(i, u);
- }
- }
- };
-
- // 获取数据
- @Cacheable(value = "cache", key = "'user:' + #id")
- public User get(int id){
- log.info("通过id{}查询获取", id);
- return dataMap.get(id);
- }
-
- // 更新数据
- @CachePut(value = "cache", key = "'user:' + #id")
- public User set(int id, User u){
- log.info("更新id{}数据", id);
- dataMap.put(id, u);
- return u;
- }
-
- //删除数据
- @CacheEvict(value = "cache", key = "'user:' + #id")
- public void del(int id){
- log.info("删除id{}数据", id);
- dataMap.remove(id);
- }
-
- }
启动服务进行测试,可以看到缓存功能正常,且打开redis进行查看,也能看到对应的缓存数据。
源码地址:https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。