Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.zengtengpeng.operation.RedissonCollectionCache Maven / Gradle / Ivy
package com.zengtengpeng.operation;
import com.zengtengpeng.func.*;
import com.zengtengpeng.properties.RedissonProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.*;
import org.springframework.util.ObjectUtils;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* 操作映射缓存集合 请勿使用 RMapCache
* 高并发情况下无法自动淘汰元素,导致redis内存溢出,
* 请使用 Object对象.利用redis淘汰机制淘汰
* @author ztp
* @date 2023/2/14 15:17
*/
@Deprecated
public class RedissonCollectionCache {
@Autowired
private RedissonClient redissonClient;
@Autowired
private RedissonProperties redissonProperties;
/**
* 获取map集合
*
* @param name
* @param
* @param
* @return
*/
public RMapCache getMapCache(String name) {
return redissonClient.getMapCache(name);
}
/**
* 获取map集合,如果没有则通过实时数据
*
* @param name
* @param
* @param
* @return
*/
public RMapCache getMapCache(String name, RealDataMap realDataMap) {
return getMapCache(name, realDataMap, redissonProperties.getDataValidTime());
}
/**
* 获取map集合,如果没有则通过实时数据
*
* @param name
* @param
* @param
* @return
*/
public RMapCache getMapCache(String name, RealDataMap realDataMap, Long time) {
RMapCache map = redissonClient.getMapCache(name);
if (map == null || map.size() == 0) {
Map objectObjectMap = realDataMap.get();
setMapCacheValues(name, objectObjectMap, time);
}
return redissonClient.getMapCache(name);
}
/**
* 获取map集合对应的key值
*
* @param name
* @return
*/
public T getMapCacheValue(String name, String key) {
RMapCache mapCache = getMapCache(name);
return mapCache.get(key);
}
/**
* 先从map集合获取数据,如果没有则从接口获取
*
* @param name
* @return
*/
public T getMapCacheValue(String name, String key, RealData realData) {
return getMapCacheValue(name, key, realData, redissonProperties.getDataValidTime());
}
/**
* 先从map集合获取数据,如果没有则从接口获取
*
* @param name
* @return
*/
public T getMapCacheValue(String name, String key, RealData realData, Long time) {
RMapCache mapCache = getMapCache(name);
T o = mapCache.get(key);
if (o == null) {
o = realData.get();
if (ObjectUtils.isEmpty(o)) {
mapCache.remove(key);
} else {
setMapCacheValue(name, key, o, time);
}
}
return o;
}
/**
* 先从map集合获取数据,如果没有则从接口获取
*
* @param name
* @return
*/
public T getMapCacheValue(String name, String key, RealData realData, DataCache dataCache, Long time) {
RMapCache mapCache = getMapCache(name);
T o = mapCache.get(key);
if (o == null) {
o = realData.get();
if (ObjectUtils.isEmpty(o)) {
mapCache.remove(key);
} else {
Boolean cache = dataCache.isCache(o);
if (cache){
setMapCacheValue(name, key, o, time);
}
}
}
return o;
}
/**
* 设置map集合
*
* @param name
* @param data
* @param time 缓存时间,单位毫秒 -1永久缓存
* @return
*/
public void setMapCacheValues(String name, Map data, Long time) {
RMapCache map = redissonClient.getMapCache(name);
if (time == null) {
time = redissonProperties.getDataValidTime();
}
map.putAll(data, time, TimeUnit.MILLISECONDS);
}
/**
* 设置 map值
*
* @param name
* @param time 缓存时间,单位毫秒 0永久缓存
* @return
*/
public void setMapCacheValue(String name, String key, Object value, Long time) {
setMapCacheValue(name, key, value, time, 0L);
}
/**
* 设置 map值
*
* @param name
* @param time 缓存时间,单位毫秒 0永久缓存
* @return
*/
public void setMapCacheValue(String name, String key, T value, Long time, Long maxIdleTime) {
RMapCache map = redissonClient.getMapCache(name);
if (time == null) {
time = redissonProperties.getDataValidTime();
}
map.put(key, value, time, TimeUnit.MILLISECONDS, maxIdleTime, TimeUnit.MILLISECONDS);
}
/**
* 设置map集合
*
* @param name
* @param data
* @return
*/
public void setMapCacheValues(String name, Map data) {
setMapCacheValues(name, data, redissonProperties.getDataValidTime());
}
/**
* 设置map集合
*
* @param name
* @return
*/
public void setMapCacheValue(String name, String key, Object value) {
setMapCacheValue(name, key, value, redissonProperties.getDataValidTime());
}
/**
* 获取set集合
*
* @param name
* @return
*/
public RSetCache getSetCache(String name) {
return redissonClient.getSetCache(name);
}
/**
* 获取List集合 如果没有则通过实时数据获取
*
* @param name
* @return
*/
public RSetCache getSetCache(String name, RealDataSet realDataSet, Long time) {
RSetCache set = getSetCache(name);
if (set == null || set.size() == 0) {
Set objects = realDataSet.get();
setSetCacheValues(name, objects, time);
}
return getSetCache(name);
}
/**
* 获取List集合对应的index值
*
* @param name
* @return
*/
public RSetCache getSetCache(String name, RealDataSet realDataSet) {
return getSetCache(name, realDataSet, redissonProperties.getDataValidTime());
}
/**
* 设置set集合
*
* @param name
* @param data
* @param time 缓存时间,单位毫秒 -1永久缓存
* @return
*/
public void setSetCacheValues(String name, Set data, Long time) {
RSetCache set = redissonClient.getSetCache(name);
set.addAll(data);
Long dataValidTime = redissonProperties.getDataValidTime();
if (time == null) {
set.expire(Duration.ofMillis(dataValidTime));
} else if (time != -1) {
set.expire(Duration.ofMillis(time));
}
}
/**
* 设置set集合
*
* @param name
* @param data
* @param time 缓存时间,单位毫秒 -1永久缓存
* @return
*/
public void setSetCacheValue(String name, Object data, Long time) {
RSetCache set = redissonClient.getSetCache(name);
if (time == null) {
time = redissonProperties.getDataValidTime();
}
set.add(data, time, TimeUnit.MILLISECONDS);
}
/**
* 设置set集合
*
* @param name
* @param data
* @return
*/
public void setSetCacheValues(String name, Set data) {
setSetCacheValues(name, data, redissonProperties.getDataValidTime());
}
/**
* 设置set集合
*
* @param name
* @param data
* @return
*/
public void setSetValues(String name, Object data) {
setSetCacheValue(name, data, redissonProperties.getDataValidTime());
}
}