spring boot结合Redis实现工具类的方法示例
作者:兰茗翔 时间:2023-05-12 09:47:46
自己整理了 spring boot 结合 Redis 的工具类
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
加入配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
实现代码
这里用到了 静态类工具类中 如何使用 @Autowired
package com.lmxdawn.api.common.utils;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
* 缓存操作类
*/
@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 维护一个本类的静态变量
private static CacheUtils cacheUtils;
@PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
* 将参数中的字符串值设置为键的值,不设置过期时间
* @param key
* @param value 必须要实现 Serializable 接口
*/
public static void set(String key, String value) {
cacheUtils.redisTemplate.opsForValue().set(key, value);
}
/**
* 将参数中的字符串值设置为键的值,设置过期时间
* @param key
* @param value 必须要实现 Serializable 接口
* @param timeout
*/
public static void set(String key, String value, Long timeout) {
cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 获取与指定键相关的值
* @param key
* @return
*/
public static Object get(String key) {
return cacheUtils.redisTemplate.opsForValue().get(key);
}
/**
* 设置某个键的过期时间
* @param key 键值
* @param ttl 过期秒数
*/
public static boolean expire(String key, Long ttl) {
return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
}
/**
* 判断某个键是否存在
* @param key 键值
*/
public static boolean hasKey(String key) {
return cacheUtils.redisTemplate.hasKey(key);
}
/**
* 向集合添加元素
* @param key
* @param value
* @return 返回值为设置成功的value数
*/
public static Long sAdd(String key, String... value) {
return cacheUtils.redisTemplate.opsForSet().add(key, value);
}
/**
* 获取集合中的某个元素
* @param key
* @return 返回值为redis中键值为key的value的Set集合
*/
public static Set<String> sGetMembers(String key) {
return cacheUtils.redisTemplate.opsForSet().members(key);
}
/**
* 将给定分数的指定成员添加到键中存储的排序集合中
* @param key
* @param value
* @param score
* @return
*/
public static Boolean zAdd(String key, String value, double score) {
return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
}
/**
* 返回指定排序集中给定成员的分数
* @param key
* @param value
* @return
*/
public static Double zScore(String key, String value) {
return cacheUtils.redisTemplate.opsForZSet().score(key, value);
}
/**
* 删除指定的键
* @param key
* @return
*/
public static Boolean delete(String key) {
return cacheUtils.redisTemplate.delete(key);
}
/**
* 删除多个键
* @param keys
* @return
*/
public static Long delete(Collection<String> keys) {
return cacheUtils.redisTemplate.delete(keys);
}
}
相关地址
GitHub 地址:https://github.com/lmxdawn/vue-admin-java
来源:https://segmentfault.com/a/1190000017131552
标签:spring,boot,Redis,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中JSON处理工具类使用详解
2023-09-19 17:59:08
android利用消息机制获取网络图片
2023-07-24 09:46:57
![](https://img.aspxhome.com/file/2023/2/85962_0s.jpg)
Java8 HashMap遍历方式性能探讨
2022-01-31 08:48:11
springBoot Junit测试用例出现@Autowired不生效的解决
2023-01-24 12:57:59
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
Android仿微信之界面导航篇(1)
2022-07-06 06:59:50
![](https://img.aspxhome.com/file/2023/8/138398_0s.jpg)
java后台批量下载文件并压缩成zip下载的方法
2021-07-24 21:13:11
Spring boot中filter类不能注入@Autowired变量问题
2023-04-24 14:17:41
![](https://img.aspxhome.com/file/2023/5/64375_0s.jpg)
Android实现WebView删除缓存的方法
2023-02-19 08:38:22
java中对字符串每个字符统计的方法
2023-09-23 13:53:16
Java使用@EnableEurekaServer实现自动装配详解
2023-08-23 19:50:32
![](https://img.aspxhome.com/file/2023/4/113484_0s.png)
Java生产1-100的随机数简单实例(分享)
2021-08-15 02:52:28
IntelliJ IDEA(2020.2)的下载、安装步骤详细教程
2023-11-25 07:10:16
![](https://img.aspxhome.com/file/2023/0/59900_0s.png)
详解Elasticsearch如何实现简单的脚本排序
2022-03-13 13:17:50
![](https://img.aspxhome.com/file/2023/5/87135_0s.jpg)
C# 扩展方法小结
2022-12-25 22:43:34
Android自定义控件实现简单滑动开关效果
2022-09-07 16:46:01
![](https://img.aspxhome.com/file/2023/8/137848_0s.jpg)
SpringBoot创建WebService方法详解
2022-02-08 10:29:31
![](https://img.aspxhome.com/file/2023/1/79821_0s.png)
C#实现基于ffmpeg加虹软的人脸识别的示例
2023-03-24 22:27:51
java开发之内部类的用法
2023-02-04 21:30:07
Android BottomSheet效果的两种实现方式
2022-10-14 02:26:23
![](https://img.aspxhome.com/file/2023/3/98643_0s.jpg)
Vue3源码解读effectScope API及实现原理
2023-12-11 19:28:49