用法一
@Autowired
private RedissonClient redissonClient;
private RMap<String, Object> rMap;
static final String R_MAP = "rMap";
@PostConstruct
private void init() {
rMap = redissonClient.getMapCache(R_MAP);
}
RList<Object> flushCacheHistory = redissonClient.getList("名称");
RMap<String, Object> flushMap = redissonClient.getMap("名称");
用法二 原文链接:https://www.lovecto.cn/20190125/420.html
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.redisson.api.BatchOptions;
import org.redisson.api.RBatch;
import org.redisson.api.RMap;
import org.redisson.api.RMapAsync;
import org.redisson.api.RMapCache;
import org.redisson.api.RMapCacheAsync;
import org.redisson.api.RSetCache;
import org.redisson.api.RSetCacheAsync;
import org.redisson.api.RedissonClient;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class CacheMap {
private final static int NULL_VALUE_KEY_CACHE_MILLISECONDS = 10 * 60 * 1000;
private RMap rMap;
private RSetCache rSet;
private RedissonClient redisson;
private BatchOptions options = BatchOptions.defaults();
private boolean ttlControl;
private String name;
private String setName;
private boolean cacheNull;
public static CacheMap of(RedissonClient redisson, String name,
boolean ttlControl, boolean cacheNull) {
return new CacheMap(redisson, name, ttlControl, cacheNull);
}
private CacheMap(RedissonClient redisson, String name,
boolean ttlControl, boolean cacheNull) {
this.name = name;
this.setName = name + "_null_value_key_set";
if (ttlControl) {
this.rMap = redisson.getMapCache(this.name);
} else {
this.rMap = redisson.getMap(this.name);
}
if (cacheNull) {
this.rSet = redisson.getSetCache(this.setName);
}
this.ttlControl = ttlControl;
this.redisson = redisson;
this.cacheNull = cacheNull;
}
public boolean containsKey(Object key) {
boolean r = rMap.containsKey(key);
if (!r) {
if(cacheNull){
r = rSet.contains(key);
}
}
return r;
}
public void clear() {
rMap.clear();
if (cacheNull) {
rSet.clear();
}
}
public Map get(Object key) {
Map map = Maps.newHashMapWithExpectedSize(1);
Object r = rMap.get(key);
if ((r == null && cacheNull && rSet.contains(key)) || r != null) {
map.put(key, r);
}
return map;
}
public Object put(Object key, Object value) {
if (value == null) {
if (cacheNull) {
rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
} else {
rMap.put(key, value);
}
return value;
}
public Object remove(Object key) {
Object o = rMap.remove(key);
if (cacheNull) {
rSet.remove(key);
}
return o;
}
public void putAll(Map map) {
RBatch batch = redisson.createBatch(options);
RMapAsync mapCache = ttlControl ? batch.getMapCache(this.name) : batch
.getMap(this.name);
RSetCacheAsync setCache = batch.getSetCache(this.setName);
Set<Map.Entry> entrySet = map.entrySet();
Iterator<Map.Entry> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry e = it.next();
if (e.getValue() == null) {
if (cacheNull) {
setCache.addAsync(e.getKey(),
NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
} else {
mapCache.putAsync(e.getKey(), e.getValue());
}
}
batch.execute();
}
public Map getAll(Set keys) {
Map map = rMap.getAll(keys);
if (keys.size() == map.size()) {
return map;
} else if (cacheNull) {
Set set = rSet.readAll();
for (Object key : keys) {
if (!map.containsKey(key)) {
if (set.contains(key)) {
map.put(key, null);
}
}
}
}
return map;
}
public long fastRemove(Object... keys) {
long r = rMap.fastRemove(keys);
if (cacheNull) {
rSet.removeAll(Sets.newHashSet(keys));
}
return r;
}
public boolean fastPut(Object key, Object value) {
boolean r = true;
if (value != null) {
r = rMap.fastPut(key, value);
} else if (cacheNull) {
rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
return false;
}
public Object put(Object key, Object value, long ttl) {
if(ttl <= 0){
return put(key, value);
}
if (value != null) {
if (ttlControl) {
((RMapCache) rMap).put(key, value, ttl, TimeUnit.MILLISECONDS);
} else {
throw new IllegalArgumentException("can not support ttl");
}
} else if (cacheNull) {
rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
return value;
}
public void putAll(Map map, long ttl) {
if(ttl <= 0){
putAll(map);
return;
}
RBatch batch = redisson.createBatch(options);
RMapCacheAsync rMapCache = batch.getMapCache(this.name);
Set<Map.Entry> entrySet = map.entrySet();
Iterator<Map.Entry> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry e = it.next();
if (e.getValue() != null) {
if (ttlControl) {
rMapCache.putAsync(e.getKey(), e.getValue(), ttl,
TimeUnit.MILLISECONDS);
} else {
throw new IllegalArgumentException("can not support ttl");
}
} else if (cacheNull) {
rSet.add(e.getKey(), NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
}
batch.execute();
}
public boolean fastPut(Object key, Object value, long ttl) {
boolean r = true;
if (value != null) {
if (ttlControl) {
r = ((RMapCache) rMap).fastPut(key, value, ttl,
TimeUnit.MILLISECONDS);
} else {
throw new IllegalArgumentException("can not support ttl");
}
} else if (cacheNull) {
rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
TimeUnit.MILLISECONDS);
}
return r;
}
}