redisson提供了很多对象类型的api,下面介绍下一些常用的对象api。
可操作任何对象的api,前提是要确定好泛型,方法比较少。大小限制为512Mb。
RBucket bucket = redisson.getBucket("anyObject");
bucket.set(new AnyObject(1));
AnyObject obj = bucket.get();
bucket.trySet(new AnyObject(3));
bucket.compareAndSet(new AnyObject(4), new AnyObject(5));
bucket.getAndSet(new AnyObject(6));
专门操作map的对象,实现了ConcurrentMap接口,并且put、set操作直接作用于redis。
RMap map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");
// use fast* methods when previous value is not required
map.fastPut("a", new SomeObject());
map.fastPutIfAbsent("d", new SomeObject());
map.fastRemove("b");
RFuture putAsyncFuture = map.putAsync("321");
RFuture fastPutAsyncFuture = map.fastPutAsync("321");
map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");
专门操作list的对象,实现了java.util.List, add、set等方法直接作用于redis。
RList list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());
package com.springboot.demo.base.utils;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.ObjectUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @description: redisson工具类
* @author: 小花卷的Dad
* @create: 2023/8/24
*/
@Component
public class RedissonUtil {
private static RedissonClient redissonClient;
/**
* 锁默认释放时间
*/
private static final long default_lease_time = 5L;
@Autowired
public void setRedissonClient(RedissonClient redissonClient) {
RedissonUtil.redissonClient = redissonClient;
}
/**
* key是否存在
* @param key
* @return
*/
public static boolean isExists(String key){
return redissonClient.getBucket(key).isExists();
}
/**
* 获取生命周期
* @param key
* @return
*/
public static long getExpireTime(String key){
return redissonClient.getBucket(key).remainTimeToLive();
}
/**
* 设置生命周期
* @param key
* @param time(毫秒)
* @return
*/
public static boolean setExpireTime(String key, Long expire){
return redissonClient.getBucket(key).expire(Duration.ofMillis(expire));
}
public static boolean delete(String key){
if(!isExists(key)){
return true;
}
return redissonClient.getBucket(key).delete();
}
/**
* 保存字符串
* @param key
* @param value
*/
public static void setStr(String key, String value){
RBucket rBucket = redissonClient.getBucket(key);
rBucket.set(value);
}
/**
* 保存字符串
* @param key
* @param value
* @param expire
*/
public static void setStr(String key, String value, Long expire){
RBucket rBucket = redissonClient.getBucket(key);
rBucket.set(value, Duration.ofMillis(expire));
}
/**
* 查询字符串
* @param key
* @return
*/
public static String getStr(String key){
if(isExists(key)){
return null;
}
RBucket rBucket = redissonClient.getBucket(key);
return rBucket.get();
}
/**
* 保存对象
* @param key
* @param value
* @param
*/
public static void setObject(String key, T value){
RBucket rBucket = redissonClient.getBucket(key);
rBucket.set(value);
}
/**
* 保存对象
* @param key
* @param value
* @param expire
* @param
*/
public static void setObject(String key, T value, Long expire){
RBucket rBucket = redissonClient.getBucket(key);
rBucket.set(value, Duration.ofMillis(expire));
}
/**
* 查询对象
* @param key
* @return
*/
public static T getObject(String key){
RBucket rBucket = redissonClient.getBucket(key);
return rBucket.get();
}
/**
* map.get
* @param key
* @param mapKey
* @param
* @return
*/
public static T mapGet(String key, String mapKey){
if(!isExists(key)){
return null;
}
Map rMap = redissonClient.getMap(key);
return rMap.get(mapKey);
}
/**
* 查询map
* @param key
* @param
* @return
*/
public static Map mapGetAll(String key){
RMap rMap = redissonClient.getMap(key);
return rMap.readAllMap();
}
/**
* map.put
* @param key
* @param mapKey
* @param mapValue
* @param
*/
public static void mapPut(String key, String mapKey,T mapValue){
RMap rMap = redissonClient.getMap(key);
rMap.put(mapKey, mapValue);
}
/**
* map.putAll
* @param key
* @param map
* @param
*/
public static void mapPutAll(String key, Map map){
RMap rMap = redissonClient.getMap(key);
rMap.putAll(map);
}
/**
* map.contains
* @param key
* @param mapKey
* @return
*/
public static boolean mapContains(String key, String mapKey){
if(!isExists(key)){
return false;
}
Map rMap = redissonClient.getMap(key);
return rMap.containsKey(mapKey);
}
/**
* list.get
* @param key
* @param listIndex
* @param
* @return
*/
public static T listGet(String key, int listIndex){
if(!isExists(key)){
return null;
}
if(listIndex < 0){
return null;
}
RList rList = redissonClient.getList(key);
if(rList.size()-1 < listIndex){
return null;
}
return rList.get(listIndex);
}
/**
* list.getAll
* @param key
* @param
* @return
*/
public static List listGetAll(String key){
RList rList = redissonClient.getList(key);
return rList.readAll();
}
/**
* list.add
* @param key
* @param addValue
* @param
*/
public static void listAdd(String key, T addValue){
RList rList = redissonClient.getList(key);
rList.add(addValue);
}
/**
* list.add
* @param key
* @param addList
* @param
*/
public static void listAddAll(String key, List addList){
RList rList = redissonClient.getList(key);
rList.addAll(addList);
}
/**
* list.set
* @param key
* @param listIndex
* @param setValue
* @param
*/
public static void listSet(String key, int listIndex, T setValue){
RList rList = redissonClient.getList(key);
if(rList.size()-1 < listIndex){
return;
}
rList.set(listIndex, setValue);
}
}