Redis在springboot中的使用教程
作者:野蛮-生长 时间:2021-10-02 18:10:46
依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件如下:
spring:
redis:
open: true # 是否开启redis缓存 true开启 false关闭
database: 0
host: 47.104.208.124
port: 6378
password: lf.1228 # 密码(默认为空)
timeout: 6000 # 连接超时时长(毫秒)
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
RedisConfig类:
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
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 HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
RedisUtils如下:
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private ValueOperations<String, String> valueOperations;
@Autowired
private HashOperations<String, String, Object> hashOperations;
@Autowired
private ListOperations<String, Object> listOperations;
@Autowired
private SetOperations<String, Object> setOperations;
@Autowired
private ZSetOperations<String, Object> zSetOperations;
/** 默认过期时长,单位:秒 */
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
/** 不设置过期时长 */
public final static long NOT_EXPIRE = -1;
private final static Gson gson = new Gson();
public void set(String key, Object value, long expire){
valueOperations.set(key, toJson(value));
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void set(String key, Object value){
set(key, value, DEFAULT_EXPIRE);
}
public <T> T get(String key, Class<T> clazz, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public <T> T get(String key, Class<T> clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public String get(String key, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}
public String get(String key) {
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* Object转成JSON数据
*/
private String toJson(Object object){
if(object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String){
return String.valueOf(object);
}
return gson.toJson(object);
}
/**
* JSON数据,转成Object
*/
private <T> T fromJson(String json, Class<T> clazz){
return gson.fromJson(json, clazz);
}
}
springboot如何封装redis:
RedisTemplate
所在包: org.springframework.data.redis.core
作用:redis模板,redis事务,序列化支持,操作redis方法
JedisConnectionFactory
所在包:org.springframework.data.redis.connection.jedis
作用:redis连接工厂类,创建redis连接池等
RedisAutoConfiguration
所在包:org.springframework.boot.autoconfigure.data.redis
作用:将redis配置文件相关信息注入工厂类
RedisProperties
所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis连接基础类通过@ConfigurationProperties注解将配置信息注入属性
总结
以上所述是小编给大家介绍的Redis在springboot中的使用教程网站的支持!
来源:https://blog.csdn.net/qq_39533847/article/details/80680873
标签:spring,boot,redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java算法题解牛客BM99顺时针旋转矩阵示例
2021-07-08 18:55:18
![](https://img.aspxhome.com/file/2023/3/80953_0s.jpg)
Java 客户端操作 FastDFS 实现文件上传下载替换删除功能
2022-06-01 15:01:38
![](https://img.aspxhome.com/file/2023/5/62325_0s.png)
浅谈Servlet开发技术基础
2023-01-17 04:03:15
RestTemplate 401 获取错误信息的处理方案
2023-01-14 10:37:44
![](https://img.aspxhome.com/file/2023/7/85427_0s.png)
spring boot 2整合swagger-ui过程解析
2021-08-08 22:57:35
![](https://img.aspxhome.com/file/2023/1/76481_0s.png)
基于Java8 Stream API实现数据抽取收集
2021-10-01 03:13:03
springboot mybatis里localdatetime序列化问题的解决
2023-06-25 06:58:18
java可以作为第一门编程语言学习吗
2021-05-25 07:52:37
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2022-06-10 04:17:53
SpringCloud URL重定向及转发代码实例
2023-11-19 11:43:18
TKmybatis的框架介绍和原理解析
2022-08-28 21:35:42
基于Jenkins搭建.NET Core持续集成环境过程图解
2021-10-25 03:44:33
![](https://img.aspxhome.com/file/2023/8/78858_0s.png)
Ireport的安装与使用教程
2021-08-08 00:16:43
![](https://img.aspxhome.com/file/2023/2/84632_0s.png)
解读classpath读取resources目录下的文件
2023-01-25 05:49:36
![](https://img.aspxhome.com/file/2023/2/85482_0s.png)
Java在web页面上的编码解码处理及中文URL乱码解决
2023-08-25 11:10:19
![](https://img.aspxhome.com/file/2023/6/58206_0s.gif)
winform 实现选择文件和选择文件夹对话框的简单实例
2022-02-21 07:05:12
springboot中使用Feign整合nacos,gateway进行微服务之间的调用方法
2021-07-12 07:00:28
![](https://img.aspxhome.com/file/2023/4/69304_0s.png)
解决java.lang.Error: Unresolved compilation problems:问题
2023-02-10 05:58:08
![](https://img.aspxhome.com/file/2023/6/82456_0s.png)
SpringBoot集成FTP与SFTP连接池流程
2021-12-27 12:22:26
![](https://img.aspxhome.com/file/2023/5/63055_0s.png)
C#使用opencv截取旋转矩形区域图像的实现示例
2023-11-12 22:20:44
![](https://img.aspxhome.com/file/2023/2/70412_0s.png)