自己整理了 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 地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
TensorFlow 2.1.0 使用 TFRecord 转存与读取文本数据
荐 Python部分第三方库简要介绍及近期Python入门学习笔记
网友评论