前提:你已经安装了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的更多相关文章

  1. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  2. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  3. SpringBoot(十一): Spring Boot集成Redis

    1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...

  4. spring boot集成redis基础入门

    redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...

  5. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  6. spring boot集成redis实现session共享

    1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...

  7. spring boot 集成 redis lettuce

    一.简介 spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端,两种客户端的区别如下 # Jedis和L ...

  8. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...

  9. spring boot 集成 redis lettuce(jedis)

    spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot ...

  10. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

随机推荐

  1. SQLAlchemy+Flask-RESTful使用(一)

    前言 开新坑啦.最近打算自己开一个资源聚合网站.就用Flask. 当然也使用了 Flask-RESTful和SQLAlchemy啦 写的过程中遇到过很多坑/觉得比较有意义的就写在这里. 变更记录 # ...

  2. centOS设置开机自启

    原文:https://blog.csdn.net/txz317/article/details/49683439 1.利用 chkconfig 来配置启动级别 在CentOS或者RedHat其他系统下 ...

  3. Django之Orm的各种操作

    1.一般操作 ***必知必会13条*** <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 models.Cu ...

  4. 2-4、配置Filebeat使用logstash

    配置filebeat使用logstash 重要:要将事件发送到Logstash,还需要创建一个Logstash配置管道,该管道监听传入的Beats连接并将收到的事件编入索引到Elasticsearch ...

  5. Bootstrap-datepicker3官方文档中文翻译---Methods/方法(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)

    Methods/方法 方法是由 datepicker 函数调用的,第一个参数为字符串,随后是方法所需的任何参数. $('.datepicker').datepicker('method', arg1, ...

  6. Hanlp学习笔记

    一.首先要引入mawen依赖包: <dependency> <groupId>com.hankcs</groupId> <artifactId>hanl ...

  7. PADS Layout VX.2.3 出Gerber文件时遇到一个奇怪的现象

    操作系统:Windows 10 x64 工具:PADS Layout VX.2.3 对CAM Documents的NC Drill的Units进行如下设置: 之后使用CAM Preview功能,查看N ...

  8. 最长共公子序列(LCS)

    #include <iostream> #include <cstdio> #include <algorithm> using namespace std; ], ...

  9. Vuejs自定义select2指令

    在做select2插件的时候遇到一些坑,最终解决如下: Vue.directive('select2', { inserted: function (el, binding, vnode) { var ...

  10. .net Core+Dapper MySQL增删改查

    新建一个用户表,以该有为例 1.Model层 public class TuiUsers { public int id { get; set; } public string userName { ...