Merge remote-tracking branch 'origin/master'
commit
b5ffa0c36e
@ -0,0 +1,79 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.redis;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.ValueOperations;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.apache.logging.log4j.ThreadContext.containsKey;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class RedisParentDao {
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
public void setRedisTemplate(RedisTemplate redisTemplate) {
|
||||||
|
this.redisTemplate = redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存value操作
|
||||||
|
* @param k
|
||||||
|
* @param v
|
||||||
|
* @param time
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean cacheValue(String k, String v, long time) {
|
||||||
|
try {
|
||||||
|
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
|
||||||
|
valueOps.set(k, v);
|
||||||
|
if (time > 0) redisTemplate.expire(k, time, TimeUnit.SECONDS);
|
||||||
|
return true;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
log.error("TbUploadFileNewController:fileUpload | 缓存失败!!!"+t);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断缓存是否存在
|
||||||
|
* @param k
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean containsValueKey(String k) {
|
||||||
|
return containsKey(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存
|
||||||
|
* @param k
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getValue(String k) {
|
||||||
|
try {
|
||||||
|
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
|
||||||
|
return valueOps.get(k);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
log.error("TbUploadFileNewController:fileUpload | 获取缓存失败!!!");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除缓存
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean remove(String key) {
|
||||||
|
try {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
return true;
|
||||||
|
} catch (Throwable t) {
|
||||||
|
log.error("TbUploadFileNewController:fileUpload | 移除缓存失败!!!");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue