com.zengtengpeng.operation.RedissonObject Maven / Gradle / Ivy
package com.zengtengpeng.operation;
import com.zengtengpeng.func.DataCache;
import com.zengtengpeng.func.RealData;
import com.zengtengpeng.properties.RedissonProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.util.ObjectUtils;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* 操作对象
*/
public class RedissonObject {
@Autowired
private RedissonClient redissonClient;
@Autowired
private RedissonProperties redissonProperties;
/**
* 获取对象值
*
* @param name
* @param
* @return
*/
public T getValue(String name) {
RBucket bucket = redissonClient.getBucket(name);
return bucket.get();
}
/**
* 获取对象值
*
* @param name
* @param
* @return
*/
public T getValue(String name, RealData realData) {
return getValue(name, realData, redissonProperties.getDataValidTime());
}
/**
* 获取对象值
*
* @param name
* @param
* @return
*/
public T getValue(String name, RealData realData, Long time) {
T value = getValue(name);
if (value == null) {
value = realData.get();
if (ObjectUtils.isEmpty(value)) {
//如果是空的,则删除
delete(name);
} else {
//否则insert
setValue(name, value, time);
}
}
return value;
}
/**
* 获取对象值
*
* @param name
* @param
* @return
*/
public T getValue(String name, RealData realData, DataCache dataCache, Long time) {
T value = getValue(name);
if (value == null) {
value = realData.get();
if (ObjectUtils.isEmpty(value)) {
//如果是空的,则删除
delete(name);
} else {
Boolean cache = dataCache.isCache(value);
if (cache) {
//否则insert
setValue(name, value, time);
}
}
}
return value;
}
/**
* 获取对象空间
*
* @param name
* @param
* @return
*/
public RBucket getBucket(String name) {
return redissonClient.getBucket(name);
}
/**
* 设置对象的值
*
* @param name 键
* @param value 值
* @return
*/
public void setValue(String name, T value) {
setValue(name, value, redissonProperties.getDataValidTime());
}
/**
* 设置对象的值
*
* @param name 键
* @param value 值
* @param time 缓存时间 单位毫秒 -1 永久缓存
* @return
*/
public void setValue(String name, T value, Long time) {
RBucket