redis 使用redisson缓存api模板

 

 大家进入下面小程序体验一下:

 

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.redisson.api.RLock;
import org.redisson.api.listener.MessageListener;
import org.redisson.client.protocol.ScoredEntry;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Garcia
 * @CreateDate: 2021/04/10 17:45
 * @Description: what it is class?
 */
public interface ICacheService {

    /**
     * 设置缓存
     * - 默认缓存失效时间30m
     *
     * @param key
     * @param value
     */
    <V> void set(String key,V value);

    /**
     * 设置String类型缓存
     *
     * @param key
     * @param value
     * @param expireSeconds 缓存失效时间单位 秒
     */
    void set(String key,String value,int expireSeconds);

    /**
     * 设置String类型缓存
     *
     * @param key
     * @param value
     * @param expire 缓存失效时间
     * @param timeUnit 时间单位
     */
    void set(String key,String value,int expire,TimeUnit timeUnit);

    /**
     * 批量设置多个缓存
     *
     * @param temps
     * @param <V>
     */
    <V> void multiSet(Map<String,V> temps);

    /**
     * 设置缓存
     *
     * @param key
     * @param value
     * @param expireSeconds 缓存失效时间单位 秒
     */
    <V> void set(String key,V value,int expireSeconds);

    /**
     * 设置缓存失效时间
     *
     * @param key
     * @param duration 缓存失效时间,of
     * @return 是否成功
     */
    boolean expire(String key, Duration duration);

    /**
     * 获取缓存生存时间
     *
     * @param key
     * @return 是否成功
     */
    Long ttl(String key);

    /**
     * 获取缓存值
     *
     * @param key
     * @param <V>
     * @return
     */
    <V> V get(String key);

    /**
     * 以原子操作设置指定缓存值,并返回之前的缓存值
     *
     * @param key
     * @param value
     * @param <V>
     * @return
     */
    <V> V getAndSet(String key , V value);

    /**
     * 获取所有匹配 pattern 的缓存key 对应的缓存值
     * 此方法 谨慎使用
     *
     * @param pattern
     * @param <V>
     * @return
     */
    <V> Set<V> getAll(String pattern);

    /**
     * 是否存在缓存key
     *
     * @param key
     * @return
     */
    Boolean hasKey(String key);

    /**
     * 删除key
     *
     * @param key
     */
    void delete(String key);

    /**
     * 删除所有匹配 pattern 的缓存key
     *
     * @param pattern
     */
    void deleteAll(String pattern);

    /**
     * 获取缓存类型
     *
     * @param key
     * @return
     */
    String getType(String key);

    /**
     * 获取Map
     *
     * @param key
     * @param <K>
     * @param <V>
     * @return
     */
    <K,V> Map<K,V> getMap(String key);

    /**
     * 是否有Map Key
     *
     * @param key
     * @param entryKey
     * @param <K>
     * @return
     */
    <K> Boolean hasMapKey(String key, K entryKey);

    /**
     * 设置Map 参数
     * @param key
     * @param entryKey
     * @param entryValue
     * @param <K>
     * @param <V>
     */
    <K,V> void addMapEntry(String key,K entryKey ,V entryValue);

    /**
     * 设置Map 参数
     * @param key
     * @param entryKey
     * @param entryValue
     * @param expireSeconds 缓存失效时间单位 秒
     * @param <K>
     * @param <V>
     */
    <K,V> void addMapEntry(String key,K entryKey ,V entryValue,int expireSeconds);

    /**
     * 添加整个Map
     * @param key
     * @param map
     * @param <K>
     * @param <V>
     */
    <K,V> void addMap(String key,Map<K,V> map);

    /**
     * 获取 map 值
     * @param key
     * @param entryKey
     * @param <K>
     * @param <V>
     * @return
     */
    <K,V> V getMapEntry(String key,K entryKey);

    /**
     * 根据KEY集合获取Map数据
     * @param key
     * @param entryKey
     * @param <K>
     * @param <V>
     * @return
     */
    <K, V> Map<K,V> getMapEntryAll(String key, Set<K> entryKey);

    /**
     * 删除Map值
     * @param key
     * @param entryKey
     * @param <K>
     */
    <K> void delMapEntry(String key,K entryKey);

    /**
     * 获取Set 集合size
     *
     * @param key
     * @return
     */
    Integer getSetSize(String key);

    /**
     * 添加Set 元素
     *
     * @param key
     * @param element
     * @param <V>
     */
    <V> void addSetElement(String key, V element);

    /**
     *
     * @param key
     * @param element
     * @param expireSeconds 缓存失效时间单位 秒
     * @param <V>
     */
    <V> void addSetElement(String key, V element,int expireSeconds);

    /**
     * 添加Set
     *
     * @param key
     * @param elements
     * @param <V>
     */
    <V> void addSet(String key, Set<V> elements);

    /**
     * 添加Set
     *
     * @param key
     * @param elements
     * @param <V>
     */
    <V> void addSet(String key, Set<V> elements,int expireSeconds);

    /**
     * 获取Set 集合
     *
     * @param key
     * @param <V>
     * @return
     */
    <V> Set<V> getSet(String key);

    /**
     * 是否有Set元素
     *
     * @param key
     * @param element
     * @param <V>
     * @return
     */
    <V> boolean hasSetElement(String key,V element);

    /**
     * 删除Set 元素
     * @param key
     * @param element
     * @param <V>
     * @return
     */
    <V> boolean delSetElement(String key,V element);

    /**
     * 获取List 集合size
     *
     * @param key
     * @return
     */
    Integer getListSize(String key);

    <V> void addListElement(String key, V element);

    <V> void addListElement(String key, V element,int expireSeconds);

    /**
     * 添加List
     *
     * @param key
     * @param elements
     * @param <V>
     */
    <V> void addList(String key, List<V> elements);

    /**
     * 添加List
     *
     * @param key
     * @param elements
     * @param <V>
     */
    <V> void addList(String key, List<V> elements,int expireSeconds);

    /**
     * 获取List 集合
     *
     * @param key
     * @param <V>
     * @return
     */
    <V> List<V> getList(String key);

    /**
     * 分布式锁-获取锁
     * @param key
     * @return
     */
    RLock fairLock(String key);

    /**
     * 计数器+
     *
     * @param key
     * @param num 从几开始
     * @param duration
     * @return
     */
    long incrementAndGet(String key , int num ,Duration duration);

    /**
     * 计算器+
     *
     * @param key
     * @param duration
     * @return
     */
    long incrementAndGet(String key ,Duration duration);

    /**
     * 计数器+
     *
     * @param key
     * @return
     */
    long incrementAndGet(String key);

    /**
     * 获取计数值
     * @param key
     * @return
     */
    long incrementGet(String key);


    /**
     * 计数器 -
     *
     * @param key
     * @param num 从几开始
     * @param duration
     * @return
     */
    long decrementAndGet(String key , int num ,Duration duration);

    /**
     * 计算器-
     *
     * @param key
     * @param duration
     * @return
     */
    long decrementAndGet(String key ,Duration duration);

    /**
     * 计数器-
     *
     * @param key
     * @return
     */
    long decrementAndGet(String key);

    /**
     * 获取计数值
     * @param key
     * @return
     */
    long decrementGet(String key);

    /**
     * 获取分数排序集合大小
     * 默认从低分到高分排序
     * @param key
     * @return
     */
    Integer getScoredSortedSetSize(String key);

    /**
     * 分数排序集合添加元素
     * 默认从低分到高分排序
     * @param key
     * @param element
     * @param <V>
     */
    <V> void addScoredSortedSetElement(String key, V element);

    /**
     * 分数排序集合添加元素
     * 默认从低分到高分排序
     * 指定分数
     * @param key
     * @param element
     * @param v
     * @param <V>
     */
    <V> void addScoredSortedSetElement(String key, V element,double v);

    /**
     * 分数排序集合添加元素
     * 默认从低分到高分排序
     * @param key
     * @param element
     * @param expireSeconds
     * @param <V>
     */
    <V> void addScoredSortedSetElement(String key, V element, int expireSeconds);

    /**
     * 分数排序集合添加元素
     * 默认从低分到高分排序
     * 指定分数
     * @param key
     * @param element
     * @param v
     * @param <V>
     */
    <V> void addScoredSortedSetElement(String key, V element,double v, int expireSeconds);

    /**
     * 获取分数排序集合
     * 以相反的顺序按排名范围返回值
     * @param key
     * @param startIndex
     * @param endIndex
     * @param <V>
     * @return
     */
    <V> Collection<V> getScoredSortedSetValueRangeReversed(String key,int startIndex, int endIndex);

    /**
     * 获取分数排序集合
     * 顺序按排名范围返回值
     * @param key
     * @param startIndex
     * @param endIndex
     * @param <V>
     * @return
     */
    <V> Collection<V> getScoredSortedSetValueRange(String key,int startIndex, int endIndex);

    /**
     * 获取分数排序集合
     * 以相反的顺序按排名范围返回条目(值及其分数)
     * @param key
     * @param startIndex
     * @param endIndex
     * @param <V>
     * @return
     */
    <V> Collection<ScoredEntry<V>> getScoredSortedSetEntryRangeReversed(String key,int startIndex, int endIndex);

    /**
     * 获取分数排序集合所有值
     * @param key
     * @param <V>
     * @return
     */
    <V> Collection<V> getScoredSortedSet(String key);

    /**
     * 是否存在该元素
     * @param key
     * @param element
     * @param <V>
     * @return
     */
    <V> boolean hasScoredSortedSetElement(String key, V element);

    /**
     * 删除指定元素
     * @param key
     * @param element
     * @param <V>
     * @return
     */
    <V> boolean delScoredSortedSetElement(String key, V element);

    /**
     * 是否存在分页元素
     * @param key
     * @param elementKey
     * @param <V>
     * @return
     */
    <V> boolean hasPageElement(String key, V elementKey);

    /**
     * 添加分页元素
     * 存在则覆盖
     * @param key
     * @param entryKey
     * @param element
     * @param <V>
     */
    <V> void addPageElement(String key,V entryKey,V element);

    /**
     * 添加分页元素。并指定分数
     * 存在则覆盖
     * @param key
     * @param entryKey
     * @param element
     * @param <V>
     * @param v
     */
    <V> void addPageElement(String key,V entryKey,V element,double v);

    /**
     * 删除分页元素
     * @param key
     * @param entryKey
     */
    <V> void delPageElement(String key,V entryKey);

    /**
     * 获取分页数据,按从高分到低分排序
     * 页码从1开始
     * @param key
     * @param pageCurrent
     * @param pageSize
     * @param <V>
     * @return
     */
    <V> IPage<V> getPageElementDesc(String key,Integer pageCurrent,Integer pageSize);

    /**
     * 获取分页数据,按从低分到高分排序
     * 页码从1开始
     * @param key
     * @param pageCurrent
     * @param pageSize
     * @param <V>
     * @return
     */
    <V> IPage<V> getPageElement(String key,Integer pageCurrent,Integer pageSize);

    /**
     * 获取 map 值
     * @param key
     * @param entryKey
     * @param <K>
     * @param <V>
     * @return
     */
    <K,V> V getPageElement(String key,K entryKey);

    /**
     * 发布消息
     * @param key
     * @param msg
     * @param <V>
     */
    <V> void publish(String key,V msg);

    /**
     * 监听消息
     * @param key
     * @param clazz
     * @param messageListener
     * @param <V>
     */
    <V> void addListener(String key,Class<V> clazz, MessageListener<V> messageListener);

    /**
     * 添加延迟队列
     * 应用场景:订单未支付到期自动关闭
     * @param queueName
     * @param value
     * @param expire
     * @param timeUnit
     * @param <V>
     */
    <V> void addDelayQueue(String queueName,V value, int expire, TimeUnit timeUnit);

    /**
     * 添加延迟队列
     * 应用场景:订单未支付到期自动关闭
     * @param queueName
     * @param value
     * @param expire
     * @param <V>
     */
    <V> void addDelayQueue(String queueName,V value, int expire);

    /**
     * 获取延迟队列
     * @param queueName
     * @return
     * @param <T>
     */
    <T> T getDelayQueue(String queueName);

    /**
     * 延迟队列是否包含值
     * @param queueName
     * @param value
     * @return
     * @param <V>
     */
    <V> Boolean delayQueueContainValue(String queueName,V value);

    /**
     * 延迟队列删除值
     * @param queueName
     * @param value
     * @return
     * @param <V>
     */
    <V> Boolean delayQueueRemoveValue(String queueName,V value);

}

 Impl:

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuing.awaken.commons.cache.ICacheService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.api.listener.MessageListener;
import org.redisson.client.protocol.ScoredEntry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Garcia
 * @CreateDate: 2023/05/13 17:46
 * @Description: what it is class?
 */
@Slf4j
@Service
public class CacheServiceImpl implements ICacheService {

    @Resource
    private RedissonClient redisson;

    @Value("${spring.redis.app-code}")
    private String appCode;

    @Value("${spring.profiles.active}")
    private String profile;

    /**
     * 指定key位置
     * @param key
     * @return
     */
    private String wrap(String key){
        StringBuilder sb = new StringBuilder(appCode).append(":").append(profile).append(":").append(key);
        if (!key.endsWith(":")){
            sb.append(":");
        }
        return sb.toString();
    }

    private String wrapEntry(String key){
        StringBuilder sb = new StringBuilder(appCode).append(":").append(profile).append(":").append(key);
        if (!key.endsWith(":")){
            sb.append(":").append("entry:");
        }else {
            sb.append("entry:");
        }
        return sb.toString();
    }

    @Override
    public <V> void set(String key, V value) {
        RBucket<V> bucket = redisson.getBucket(wrap(key));
        bucket.set(value);
    }

    @Override
    public void set(String key, String value, int expireSeconds) {
        RBucket<String> bucket = redisson.getBucket(wrap(key));
        bucket.set(value,expireSeconds,TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, String value, int expire, TimeUnit timeUnit) {
        RBucket<String> bucket = redisson.getBucket(wrap(key));
        bucket.set(value,expire,timeUnit);
    }

    @Override
    public <V> void multiSet(Map<String, V> temps) {
        redisson.getBuckets().set(temps);
    }

    @Override
    public <V> void set(String key, V value, int expireSeconds) {
        RBucket<V> bucket = redisson.getBucket(wrap(key));
        bucket.set(value,expireSeconds,TimeUnit.SECONDS);
    }

    @Override
    public boolean expire(String key, Duration duration) {
        try {
            return redisson.getBucket(wrap(key)).expire(duration);
        }catch (Exception e){
            log.error("Method#expire failed",e);
            return false;
        }
    }

    @Override
    public Long ttl(String key) {
        try {
            return redisson.getBucket(wrap(key)).remainTimeToLive();
        }catch (Exception e){
            log.error("Method#ttl failed",e);
            return 0L;
        }
    }

    @Override
    public <V> V get(String key) {
        try {
            RBucket<V> bucket = redisson.getBucket(wrap(key));
            return bucket.get();
        }catch (Exception e){
            log.error("Method#get failed",e);
            return null;
        }
    }

    @Override
    public <V> V getAndSet(String key, V value) {
        try {
            RBucket<V> bucket = redisson.getBucket(wrap(key));
            return bucket.getAndSet(value);
        }catch (Exception e){
            log.error("Method#getAndSet failed",e);
            return null;
        }
    }

    @Override
    public <V> Set<V> getAll(String pattern) {
        try {
            Set<V> set = new HashSet<>();
            Iterable<String> keys = redisson.getKeys().getKeysByPattern(pattern);
            for (Iterator<String> iterator = keys.iterator();iterator.hasNext();){
                String key = iterator.next();
                RBucket<V> bucket = redisson.getBucket(wrap(key));
                set.add(bucket.get());
            }
            return set;
        }catch (Exception e){
            log.error("Method#getAll failed",e);
            return null;
        }
    }

    @Override
    public Boolean hasKey(String key) {
        try {
            return redisson.getBucket(wrap(key)).isExists();
        }catch (Exception e){
            log.error("Method#hasKey failed",e);
            return null;
        }
    }

    @Override
    public void delete(String key) {
        redisson.getKeys().delete(wrap(key));
    }

    @Override
    public void deleteAll(String pattern) {
        redisson.getKeys().deleteByPattern(pattern);
    }

    @Override
    public String getType(String key) {
        try {
            RType type = redisson.getKeys().getType(wrap(key));
            if (type==null){
                return null;
            }
            return type.getClass().getName();
        }catch (Exception e){
            log.error("Method#getType failed",e);
            return null;
        }
    }

    @Override
    public <K, V> Map<K, V> getMap(String key) {
        try {
            return (RMap<K, V>)redisson.getMap(wrap(key)).readAllMap();
        }catch (Exception e){
            log.error("Method#getMap failed",e);
            return Collections.emptyMap();
        }
    }

    @Override
    public <K> Boolean hasMapKey(String key, K entryKey) {
        try {
            return redisson.getMap(wrap(key)).containsKey(entryKey);
        }catch (Exception e){
            log.error("Method#getMap failed",e);
            return false;
        }
    }

    @Override
    public <K, V> void addMapEntry(String key, K entryKey, V entryValue) {
        redisson.getMap(wrap(key)).put(entryKey,entryValue);
    }

    @Override
    public <K, V> void addMapEntry(String key, K entryKey, V entryValue, int expireSeconds) {
        redisson.getMap(wrap(key)).put(entryKey,entryValue);
        expire(key,Duration.ofSeconds(expireSeconds));
    }

    @Override
    public <K, V> void addMap(String key, Map<K, V> map) {
        redisson.getMap(wrap(key)).putAll(map);
    }

    @Override
    public <K, V> V getMapEntry(String key, K entryKey) {
        try {
            RMap<K, V> rMap = redisson.getMap(wrap(key));
            return rMap.get(entryKey);
        }catch (Exception e){
            log.error("Method#getMapEntry failed",e);
            return null;
        }
    }

    @Override
    public <K, V> Map<K,V> getMapEntryAll(String key, Set<K> entryKey) {
        try {
            RMap<K, V> rMap = redisson.getMap(wrap(key));
            return rMap.getAll(entryKey);
        }catch (Exception e){
            log.error("Method#getMapEntryAll failed",e);
            return null;
        }
    }

    @Override
    public <K> void delMapEntry(String key, K entryKey) {
        try {
            redisson.getMap(wrap(key)).remove(entryKey);
        }catch (Exception e){
            log.error("Method#delMapEntry failed",e);
        }
    }

    @Override
    public Integer getSetSize(String key) {
        return redisson.getSet(wrap(key)).size();
    }

    @Override
    public <V> void addSetElement(String key, V element) {
        redisson.getSet(wrap(key)).add(element);
    }

    @Override
    public <V> void addSetElement(String key, V element, int expireSeconds) {
        redisson.getSet(wrap(key)).add(element);
        expire(key,Duration.ofSeconds(expireSeconds));
    }

    @Override
    public <V> void addSet(String key, Set<V> elements) {
        redisson.getSet(wrap(key)).addAll(elements);
    }

    @Override
    public <V> void addSet(String key, Set<V> elements, int expireSeconds) {
        redisson.getSet(wrap(key)).addAll(elements);
        expire(key,Duration.ofSeconds(expireSeconds));
    }

    @Override
    public <V> Set<V> getSet(String key) {
        try {
            return (Set<V>)redisson.getSet(wrap(key)).readAll();
        }catch (Exception e){
            log.error("Method#getSet failed",e);
            return Collections.emptySet();
        }
    }

    @Override
    public <V> boolean hasSetElement(String key, V element) {
        try {
            return redisson.getSet(wrap(key)).contains(element);
        }catch (Exception e){
            log.error("Method#hasSetElement failed",e);
            return false;
        }
    }

    @Override
    public <V> boolean delSetElement(String key, V element) {
        return redisson.getSet(wrap(key)).remove(element);
    }

    @Override
    public Integer getListSize(String key) {
        try {
            return redisson.getList(wrap(key)).size();
        }catch (Exception e){
            log.error("Method#getListSize failed",e);
            return 0;
        }
    }

    @Override
    public <V> void addListElement(String key, V element) {
        redisson.getList(wrap(key)).add(element);
    }

    @Override
    public <V> void addListElement(String key, V element, int expireSeconds) {
        redisson.getList(wrap(key)).add(element);
        expire(key,Duration.ofSeconds(expireSeconds));
    }

    @Override
    public <V> void addList(String key, List<V> elements) {
        redisson.getList(wrap(key)).addAll(elements);
    }

    @Override
    public <V> void addList(String key, List<V> elements, int expireSeconds) {
        redisson.getList(wrap(key)).addAll(elements);
        expire(key,Duration.ofSeconds(expireSeconds));
    }

    @Override
    public <V> List<V> getList(String key) {
        try {
            return (List<V>)redisson.getList(wrap(key)).readAll();
        }catch (Exception e){
            log.error("Method#getList failed",e);
            return Collections.emptyList();
        }
    }

    @Override
    public RLock fairLock(String key) {
        try {
            return redisson.getFairLock(wrap(key));
        }catch (Exception e){
            log.error("Method#lock failed",e);
        }
        return null;
    }

    @Override
    public long incrementAndGet(String key, int num ,Duration duration) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            atomicLong.set(num);
            atomicLong.expire(duration);
            return atomicLong.incrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long incrementAndGet(String key,Duration duration) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            atomicLong.expire(duration);
            return atomicLong.incrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long incrementAndGet(String key) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            return atomicLong.incrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long incrementGet(String key) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            return atomicLong.get();
        }catch (Exception e){
            log.error("Method#incrementGet failed",e);
        }
        return 0;
    }

    @Override
    public long decrementAndGet(String key, int num, Duration duration) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            atomicLong.set(num);
            atomicLong.expire(duration);
            return atomicLong.decrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long decrementAndGet(String key, Duration duration) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            atomicLong.expire(duration);
            return atomicLong.decrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long decrementAndGet(String key) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            return atomicLong.decrementAndGet();
        }catch (Exception e){
            log.error("Method#incrementAndGet failed",e);
        }
        return 0;
    }

    @Override
    public long decrementGet(String key) {
        try {
            RAtomicLong atomicLong = redisson.getAtomicLong(wrap(key));
            return atomicLong.get();
        }catch (Exception e){
            log.error("Method#incrementGet failed",e);
        }
        return 0;
    }

    @Override
    public Integer getScoredSortedSetSize(String key) {
        RScoredSortedSet<Object> sortedSet = redisson.getScoredSortedSet(wrap(key));
        return sortedSet.size();
    }

    @Override
    public <V> void addScoredSortedSetElement(String key, V element) {
        redisson.getScoredSortedSet(wrap(key)).add(0,element);
    }

    @Override
    public <V> void addScoredSortedSetElement(String key, V element,double v) {
        redisson.getScoredSortedSet(wrap(key)).add(v,element);
    }

    @Override
    public <V> void addScoredSortedSetElement(String key, V element, int expireSeconds) {
        redisson.getScoredSortedSet(wrap(key)).add(0,element);
    }

    @Override
    public <V> void addScoredSortedSetElement(String key, V element,double v, int expireSeconds) {
        redisson.getScoredSortedSet(wrap(key)).add(v,element);
    }

    @Override
    public <V> Collection<V> getScoredSortedSetValueRangeReversed(String key,int startIndex, int endIndex) {
        try {
            RScoredSortedSet<V> rSet = redisson.getScoredSortedSet(wrap(key));
            return rSet.valueRangeReversed(startIndex,endIndex);
        }catch (Exception e){
            log.error("Method#getScoredSortedSetValueRangeReversed failed",e);
            return Collections.emptySet();
        }
    }

    @Override
    public <V> Collection<V> getScoredSortedSetValueRange(String key,int startIndex, int endIndex) {
        try {
            RScoredSortedSet<V> rSet = redisson.getScoredSortedSet(wrap(key));
            return rSet.valueRange(startIndex,endIndex);
        }catch (Exception e){
            log.error("Method#getScoredSortedSetValueRangeReversed failed",e);
            return Collections.emptySet();
        }
    }

    @Override
    public <V> Collection<ScoredEntry<V>> getScoredSortedSetEntryRangeReversed(String key,int startIndex, int endIndex) {
        try {
            RScoredSortedSet<V> rSet = redisson.getScoredSortedSet(wrap(key));
            return rSet.entryRangeReversed(startIndex,endIndex);
        }catch (Exception e){
            log.error("Method#getScoredSortedSetValueRangeReversed failed",e);
            return Collections.emptySet();
        }
    }

    @Override
    public <V> Collection<V> getScoredSortedSet(String key) {
        try {
            RScoredSortedSet<V> rSet = redisson.getScoredSortedSet(wrap(key));
            return rSet.readAll();
        }catch (Exception e){
            log.error("Method#getScoredSortedSet failed",e);
            return Collections.emptySet();
        }
    }

    @Override
    public <V> boolean hasScoredSortedSetElement(String key, V element) {
        try {
            return redisson.getSet(wrap(key)).contains(element);
        }catch (Exception e){
            log.error("Method#hasSetElement failed",e);
            return false;
        }
    }

    @Override
    public <V> boolean delScoredSortedSetElement(String key, V element) {
        return redisson.getSet(wrap(key)).remove(element);
    }

    @Override
    public <V> boolean hasPageElement(String key, V elementKey) {
        boolean b = this.hasScoredSortedSetElement(key,elementKey);
        if (!b){
            return false;
        }
        return this.hasMapKey(wrapEntry(key),elementKey);
    }

    @Override
    public <V> void addPageElement(String key,V entryKey,V element){
        this.addScoredSortedSetElement(key,entryKey);
        this.addMapEntry(wrapEntry(key),entryKey,element);
    }

    @Override
    public <V> void addPageElement(String key,V entryKey,V element,double v){
        this.addScoredSortedSetElement(key,entryKey,v);
        this.addMapEntry(wrapEntry(key),entryKey,element);
    }

    @Override
    public <V> void delPageElement(String key,V entryKey){
        this.delScoredSortedSetElement(key,entryKey);
        this.delMapEntry(wrapEntry(key),entryKey);
    }

    @Override
    public <V> IPage<V> getPageElementDesc(String key,Integer pageCurrent,Integer pageSize){
        IPage<V> page = new Page<>(pageCurrent,pageSize);
        //总数
        Integer total = this.getScoredSortedSetSize(key);
        if (total!=null&&total>0){
            page.setTotal(total);
            if (page.getPages()<pageCurrent){
                //当前页码大于页码数直接返回空
                return page;
            }
            int startIndex = Integer.parseInt(String.valueOf(page.offset()));
            int endIndex = Integer.parseInt(String.valueOf(page.offset()))+pageSize-1;
            //分页条目
            Collection<Long> list = this.getScoredSortedSetValueRangeReversed(key,startIndex,endIndex);
            return getPage(key,page,list);
        }
        return page;
    }

    @Override
    public <V> IPage<V> getPageElement(String key,Integer pageCurrent,Integer pageSize){
        IPage<V> page = new Page<>(pageCurrent,pageSize);
        //总数
        Integer total = this.getScoredSortedSetSize(key);
        if (total!=null&&total>0){
            page.setTotal(total);
            if (page.getPages()<pageCurrent){
                //当前页码大于页码数直接返回空
                return page;
            }
            int startIndex = Integer.parseInt(String.valueOf(page.offset()));
            int endIndex = Integer.parseInt(String.valueOf(page.offset()))+pageSize-1;
            //分页条目
            Collection<Long> list = this.getScoredSortedSetValueRange(key,startIndex,endIndex);
            return getPage(key,page,list);
        }
        return page;
    }

    @Override
    public <K, V> V getPageElement(String key, K entryKey) {
        return this.getMapEntry(wrapEntry(key),entryKey);
    }

    @Override
    public <V> void publish(String key, V msg){
        try {
            redisson.getTopic(key).publish(msg);
        }catch (Exception e){
            log.error("Method#publish failed",e);
        }
    }

    @Override
    public <V> void addListener(String key, Class<V> clazz, MessageListener <V> messageListener){
        try {
            redisson.getTopic(key).addListener(clazz,messageListener);
        }catch (Exception e){
            log.error("Method#addListener failed",e);
        }
    }

    @Override
    public <V> void addDelayQueue(String queueName,V value, int expire, TimeUnit timeUnit){
        try {
            RBlockingDeque<V> blockingDeque = redisson.getBlockingDeque(queueName);
            RDelayedQueue<V> delayedQueue = redisson.getDelayedQueue(blockingDeque);
            delayedQueue.offer(value, expire, timeUnit);
        }catch (Exception e){
            log.error("Method#addDelayQueue failed",e);
        }
    }

    @Override
    public <V> void addDelayQueue(String queueName,V value, int expire){
        addDelayQueue(queueName,value,expire,TimeUnit.SECONDS);
    }

    @Override
    public <T> T getDelayQueue(String queueName){
        try {
            RBlockingDeque<T> blockingDeque = redisson.getBlockingDeque(queueName);
            //避免消息伪丢失(应用重启未消费),官网推荐
            redisson.getDelayedQueue(blockingDeque);
            return blockingDeque.take();
        }catch (Exception e){
            log.error("Method#getDelayQueue failed",e);
        }
        return null;
    }

    @Override
    public <V> Boolean delayQueueContainValue(String queueName,V value){
        try {
            RBlockingDeque<Object> blockingDeque = redisson.getBlockingDeque(queueName);
            RDelayedQueue<Object> delayedQueue = redisson.getDelayedQueue(blockingDeque);
            return delayedQueue.contains(value);
        }catch (Exception e){
            log.error("Method#delayQueueContainValue failed",e);
        }
        return Boolean.FALSE;
    }

    @Override
    public <V> Boolean delayQueueRemoveValue(String queueName,V value){
        try {
            RBlockingDeque<Object> blockingDeque = redisson.getBlockingDeque(queueName);
            RDelayedQueue<Object> delayedQueue = redisson.getDelayedQueue(blockingDeque);
            if (!delayedQueue.contains(value)){
                return Boolean.FALSE;
            }
            return delayedQueue.remove(value);
        }catch (Exception e){
            log.error("Method#delayQueueRemoveValue failed",e);
        }
        return Boolean.FALSE;
    }

    private <V> IPage<V> getPage(String key,IPage<V> page,Collection<Long> list){
        if (!CollectionUtils.isEmpty(list)){
            Set<Long> set = new HashSet<>(list);
            Map<Long, V> masterMap = this.getMapEntryAll(wrapEntry(key),set);
            masterMap.forEach((id, master) -> page.getRecords().add(master));
            return page;
        }
        return page;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qensq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值