SpringBoot使用Redis缓存
(1)、添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
(2)、配置Redis
- spring.redis.host=192.168.205.128
(3)、使用
配置好Redis后,缓存注解就会把数据缓存到Redis中了,如果我们想要手动添加缓存可以使用
- StringRedisTemplate 仅仅操作字符串
- RedisTemplate 操作Key-Value对象
**Redis默认使用Jdk序列化机制(将对象序列化到Redis中必须要实现Serializable接口且缓存的数据是二进制),若要将数据以Json的形式序列化,可采用以下两种方式
1.使用第三方jar包将对象转化为json
2.更改redisTemplate默认的序列化规则
- package cn.coreqi.config;
- import cn.coreqi.entities.User;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.data.redis.cache.RedisCacheConfiguration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.cache.RedisCacheWriter;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.RedisSerializationContext;
- import org.springframework.data.redis.serializer.RedisSerializer;
- import java.time.Duration;
- @Configuration
- public class MyRedisConfig {
- @Bean
- public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory){
- RedisTemplate<Object, User> template = new RedisTemplate<>();
- template.setConnectionFactory(redisConnectionFactory);
- template.setDefaultSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
- return template;
- }
- @Primary
- @Bean
- public RedisCacheManager userCacheManager(RedisConnectionFactory redisConnectionFactory) {
- //初始化一个RedisCacheWriter
- RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
- //设置CacheManager的值序列化方式为json序列化
- RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
- RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
- .fromSerializer(jsonSerializer);
- RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
- .serializeValuesWith(pair);
- //设置默认超过期时间是30秒
- defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
- //初始化RedisCacheManager
- return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
- }
- }
- @Service
- @CacheConfig(cacheNames = "user",cacheManager = "userCacheManager") //抽取缓存的公共配置
- //可以使用@Caching注解在方法上运用多个缓存注解
- public class UserService {
- .......
- }
*如何在代码中获取缓存
- @Qualifier("userCacheManager")
- @Autowired
- private RedisCacheManager userCacheManager;
- public User cache(){
- Cache user = userCacheManager.getCache("user");
- user.put("2",new User("gaoxing","fanqisoft",1));
- return user.get("1",User.class);
- }
SpringBoot使用Redis缓存的更多相关文章
- springboot整合redis缓存
使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- SpringBoot使用redis缓存List<Object>
一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...
- springboot集成redis缓存
1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...
- SpringBoot整合redis缓存(一)
准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...
- Java SpringBoot使用Redis缓存和Ehcache
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...
- springboot整合redis缓存一些知识点
前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- SpringBoot(七) - Redis 缓存
1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...
- SpringBoot使用redis缓存List
一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库: 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业 ...
随机推荐
- UVa - 10341
Solve the equation:p ∗ e ^−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x ^2 + u = 02 + u = 0where ...
- 自学Zabbix12.5 Zabbix命令-zabbix_proxy
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.5 Zabbix命令-zabbix_proxy 1. zabbix prox ...
- HTML 百度地图API调用示例源码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 前端学习 -- Html&Css -- ie6 png 背景问题
在IE6中对图片格式png24支持度不高,如果使用的图片格式是png24,则会导致透明效果无法正常显示 解决方法: 1.可以使用png8来代替png24,即可解决问题,但是使用png8代替png24以 ...
- A1097. Deduplication on a Linked List
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- 字符串格式化(七)-format
print("i am %s" %'admin') # i am admin msg = "i am %s" %'Alex' print(msg) # i am ...
- C++初始化列表(good)
本文转载自http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html 感谢作者分享 何谓初始化列表 与其他函数不同,构造函数除了有名 ...
- SQL SERVER与C#数据类型对照表
分类 SQL SERVER类型 类型说明 C#类型 精确数字 bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) ...
- 即将上线的flume服务器面临的一系列填坑笔记
即将上线的flume服务器面临的一系列填坑笔记 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.flume缺少依赖包导致启动失败! 报错信息如下: 2018-10-17 ...
- Spark进阶之路-Spark提交Jar包执行
Spark进阶之路-Spark提交Jar包执行 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际开发中,使用spark-submit提交jar包是很常见的方式,因为用spark ...