马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

[复制链接]
查看2242 | 回复0 | 2019-10-24 09:48:08 | 显示全部楼层 |阅读模式

前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包罗EhCache、RedisCache、ConcurrentMapCache等。

这一节我们来看看Spring Cache利用RedisCache。

一、RedisCache利用演示

Redis是一个key-value存储体系,在web应用上被广泛应用,这里就不对其过多形貌了。

本章节示例是在Spring Boot集成Spring Cache的源码基础上举行改造。源码地点:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

利用RedisCache作为缓存,我们先引入相干依赖。

  1. <code><dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!--redis依赖-->
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-redis</artifactId>
  9. </dependency></code>
复制代码

然后SpringBoot设置文件中,对redis举行设置。

  1. <code>server:
  2. port: 10900
  3. spring:
  4. profiles:
  5. active: dev
  6. redis:
  7. host: localhost #redis服务器地点
  8. port: 6379 #redis端口
  9. password: 1234 #redis暗码
  10. timeout: 60000 #连接超时时间
  11. database: 0 #数据库索引,默认为0</code>
复制代码

SpringBoot中利用Redis,可以通过Spring Cache的注解,也可以利用RedisTemplate来实现,大部分情况下,由于注解存在肯定范围性不敷机动,一般实际开辟中都是利用的RedisTemplate。

附上CacheConfig注入RedisTemplate,假如不必要利用RedisTemplate,直接将@EnableCaching注解加在SpringBoot启动类上即可。

  1. <code>@Configuration
  2. @EnableCaching
  3. public class CacheConfig {
  4. @Bean
  5. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  6. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  7. redisTemplate.setConnectionFactory(connectionFactory);
  8. // 利用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认利用JDK的序列化方式)
  9. Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
  10. ObjectMapper mapper = new ObjectMapper();
  11. mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  12. mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  13. serializer.setObjectMapper(mapper);
  14. redisTemplate.setValueSerializer(serializer);
  15. // 利用StringRedisSerializer来序列化和反序列化redis的key值
  16. redisTemplate.setKeySerializer(new StringRedisSerializer());
  17. redisTemplate.afterPropertiesSet();
  18. return redisTemplate;
  19. }
  20. }</code>
复制代码

如许就可以开始利用RedisCache了,测试代码与Spring Boot集成Spring Cache一致。

CacheApi接口调用类,方便调用举行测试。

  1. <code>@RestController
  2. @RequestMapping("cache")
  3. public class CacheApi {
  4. @Autowired
  5. private CacheService cacheService;
  6. @GetMapping("get")
  7. public User get(@RequestParam int id){
  8. return cacheService.get(id);
  9. }
  10. @PostMapping("set")
  11. public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
  12. User u = new User(code, name);
  13. return cacheService.set(id, u);
  14. }
  15. @DeleteMapping("del")
  16. public void del(@RequestParam int id){
  17. cacheService.del(id);
  18. }
  19. }</code>
复制代码

CacheService缓存业务处置惩罚类,添加缓存,更新缓存和删除。

  1. <code>@Slf4j
  2. @Service
  3. public class CacheService {
  4. private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
  5. {
  6. for (int i = 1; i < 100 ; i++) {
  7. User u = new User("code" + i, "name" + i);
  8. put(i, u);
  9. }
  10. }
  11. };
  12. // 获取数据
  13. @Cacheable(value = "cache", key = "&#39;user:&#39; + #id")
  14. public User get(int id){
  15. log.info("通过id{}查询获取", id);
  16. return dataMap.get(id);
  17. }
  18. // 更新数据
  19. @CachePut(value = "cache", key = "&#39;user:&#39; + #id")
  20. public User set(int id, User u){
  21. log.info("更新id{}数据", id);
  22. dataMap.put(id, u);
  23. return u;
  24. }
  25. //删除数据
  26. @CacheEvict(value = "cache", key = "&#39;user:&#39; + #id")
  27. public void del(int id){
  28. log.info("删除id{}数据", id);
  29. dataMap.remove(id);
  30. }
  31. }</code>
复制代码

启动服务举行测试,可以看到缓存功能正常,且打开redis举行查看,也能看到对应的缓存数据。

源码地点:https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis







来源:https://www.cnblogs.com/imyanger/p/11729490.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则