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
0
投稿
猜你喜欢
C#窗体实现酒店管理系统
2023-05-23 18:58:47
springboot 自定义异常并捕获异常返给前端的实现代码
2022-07-23 03:09:52
Maven中利用assembly插件打包jar包
2022-07-09 04:32:14
SpringBoot使用邮箱发送验证码实现注册功能
2022-03-30 10:13:36
Android中ListView下拉刷新的实现代码
2021-12-13 21:02:26
java时区转换的理解及示例详解
2022-01-19 08:35:20
Windows+Apache+resin配置
2023-10-16 08:53:07
MyBatis Plus插件机制与执行流程原理分析详解
2021-10-09 20:53:48
Java class文件格式之数据类型(二)_动力节点Java学院整理
2022-06-26 02:15:45
SpringBoot配置及使用Schedule过程解析
2021-12-31 18:47:32
Android源码中final关键字的用法及final,finally,finalize的区别
2023-10-11 09:03:43
c#取得控制台应用程序根目录
2023-06-16 14:25:44
解决使用ProcessBuilder踩到的坑及注意事项
2023-11-24 01:37:55
Android activity和view判断滑动
2021-09-18 17:25:17
Android三种方式实现ProgressBar自定义圆形进度条
2021-09-15 11:19:43
Java模拟死锁发生之演绎哲学家进餐问题案例详解
2022-10-09 22:52:44
在WPF中合并两个ObservableCollection集合
2022-11-01 18:42:10
一文带你搞懂Java中Get和Post的使用
2023-04-16 23:57:24
Spring Boot接口限流的常用算法及特点
2023-12-20 05:16:30
Java 多线程并发AbstractQueuedSynchronizer详情
2022-12-22 10:54:35