前面的章節,講解了Spring Boot集成Spring Cache,Spring Cache已經完成了多種Cache的實現,包括EhCache、RedisCache、ConcurrentMapCache等。java
這一節咱們來看看Spring Cache使用RedisCache。git
Redis是一個key-value存儲系統,在web應用上被普遍應用,這裏就不對其過多描述了。github
本章節示例是在Spring Boot集成Spring Cache的源碼基礎上進行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cacheweb
使用RedisCache做爲緩存,咱們先引入相關依賴。redis
<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進行配置。spring
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一致。springboot
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