redisson常用api

redisson提供了很多对象类型的api,下面介绍下一些常用的对象api。

RBucket

可操作任何对象的api,前提是要确定好泛型,方法比较少。大小限制为512Mb。

java 复制代码
RBucket<AnyObject> 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));

RMap

专门操作map的对象,实现了ConcurrentMap接口,并且put、set操作直接作用于redis。

java 复制代码
RMap<String, SomeObject> 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<SomeObject> putAsyncFuture = map.putAsync("321");
RFuture<Void> fastPutAsyncFuture = map.fastPutAsync("321");

map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");

RList

专门操作list的对象,实现了java.util.List, add、set等方法直接作用于redis。

java 复制代码
RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());

自定义工具类代码

java 复制代码
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工具类 <br>
 * @author: 小花卷的Dad <br>
 * @create: 2023/8/24 <br>
 */
@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<String> rBucket = redissonClient.getBucket(key);
        rBucket.set(value);
    }

    /**
     * 保存字符串
     * @param key
     * @param value
     * @param expire
     */
    public static void setStr(String key, String value, Long expire){
        RBucket<String> 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<String> rBucket = redissonClient.getBucket(key);
        return rBucket.get();
    }

    /**
     * 保存对象
     * @param key
     * @param value
     * @param <T>
     */
    public static <T> void setObject(String key, T value){
        RBucket<T> rBucket = redissonClient.getBucket(key);
        rBucket.set(value);
    }

    /**
     * 保存对象
     * @param key
     * @param value
     * @param expire
     * @param <T>
     */
    public static <T> void setObject(String key, T value, Long expire){
        RBucket<T> rBucket = redissonClient.getBucket(key);
        rBucket.set(value, Duration.ofMillis(expire));
    }

    /**
     * 查询对象
     * @param key
     * @return
     */
    public static <T> T getObject(String key){
        RBucket<T> rBucket = redissonClient.getBucket(key);
        return rBucket.get();
    }

    /**
     * map.get
     * @param key
     * @param mapKey
     * @param <T>
     * @return
     */
    public static <T> T mapGet(String key, String mapKey){
        if(!isExists(key)){
            return null;
        }
        Map<String, T> rMap = redissonClient.getMap(key);
        return rMap.get(mapKey);
    }

    /**
     * 查询map
     * @param key
     * @param <T>
     * @return
     */
    public static <T> Map<String, T> mapGetAll(String key){
        RMap<String, T> rMap = redissonClient.getMap(key);
        return rMap.readAllMap();
    }

    /**
     * map.put
     * @param key
     * @param mapKey
     * @param mapValue
     * @param <T>
     */
    public static <T> void mapPut(String key, String mapKey,T mapValue){
        RMap<String, T> rMap = redissonClient.getMap(key);
        rMap.put(mapKey, mapValue);
    }

    /**
     * map.putAll
     * @param key
     * @param map
     * @param <T>
     */
    public static <T> void mapPutAll(String key, Map<String, T> map){
        RMap<String, T> 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<String, Object> rMap = redissonClient.getMap(key);
        return rMap.containsKey(mapKey);
    }

    /**
     * list.get
     * @param key
     * @param listIndex
     * @param <T>
     * @return
     */
    public static <T> T listGet(String key, int listIndex){
        if(!isExists(key)){
            return null;
        }
        if(listIndex < 0){
            return null;
        }
        RList<T> rList = redissonClient.getList(key);
        if(rList.size()-1 < listIndex){
            return null;
        }
        return rList.get(listIndex);
    }

    /**
     * list.getAll
     * @param key
     * @param <T>
     * @return
     */
    public static <T> List<T> listGetAll(String key){
        RList<T> rList = redissonClient.getList(key);
        return rList.readAll();
    }

    /**
     * list.add
     * @param key
     * @param addValue
     * @param <T>
     */
    public static <T> void listAdd(String key, T addValue){
        RList<T> rList = redissonClient.getList(key);
        rList.add(addValue);
    }

    /**
     * list.add
     * @param key
     * @param addList
     * @param <T>
     */
    public static <T> void listAddAll(String key, List<T> addList){
        RList<T> rList = redissonClient.getList(key);
        rList.addAll(addList);
    }

    /**
     * list.set
     * @param key
     * @param listIndex
     * @param setValue
     * @param <T>
     */
    public static <T> void listSet(String key, int listIndex, T setValue){
        RList<T> rList = redissonClient.getList(key);
        if(rList.size()-1 < listIndex){
            return;
        }
        rList.set(listIndex, setValue);
    }
}
相关推荐
圆山猫5 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v
城管不管5 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
IT小白杨5 小时前
从环境制备到自动化工作流:多账号运营的工程化架构拆解
java·经验分享·自动化·安全架构·指纹浏览器
豆瓣鸡6 小时前
算法日记 - Day3
java·开发语言·算法
萧瑟余晖6 小时前
Java深入解析篇九之NIO详解
java·网络·nio
The Chosen One9856 小时前
高进度算法模板速记(待完善)
java·前端·算法
独行侠影a8 小时前
APScheduler+Redis 分布式定时任务:解决多实例任务重复执行
数据库·redis·分布式
极光代码工作室9 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
Leighteen9 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
名字还没想好☜10 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine