Spring boot集成redis lettuce代码实例
作者:穆晟铭 发布时间:2022-08-03 12:21:19
spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端
引入依赖
<!-- spring boot redis 缓存引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
配置文件
#Redis 配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=123456
#Redis数据库索引(默认为0)
spring.redis.database=0
##连接超时时间
spring.redis.timeout=60s
# 以下连接池已在SpringBoot2.0不推荐使用
##连接池最大连接数(使用负值表示没有限制)
#spring.redis.jedis.pool.max-active=10
##连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.jedis.pool.max-wait=-1ms
##连接池中的最大空闲连接
#spring.redis.jedis.pool.max-idle=8
##连接池中的最小空闲连接
#spring.redis.jedis.pool.min-idle=0
# Lettuce
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
# 关闭超时时间
spring.redis.lettuce.shutdown-timeout=100
配置config
@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class RedisConfig {
// @Bean
// public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.setConnectionFactory(factory);
// return redisTemplate;
// }
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory factory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(factory);
return template;
}
@Bean
public HashOperations<String, String, String> hashOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public SetOperations<String, String> setOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ListOperations<String, String> listOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForList();
}
}
来源:https://www.cnblogs.com/achengmu/p/11345220.html


猜你喜欢
- 一、前言通过前面我们也知道,通过getMapper方式来进行查询,最后会通过mapperMehod类,对接口中传来的参数也会在这个类里面进行
- 本文实例讲述了C#非矩形窗体实现方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Colle
- 目录一 为什么要用锁二 synchronized怎么实现的三 CAS来者何人四synchronized和CAS孰优孰劣轻量级锁重量级锁总结提
- 背景相信很多人都使用过 start.spring.io 来初始化自己的 Spring Boot 工程,这个工具为开发者提供了丰富的可选组件,
- C#中的泛型和反射经常是一起工作的,因此这里就一次性的加以介绍了。由于c#是强类型语言,一般来说函数的返回类型和参数的类型都是一早写好的,这
- 通过邮件找回密码功能的实现1、最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回。现在的系统在注册的时候都会强制输入邮箱,其一目的就是
- 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数
- 项目中遇到C#调用C++算法库的情况,C++内部运算结果返回矩形坐标数组(事先长度未知且不可预计),下面方法适用于访问C++内部分配的任何结
- 一、JDK中常见的异常情况1、常见异常总结图2、java中异常分类Throwable类有两个直接子类:(1)Exception:出现的问题是
- 下边是一些我们常用的正则表达式。自己写的一些正则表达式,可以先在线测评一下。一、校验数字的表达式 1 数字:^[0-9]*$&nb
- 一、介绍Mesh类:通过脚本创建或是获取网格的类,网格包含多个顶点和三角形数组。顶点信息包含坐标和所在面的法线。unity中3D的世界的所有
- 最近项目中经常需要用到自定义控件,因此自定义属性也是经常要用到的,在此说明一下自定义属性的用法:自定义属性都存在于/value/attr.x
- 基于SpringAOP已经实现统一功能增强,但如果希望对Controller增强,就无法获取其中的http请求数据。因此,实现以下这些统一增
- 众所周知,面向对象编程的特点为:封装、继承、多态。C#是一门完全面向对象的语言,由于比Java推出的时间还要晚,所以对面向对象的思想的体现比
- Java代码package com.zzx.controller;import com.zzx.model.User;import org.
- 我就废话不多说了,大家还是直接看代码吧~package com.jalor;import java.util.ArrayList;impor
- 本文实例讲述了Android学习笔记之应用单元测试。分享给大家供大家参考,具体如下:第一步:在AndroidManifest.xml中加入如
- Javaweb获取表单数据的几种方式一、通过键值对的形式获取表单数据getParameter(String name):通过key,返回一个
- 一、File --> new -->project二、构建maven项目。三、创建项目名,报名,项目路径。四、选择好maven仓
- 一次性全部绘制出来实现代码import java.awt.*;public class AlgoVisualizer {private st