package com.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @description:
* @author: huangan
* @date: 2024/10/29 13:22
*/
@Slf4j
@Component
public class RedisUtils {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 写入redis缓存(不设置expire存活时间)
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 写入redis缓存(设置expire存活时间)
* @param key
* @param value
* @param expire
* @return
*/
public boolean set(final String key, String value, Long expire) {
boolean result = false;
try {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
log.error("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* map格式写入redis缓存(不设置expire存活时间)
* @param key
* @param modelMap
* @return
*/
public boolean setMap(String key, Map<String, Object> modelMap) {
boolean result = false;
try {
HashOperations<String, Object, Object> hps = redisTemplate.opsForHash();
hps.putAll(key, modelMap);
result = true;
} catch (Exception e) {
log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* map格式写入redis缓存(设置expire存活时间)
* @param key
* @param modelMap
* @param expire
* @return
*/
public boolean setMap(final String key, Map<String, Object> modelMap, Long expire) {
boolean result = false;
try {
HashOperations<String, Object, Object> hps = redisTemplate.opsForHash();
hps.putAll(key, modelMap);
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
log.error("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 读取redis缓存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
try {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
result = operations.get(key);
} catch (Exception e) {
log.error("读取redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 读取map类型redis缓存
* @param mapName
* @return
*/
public Map<String, Object> getMapValue(String mapName) {
Map<String, Object> result = null;
try {
HashOperations<String, String, Object> hps = redisTemplate.opsForHash();
result = hps.entries(mapName);
} catch (Exception e) {
log.error("读取redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 判断redis缓存中是否有对应的key
* @param key
* @return
*/
public boolean exists(final String key) {
boolean result = false;
try {
result = redisTemplate.hasKey(key);
} catch (Exception e) {
log.error("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据key删除对应的value
* @param key
* @return
*/
public boolean remove(final String key) {
boolean result = false;
try {
if (exists(key)) {
redisTemplate.delete(key);
}
result = true;
} catch (Exception e) {
log.error("redis根据key删除对应的value失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据keys批量删除对应的value
* @param keys
* @return
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
}
weixin_45863786
- 粉丝: 68
- 资源: 9
最新资源
- Matlab_Matlab线性算子工具箱.zip
- Matlab_Matlab文件用于各种类型的波束形成.zip
- Matlab_Matlab循环统计工具箱.zip
- Matlab_Matlab中的BP神经网络.zip
- Matlab_Matlab研究工具,读取、写入和处理地震数据.zip
- Matlab_Matlab中的曝光融合.zip
- Matlab_Matlab中的图像视频隐写.zip
- Matlab_Matlab中的图形信号处理.zip
- Matlab_MCMC工具箱的Matlab.zip
- Matlab_Matlab中的遗传算法.zip
- Matlab_MIDI工具箱11 2016是一个分析MIDI文件的Matlab函数集合.zip
- Matlab_MPC的简短例子,特别是随机MPC的SMPC与机会约束的Matlab.zip
- Matlab_NCTOOLBOX一个Matlab工具箱,用于处理常见的数据模型数据集.zip
- Matlab_MTEX是一个免费的Matlab定量纹理分析工具箱主页.zip
- Matlab_PILCO策略搜索框架Matlab版.zip
- Matlab_NIPS 2015论文的Matlab代码和补充材料用于序列建模的深度时序s型信念网络.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈