spring boot 集成 Redis
前提:你已经安装了Redis
1、创建一个spring boot 工程
2、pom 引入依赖:spring-boot-starter-data-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3、在application.propertise 配置Redis相关参数
########################################################
###redis (redis)
########################################################
#开发
spring.redis.host=localhost
spring.redis.port= 6379
spring.redis.password=123456 spring.session.store-type=redis
spring.redis.database=1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle= 8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle= 0
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active= 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait= -1
# 连接超时时间(毫秒)
spring.redis.timeout= 6000
4.创建Redis工厂
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host; @Value("${spring.redis.port}")
private int port; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis; @Value("${spring.redis.password}")
private String password; @Bean
public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool;
}
5、需要redis时,直接注入即可
@Autowired
private RedisTemplate redisTemplate;
6. redisTemplate 使用,Redis可以操作五种数据类型(字符串(String)、哈希/散列/字典(Hash)、列表(List)、集合(Set)、有序集合(sorted set))
6.1 字符串(String)
保存
redisTemplate.opsForValue().set("key1","value1");
redisTemplate.opsForValue().set("key2","value2");
取值
String result1=redisTemplate.opsForValue().get("key1").toString();
String result2=redisTemplate.opsForValue().get("key2").toString();
6.2 哈希/散列/字典(Hash)
保存
Map<String,String> map=new HashMap<String,String>();
map.put("key1","1");
map.put("key2","2");
map.put("key3","3");
map.put("key4","4");
map.put("key5","5");
redisTemplate.opsForHash().putAll("hashMap",map);
取值
String value=(String)redisTemplate.opsForHash().get("hashMap","key1"); //结果是 1
Map<String,String> resultMap= redisTemplate.opsForHash().entries("hashMap"); //结果是 {key1=1, key2=2, key5=5, key3=3, key4=4}
List<String>reslutMapList=redisTemplate.opsForHash().values("hashMap"); //结果是 取得所有value值[1,2,3,4,5]
Set<String>resultMapSet=redisTemplate.opsForHash().keys("hashMap"); //结果是 取得key值 [key1, key2, key5, key3, key4]
6.3 列表(List)
保存
List<Pays> requestJsonList = new Array<>();
redisTemplate.opsForList().leftPush("key1", requestJsonList); //将list集合放入Redis
取值
List<Pays> requestJsonList1 = (List<Pays>) redisTemplate.opsForList().leftPop("key1");
7.总结操作
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
spring boot 集成 Redis的更多相关文章
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- Spring Boot 2.X(六):Spring Boot 集成Redis
Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...
- SpringBoot(十一): Spring Boot集成Redis
1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...
- spring boot集成redis基础入门
redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
- spring boot集成redis实现session共享
1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...
- spring boot 集成 redis lettuce
一.简介 spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端,两种客户端的区别如下 # Jedis和L ...
- Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...
- spring boot 集成 redis lettuce(jedis)
spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot ...
- Spring Boot集成Redis集群(Cluster模式)
目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...
随机推荐
- Ubuntu的sources.list
参考文章: https://blog.csdn.net/u012843189/article/details/80964287 位于/etc/apt/sources.list 其中 deb http: ...
- 防XSS攻击解决方法
1.web.xml文件中新增filter配置 <!-- URL请求参数字符过滤或合法性校验 --> <filter> <filter-name>XssFilter& ...
- 马拉车算法——边界拓展时加限制hdu4513
#include<bits/stdc++.h> using namespace std; #define maxn 500005 int n,p[maxn],s[maxn],s_new[m ...
- css属性之统一设置文本及div之间的对齐方式
设为 Flex 布局以后,子元素的float.clear和vertical-align属性将失效.hdp-uf{ display: -webkit-box; /* 老版本语法: Safari, iOS ...
- Appium 测试微信小程序 Webview
通过微信打开debugx5.qq.com,或者直接扫下面二维码 勾选[打开TBS内核Inspector调试功能] Chrome查看页面元素 手机连接电脑,查看是否连接成功.如下展示设备号则为连 ...
- Safari无痕模式是不能只使用localStorage存储数据要用Cookie做补丁
safari 无痕浏览情况测试(部分手机) 1.测试机型 iPhone7 Plus 版本 11.3 iPhone6 Plus 版本 11.3.1 iPhone6 版本 10.2.1 iP ...
- Oracle 去特殊字符
Create Or Replace Function Zl_Fun_去特殊字符(内容_In In Varchar2) Return Varchar2 IsBegin Return Replace(Re ...
- SoftEther
sudo apt-get update sudo wget http://www.softether-download.com/files/softether/v4.25-9656-rtm-201 ...
- RESTful-5开发API
RESTful 是目前最流行的 API 设计规范,用于 Web 数据接口的设计. 它的大原则容易把握,但是细节不容易做对.本文总结 RESTful 的设计细节,介绍如何设计出易于理解和使用的 API. ...
- 三大家族(offset、scroll、client)
offset.scroll.client三大家族 offset家族 offsetWidth 与 offsetHeight offset 偏移 用于获取元素自身的位置和大小 offsetWidth和of ...