工具类系列-StringSimpleRedisUtil
来源:互联网 发布:sql 求平均值 编辑:程序博客网 时间:2024/05/01 17:55
public class StringSimpleRedisUtil {
private static StringRedisTemplate stringRedisTemplate = SpringContext.getApplicationContext().getBean(
"stringRedisTemplate", StringRedisTemplate.class);
/**
*
* @param key 需实现方法入参
* @param clazz 序列化/反序列化的类
* @param redisKey 需要存取的redis KEY
* @param timeout redis超时
* @param function 数据源方法
* @param <K> 入参
* @param <V> 出参
* @return
*/
public static <K, V> V getCachedData(K key, Class<V> clazz, String redisKey, Integer timeout,
Function<K, V> function) {
boolean redisValidFlag = true;
String storeValue;
V storeObject = null;
try {
BoundValueOperations<String, String> op = stringRedisTemplate.boundValueOps(redisKey);
storeValue = op.get();
if (StringUtils.isNotBlank(storeValue)) {
if(!clazz.equals(String.class)){
storeObject = JsonExceptionUtil.toBean(storeValue, clazz);
}else {
storeObject = (V) storeValue;
}
} else {
storeObject = function.apply(key);
if(!clazz.equals(String.class)){
storeValue = JsonExceptionUtil.toJsonString(storeObject);
}else {
storeValue = (String) storeObject;
}
if (StringUtils.isNotBlank(storeValue)) {
op.set(storeValue);
if (op.getExpire() < 0) {
op.expire(timeout, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
redisValidFlag = false;
}
if (!redisValidFlag) {
storeObject = function.apply(key);
}
return storeObject;
}
/**
* 删除key
* @param key
*/
public static void deleteKey(String key){
try {
stringRedisTemplate.delete(key);
} catch (Exception e) {
}
}
/**
* 更新key过期时间
* @param key
*/
public static Boolean updateKeyExpire(String key,Integer timeOut){
try {
return stringRedisTemplate.expire(key,timeOut, TimeUnit.MINUTES);
} catch (Exception e) {
return false;
}
}
/**
* 获取key内容
* @param key
*/
public static String getKeyContent(String key) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
return stringStringBoundValueOperations.get();
} catch (Exception e) {
}
return null;
}
/**
* 设置key内容
* @param key
*/
public static void setKeyContent(String key,String content) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
stringStringBoundValueOperations.set(content);
} catch (Exception e) {
}
}
/**
* 批量删除key
* @param pattern
*/
public static void deleteKeyByPattern(String pattern){
Set<String> keys = stringRedisTemplate.keys(pattern);
if (!keys.isEmpty()) {
stringRedisTemplate.delete(keys);
}
}
}
private static StringRedisTemplate stringRedisTemplate = SpringContext.getApplicationContext().getBean(
"stringRedisTemplate", StringRedisTemplate.class);
/**
*
* @param key 需实现方法入参
* @param clazz 序列化/反序列化的类
* @param redisKey 需要存取的redis KEY
* @param timeout redis超时
* @param function 数据源方法
* @param <K> 入参
* @param <V> 出参
* @return
*/
public static <K, V> V getCachedData(K key, Class<V> clazz, String redisKey, Integer timeout,
Function<K, V> function) {
boolean redisValidFlag = true;
String storeValue;
V storeObject = null;
try {
BoundValueOperations<String, String> op = stringRedisTemplate.boundValueOps(redisKey);
storeValue = op.get();
if (StringUtils.isNotBlank(storeValue)) {
if(!clazz.equals(String.class)){
storeObject = JsonExceptionUtil.toBean(storeValue, clazz);
}else {
storeObject = (V) storeValue;
}
} else {
storeObject = function.apply(key);
if(!clazz.equals(String.class)){
storeValue = JsonExceptionUtil.toJsonString(storeObject);
}else {
storeValue = (String) storeObject;
}
if (StringUtils.isNotBlank(storeValue)) {
op.set(storeValue);
if (op.getExpire() < 0) {
op.expire(timeout, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
redisValidFlag = false;
}
if (!redisValidFlag) {
storeObject = function.apply(key);
}
return storeObject;
}
/**
* 删除key
* @param key
*/
public static void deleteKey(String key){
try {
stringRedisTemplate.delete(key);
} catch (Exception e) {
}
}
/**
* 更新key过期时间
* @param key
*/
public static Boolean updateKeyExpire(String key,Integer timeOut){
try {
return stringRedisTemplate.expire(key,timeOut, TimeUnit.MINUTES);
} catch (Exception e) {
return false;
}
}
/**
* 获取key内容
* @param key
*/
public static String getKeyContent(String key) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
return stringStringBoundValueOperations.get();
} catch (Exception e) {
}
return null;
}
/**
* 设置key内容
* @param key
*/
public static void setKeyContent(String key,String content) {
try {
BoundValueOperations<String, String> stringStringBoundValueOperations = stringRedisTemplate.boundValueOps(key);
stringStringBoundValueOperations.set(content);
} catch (Exception e) {
}
}
/**
* 批量删除key
* @param pattern
*/
public static void deleteKeyByPattern(String pattern){
Set<String> keys = stringRedisTemplate.keys(pattern);
if (!keys.isEmpty()) {
stringRedisTemplate.delete(keys);
}
}
}
0 0
- 工具类系列-StringSimpleRedisUtil
- 工具类系列-JavaBean2Map
- 工具类系列-ClazzReflectUtil
- 工具类系列-GenerateHtml
- 工具类系列-JackJsonStringTrimParser
- 工具类系列-DataFilterQueue
- 工具类系列-RequestRedirectUtil
- apache工具类系列
- Android工具类系列-BitmapUtil
- Android工具类系列-ToastUtil
- Android工具类系列-LogUtil
- Android工具类系列-SDCardUtil
- Android工具类系列-KeyBoardUtil
- Android工具类系列-NetUtil
- Android工具类系列-HttpUtil
- Android工具类系列-AppUtil
- Android工具类系列-ScreenUtil
- Android工具类系列-SPUtil(SharedPreferences工具类)
- DGN格式转化为shp格式
- 国外免费 E-Book
- Java初学者:技术文章(类的继承)
- "围观"设计模式(15)--结构型之桥梁模式(Bridge Pattern)
- Java继承内存分配
- 工具类系列-StringSimpleRedisUtil
- ImageLoader缓存图片
- 3.uCOSIII中断管理
- 数据库中的分组函数+练习题
- 测试hibernate时报错Field 'id' doesn't have a default value
- iOS开发_iPhone 实现剪贴板操作_iPhone 复制粘贴功能
- 栈及实现(C语言)以及中缀式到后缀式的转换实例
- 代码静态分析工具 --- CppCheck
- c++引用的总结