(1)、添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

(2)、配置Redis

  1. spring.redis.host=192.168.205.128

(3)、使用

配置好Redis后,缓存注解就会把数据缓存到Redis中了,如果我们想要手动添加缓存可以使用

    1. StringRedisTemplate  仅仅操作字符串
    1. RedisTemplate  操作Key-Value对象

**Redis默认使用Jdk序列化机制(将对象序列化到Redis中必须要实现Serializable接口且缓存的数据是二进制),若要将数据以Json的形式序列化,可采用以下两种方式

  1.使用第三方jar包将对象转化为json

  2.更改redisTemplate默认的序列化规则

  1. package cn.coreqi.config;
  2.  
  3. import cn.coreqi.entities.User;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  8. import org.springframework.data.redis.cache.RedisCacheManager;
  9. import org.springframework.data.redis.cache.RedisCacheWriter;
  10. import org.springframework.data.redis.connection.RedisConnectionFactory;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  13. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  14. import org.springframework.data.redis.serializer.RedisSerializationContext;
  15. import org.springframework.data.redis.serializer.RedisSerializer;
  16.  
  17. import java.time.Duration;
  18.  
  19. @Configuration
  20. public class MyRedisConfig {
  21. @Bean
  22. public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory){
  23. RedisTemplate<Object, User> template = new RedisTemplate<>();
  24. template.setConnectionFactory(redisConnectionFactory);
  25. template.setDefaultSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
  26. return template;
  27. }
  28.  
  29. @Primary
  30. @Bean
  31. public RedisCacheManager userCacheManager(RedisConnectionFactory redisConnectionFactory) {
  32. //初始化一个RedisCacheWriter
  33. RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
  34. //设置CacheManager的值序列化方式为json序列化
  35. RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
  36. RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
  37. .fromSerializer(jsonSerializer);
  38. RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
  39. .serializeValuesWith(pair);
  40. //设置默认超过期时间是30秒
  41. defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
  42. //初始化RedisCacheManager
  43. return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
  44. }
  45. }
  1. @Service
  2. @CacheConfig(cacheNames = "user",cacheManager = "userCacheManager") //抽取缓存的公共配置
  3. //可以使用@Caching注解在方法上运用多个缓存注解
  4. public class UserService {
  5. .......
  6. }

*如何在代码中获取缓存

  1. @Qualifier("userCacheManager")
  2. @Autowired
  3. private RedisCacheManager userCacheManager;
  4.  
  5. public User cache(){
  6. Cache user = userCacheManager.getCache("user");
  7. user.put("2",new User("gaoxing","fanqisoft",1));
  8. return user.get("1",User.class);
  9. }

SpringBoot使用Redis缓存的更多相关文章

  1. springboot整合redis缓存

    使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...

  2. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  3. SpringBoot使用redis缓存List<Object>

    一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...

  4. springboot集成redis缓存

    1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...

  5. SpringBoot整合redis缓存(一)

    准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...

  6. Java SpringBoot使用Redis缓存和Ehcache

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

  7. springboot整合redis缓存一些知识点

    前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...

  8. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  9. SpringBoot(七) - Redis 缓存

    1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...

  10. SpringBoot使用redis缓存List

    一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库: 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业 ...

随机推荐

  1. UVa - 10341

    Solve the equation:p ∗ e ^−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x ^2 + u = 02 + u = 0where ...

  2. 自学Zabbix12.5 Zabbix命令-zabbix_proxy

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.5 Zabbix命令-zabbix_proxy 1. zabbix prox ...

  3. HTML 百度地图API调用示例源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 前端学习 -- Html&Css -- ie6 png 背景问题

    在IE6中对图片格式png24支持度不高,如果使用的图片格式是png24,则会导致透明效果无法正常显示 解决方法: 1.可以使用png8来代替png24,即可解决问题,但是使用png8代替png24以 ...

  5. A1097. Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  6. 字符串格式化(七)-format

    print("i am %s" %'admin') # i am admin msg = "i am %s" %'Alex' print(msg) # i am ...

  7. C++初始化列表(good)

    本文转载自http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html 感谢作者分享 何谓初始化列表 与其他函数不同,构造函数除了有名 ...

  8. SQL SERVER与C#数据类型对照表

    分类 SQL SERVER类型 类型说明 C#类型 精确数字 bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807)  ...

  9. 即将上线的flume服务器面临的一系列填坑笔记

      即将上线的flume服务器面临的一系列填坑笔记 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.flume缺少依赖包导致启动失败! 报错信息如下: 2018-10-17 ...

  10. Spark进阶之路-Spark提交Jar包执行

    Spark进阶之路-Spark提交Jar包执行 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际开发中,使用spark-submit提交jar包是很常见的方式,因为用spark ...