首页 > 数据库 >redisson 工具类

redisson 工具类

时间:2023-09-19 17:34:54浏览次数:37  
标签:redisson return String redissonClient key Override 工具 public

import com.alibaba.fastjson.JSON;
import com.juxiao.xchat.dao.room.dto.CallStatus;
import com.juxiao.xchat.manager.cache.redis.RedissonManager;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.client.protocol.ScoredEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RedissonManagerImpl implements RedissonManager {
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public RLock lock(String key, int waitTime, int lockTimeout) {
        RLock rLock = redissonClient.getLock(key);
        try {
            if (rLock.tryLock(waitTime, lockTimeout, TimeUnit.SECONDS)) {
                return rLock;
            }
        } catch (InterruptedException e) {
            log.error("[ Redisson ] tryLock err:", e);
        }
        return null;
    }

    @Override
    public RLock fairLock(String key, int waitTime, int lockTimeout) {
        RLock rLock = redissonClient.getFairLock(key);
        try {
            if (rLock.tryLock(waitTime, lockTimeout, TimeUnit.SECONDS)) {
                return rLock;
            }
        } catch (InterruptedException e) {
            log.error("[ Redisson ] tryLock err:", e);
        }
        return null;
    }

    @Override
    public long del(String... key) {
        RKeys rKeys = redissonClient.getKeys();
        return rKeys.delete(key);
    }

    @Override
    public boolean expire(String key, long time, TimeUnit timeUnit) {
        RKeys rKeys = redissonClient.getKeys();
        return rKeys.expire(key, time, timeUnit);
    }

    @Override
    public Iterable<String> keys(String key) {
        RKeys rKeys = redissonClient.getKeys();
        return rKeys.getKeysByPattern(key);
    }

    @Override
    public boolean exists(String key) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.isExists();
    }

    @Override
    public Long getExpire(String key) {
        RKeys rKeys = redissonClient.getKeys();
        return rKeys.remainTimeToLive(key);
    }

    @Override
    public long ttl(String key) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.remainTimeToLive();
    }

    @Override
    public String get(String key) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.get();
    }

    @Override
    public Map<String, String> mGet(String... key) {
        RBuckets rBuckets = redissonClient.getBuckets();
        return rBuckets.get(key);
    }

    @Override
    public void set(String key, String value) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        rBucket.set(value);
    }

    @Override
    public String getAndSet(String key, String value) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.getAndSet(value);
    }

    @Override
    public String getAndDelete(String key) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.getAndDelete();
    }

    @Override
    public void set(String key, String value, int time, TimeUnit timeUnit) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        rBucket.set(value, time, timeUnit);
    }

    @Override
    public void set(String key, String value, long time, TimeUnit timeUnit) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        rBucket.set(value, time, timeUnit);
    }

    @Override
    public boolean setnx(String key, String value) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.trySet(value);
    }

    @Override
    public boolean setnx(String key, String value, long time, TimeUnit timeUnit) {
        RBucket<String> rBucket = redissonClient.getBucket(key);
        return rBucket.trySet(value, time, timeUnit);
    }

    @Override
    public boolean msetnx(Map<String, String> map) {
        RBuckets rBuckets = redissonClient.getBuckets();
        return rBuckets.trySet(map);
    }

    @Override
    public boolean msetnx(String key, String otherKey, String value, String otherValue) {
        Map<String, String> map = new HashMap<>();
        map.put(key, value);
        map.put(otherKey, otherValue);
        return msetnx(map);
    }

    private final static String SCRIPT_1 =
            "local v1 = redis.call('SETNX',KEYS[1],ARGV[1]); " +
                    "local v2 = redis.call('SETNX',KEYS[2],ARGV[2]); " +
                    "return v1 + v2; ";

    @Override
    public Long setnx(String key, String otherKey, String value, String otherValue) {
        RScript rScript = redissonClient.getScript();
        List<Object> list = new ArrayList<>();
        list.add(key);
        list.add(otherKey);
        List<Object> params = new ArrayList(2);
        params.add(value);
        params.add(otherValue);
        return rScript.eval(RScript.Mode.READ_WRITE, SCRIPT_1, RScript.ReturnType.INTEGER, list, params.toArray());
    }

    private final static String SCRIPT_2 = "local val = redis.call('MSETNX',KEYS[1],ARGV[1],KEYS[2],ARGV[2]) " +
            "if val == 0 then " +
            "  return 0  " +
            "else " +
            "  redis.call('Expire', KEYS[1],ARGV[3]) " +
            "  redis.call('Expire', KEYS[2],ARGV[3]) " +
            "  return 1 " +
            "end";

    @Override
    public Long msetnx(String key, String otherKey, String value, String otherValue, String timeoutSeconds) {
        RScript rScript = redissonClient.getScript();
        List<Object> list = new ArrayList<>();
        list.add(key);
        list.add(otherKey);
        List<Object> params = new ArrayList(2);
        params.add(value);
        params.add(otherValue);
        params.add(timeoutSeconds);
        return rScript.eval(RScript.Mode.READ_WRITE, SCRIPT_2, RScript.ReturnType.INTEGER, list, params.toArray());
    }

    @Override
    public Long increment(String key, Long value) {
        RAtomicLong rAtomicLong = redissonClient.getAtomicLong(key);
        return rAtomicLong.addAndGet(value);
    }

    @Override
    public Double increment(String key, Double value) {
        RAtomicDouble rAtomicDouble = redissonClient.getAtomicDouble(key);
        return rAtomicDouble.addAndGet(value);
    }

    @Override
    public long incrByTime(String key, int timeout) {
        RAtomicLong rAtomicLong = redissonClient.getAtomicLong(key);
        long result = rAtomicLong.addAndGet(1);
        rAtomicLong.expire(timeout, TimeUnit.SECONDS);
        return result;
    }

    @Override
    public String hGet(String key, String field) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.get(field);
    }

    @Override
    public Map<String, String> hMGet(String key, Set<String> field) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.getAll(field);
    }

    @Override
    public Map<String, String> hGetAll(String key) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.readAllMap();
    }

    @Override
    public int hLen(String key) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.size();
    }

    @Override
    public String hSet(String key, String field, String value) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.put(field, value);
    }

    @Override
    public void hSetAll(String key, Map<String, String> value) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        rMap.putAll(value);
    }

    @Override
    public String hSet(String key, String field, String value, int time, TimeUnit timeUnit) {
        RMapCache<String, String> rMapCache = redissonClient.getMapCache(key);
        return rMapCache.put(field, value, time, timeUnit);
    }

    @Override
    public boolean hSetIfAbsent(String key, String field, String value) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.fastPutIfAbsent(field, value);
    }

    @Override
    public Integer hIncrement(String key, String field, Integer value) {
        RMap<String, Integer> rMap = redissonClient.getMap(key);
        return rMap.addAndGet(field, value);
    }

    @Override
    public Long hIncrement(String key, String field, Long value) {
        RMap<String, Long> rMap = redissonClient.getMap(key);
        return rMap.addAndGet(field, value);
    }

    @Override
    public Double hIncrement(String key, String field, Double value) {
        RMap<String, Double> rMap = redissonClient.getMap(key);
        return rMap.addAndGet(field, value);
    }

    @Override
    public String hDel(String key, String field) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.remove(field);
    }

    @Override
    public Long hDelKeys(String key, String[] fields) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.fastRemove(fields);
    }

    @Override
    public Set<String> hKeys(String key) {
        RMap<String, String> rMap = redissonClient.getMap(key);
        return rMap.keySet();
    }

    @Override
    public boolean pushAll(String key, List<String> values) {
        RList<String> rList = redissonClient.getList(key);
        return rList.addAll(values);
    }

    @Override
    public void leftPush(String key, String value) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        rDeque.addFirst(value);
    }

    @Override
    public String leftPop(String key) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.pollFirst();
    }

    @Override
    public List<String> lPop(String key, int size) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.pollFirst(size);
    }

    @Override
    public void rightPush(String key, String value) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        rDeque.addLast(value);
    }

    @Override
    public int rPush(String key, String[] value) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.addFirstIfExists(value);
    }

    @Override
    public String rightPop(String key) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.pollLast();
    }

    @Override
    public boolean lRemove(String key, Integer count, String value) {
        RList<String> rList = redissonClient.getList(key);
        return rList.remove(value, count);
    }

    @Override
    public List<String> lRange(String key, int start, int end) {
        RList<String> rList = redissonClient.getList(key);
        return rList.range(start, end);
    }

    @Override
    public int lSize(String key) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.size();
    }

    @Override
    public List<String> listAll(String key) {
        RDeque<String> rDeque = redissonClient.getDeque(key);
        return rDeque.readAll();
    }

    @Override
    public boolean zAdd(String key, String field, double score) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.add(score, field);
    }

    @Override
    public boolean zAdd(String key, double score, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.add(score, field);
    }

    @Override
    public int zAddAll(String key, Map<String, Double> map) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.addAll(map);
    }

    @Override
    public Double zIncrement(String key, String field, Double score) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.addScore(field, score);
    }

    @Override
    public Double zIncrement(String key, Double score, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.addScore(field, score);
    }

    @Override
    public Double zIncrement(String key, Long score, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.addScore(field, score);
    }

    private final static String ZINCRBY_IF_EXISTS = "local val = redis.call('ZRANK',KEYS[1],ARGV[1]) " +
            "if val == false or val == nil then " +
            "  return nil  " +
            "else " +
            "  return redis.call('ZINCRBY', KEYS[1],ARGV[2],ARGV[1]) " +
            "end";

    @Override
    public String zincrbyIfExists(String key, String member, double increment) {
        RScript rScript = redissonClient.getScript();
        List<Object> list = new ArrayList<>();
        list.add(key);
        List<Object> params = new ArrayList(2);
        params.add(member);
        params.add(increment);
        return rScript.eval(RScript.Mode.READ_WRITE, ZINCRBY_IF_EXISTS, RScript.ReturnType.VALUE, list, params.toArray());
    }

    @Override
    public Integer zRank(String key, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.rank(field);
    }

    @Override
    public Integer reverseZRank(String key, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.revRank(field);
    }

    @Override
    public int zCard(String key) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.size();
    }

    @Override
    public Double zScore(String key, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.getScore(field);
    }

    @Override
    public boolean zRemove(String key, String field) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.remove(field);
    }

    @Override
    public int zRemoveByScore(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.removeRangeByScore(min, true, max, true);
    }

    @Override
    public int removeRange(String key, int start, int end) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.removeRangeByRank(start, end);
    }

    @Override
    public int removeRangeByScore(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.removeRangeByScore(min, true, max, true);
    }

    @Override
    public boolean zRemoveAll(String key, List<String> fieldList) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.removeAll(fieldList);
    }

    @Override
    public Integer getZetSize(String key) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.size();
    }

    @Override
    public long zcount(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.count(min, true, max, true);
    }

    @Override
    public Collection<ScoredEntry<String>> zRange(String key, int start, int end) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.entryRange(start, end);
    }

    @Override
    public Collection<ScoredEntry<String>> reverseZRange(String key, int start, int end) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.entryRangeReversed(start, end);
    }

    @Override
    public Collection<ScoredEntry<String>> zRangeByScore(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.entryRange(min, true, max, true);
    }

    @Override
    public Collection<ScoredEntry<String>> reverseZRangeByScore(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.entryRangeReversed(min, true, max, true);
    }

    @Override
    public Collection<String> zrangeByScore(String key, double min, double max) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.valueRange(min, true, max, true);
    }

    @Override
    public Collection<String> zrevRangeByScore(String key, double max, double min) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.valueRangeReversed(min, true, max, true);
    }

    @Override
    public Collection<String> zrange(String key, int start, int end) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.valueRange(start, end);
    }

    @Override
    public Collection<String> zrevrange(String key, long start, long end) {
        RScoredSortedSet<String> rScoredSortedSet = redissonClient.getScoredSortedSet(key);
        return rScoredSortedSet.valueRangeReversed((int) start, (int) end);
    }

    @Override
    public boolean sExists(String key) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.isExists();
    }

    @Override
    public boolean sIsMember(String key, String field) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.contains(field);
    }

    @Override
    public boolean sAdd(String key, String field) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.add(field);
    }

    @Override
    public boolean sAddAll(String key, Collection<String> field) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.addAll(field);
    }

    @Override
    public boolean sRemove(String key, String field) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.remove(field);
    }

    @Override
    public Set<String> sPop(String key, int size) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.removeRandom(size);
    }

    @Override
    public int sCard(String key) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.size();
    }

    @Override
    public Set<String> sMembers(String key) {
        RSet<String> rSet = redissonClient.getSet(key);
        return rSet.readAll();
    }

    @Override
    public long sAddAndCard(String key, String field) {
        String script = "redis.call('sAdd', KEYS[1], ARGV[1]); " +
                "return redis.call('sCard', KEYS[1]); ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.INTEGER,
                Collections.singletonList(key), field);
    }

    @Override
    public boolean hSetIfExist(String key, String field, String value) {
        String script = "local n = redis.call('hGet', KEYS[1], ARGV[1]); " +
                "if n then " +
                "redis.call('hSet', KEYS[1], ARGV[1], ARGV[2]); " +
                "return true;" +
                "else " +
                "return false;" +
                "end; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.BOOLEAN,
                Collections.singletonList(key), field, value);
    }

    @Override
    public String luaKeepIncrement(String key, Long num) {
        if(num == null) {
            return null;
        }
        String script = "local last = redis.call('get', KEYS[1]); " +
                "if not last or tonumber(ARGV[1]) > tonumber(last) then " +
                "redis.call('set', KEYS[1], ARGV[1]); " +
                "end; " +
                "return last; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), num);
    }

    @Override
    public Long luaDoubleAddUp(String key, Double val, boolean end) {
        if(val == null) {
            return null;
        }
        String script = "local v = redis.call('incrbyfloat', KEYS[1], ARGV[1]); " +
                "local t1, t2 = math.modf(v);" +
                "if ARGV[2] == 'true' then " +
                "redis.call('del', KEYS[1]);" +
                "else " +
                "redis.call('set', KEYS[1], t2);" +
                "end; " +
                "return t1;";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), val, end);
    }

    @Override
    public String couponsOperator(String key, long now, boolean reduce, CallStatus.Coupons value) {
        String script = "while true do " +
                " local r = redis.call('lpop', KEYS[1]); " +
                " if not r then break; end; " +
                " local json = cjson.decode(r); " +
                " if json.expireTime > tonumber(ARGV[1]) then " +
                " if ARGV[2] == 'true' then return r; end; " +
                " redis.call('lpush', KEYS[1], r); break; " +
                " end; " +
                " end; " +
                " local v = ARGV[3]; if v ~= '' then redis.call('rpush', KEYS[1], v); end; " +
                " return v; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), now, reduce,
                value == null ? "" : JSON.toJSONString(value));
    }

    @Override
    public void fixSizeListAdd(String key, String value, int limit) {
        String script = "local t = redis.call('llen', KEYS[1]);" +
                "if (t >= tonumber(ARGV[2])) then " +
                "redis.call('rpop', KEYS[1]); end; " +
                "redis.call('lpush', KEYS[1], ARGV[1]); ";
        RScript rscript = redissonClient.getScript();
        rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.BOOLEAN, Collections.singletonList(key), value, limit);
    }

    /**
     * 数据量不大的情况下,首页也可以考虑这种方式获取排序的数据。
     * @param key
     * @param language
     * @param country
     * @return
     */
    @Override
    public String sortTable(String key, String language, String country) {
        String script = "local t = redis.call('hgetall', KEYS[1]);\n" +
                "local arr = {};\n" +
                "for i, v in pairs(t) do\n" +
                "    if i % 2 == 0 then\n" +
                "        local j = cjson.decode(v)\n" +
                "        if j.language == ARGV[1] then j.languageEquals = 1 else j.languageEquals = 0 end;\n" +
                "        if j.country == ARGV[2] then j.countryEquals = 1 else j.countryEquals = 0 end;\n" +
                "        table.insert(arr, j)\n" +
                "    end    \n" +
                "end \n" +
                "table.sort(arr, function (a, b) \n" +
                "   local ta =  a.languageEquals + a.countryEquals; \n" +
                "   local tb =  b.languageEquals + b.countryEquals; \n" +
                "   if ta > 0 then ta = 1 end; \n" +
                "   if tb > 0 then tb = 1 end; \n" +
                "   if ta - tb == 0 then \n" +
                "      return a.createTime < b.createTime \n" +
                "   else " +
                "      return ta > tb end;\n" +
                "end)\n" +
                "return cjson.encode(arr); \n";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), language, country);
    }

    @Override
    public String hSetIsNull(String key, String field, String value) {
        String script = "local t = redis.call('hget', KEYS[1], ARGV[1]);\n" +
                "if t then return t; else redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); return t; end;";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), field, value);
    }

    @Override
    public String setIsNull(String key, String value) {
        String script = "local t = redis.call('get', KEYS[1]);\n" +
                "if t then return t; else redis.call('set', KEYS[1], ARGV[1]); return t; end;";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), value);
    }

    @Override
    public String updateJson(String key, String field, long num, long limit) {
        String script = "local t = redis.call('get', KEYS[1]);\n" +
                "local j;\n" +
                "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" +
                "local n;\n" +
                "if j[ARGV[3]] then n = tonumber(j[ARGV[3]]); else n = 0 end;\n" +
                "local r = n + tonumber(ARGV[1]);\n" +
                "local limit = tonumber(ARGV[2]);\n" +
                "if r >= 0 and (limit < 0 or r <= limit) then j[ARGV[3]] = r; " +
                "redis.call('set', KEYS[1], cjson.encode(j)); end;\n" +
                "j[ARGV[3]] = r;" +
                "return cjson.encode(j);";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), num, limit, field);
    }

    @Override
    public String updateJson(String key, String field, String value) {
        String script = "local t = redis.call('get', KEYS[1]);\n" +
                "local j;\n" +
                "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" +
                "j[ARGV[1]] = ARGV[2]; " +
                "redis.call('set', KEYS[1], cjson.encode(j));\n" +
                "return cjson.encode(j);";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), field, value);
    }

    @Override
    public String hUpdateJson(String key, String uid, String field, long num, long limit) {
        String script = "local t = redis.call('hget', KEYS[1], ARGV[4]);\n" +
                "local j;\n" +
                "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" +
                "local n;\n" +
                "if j[ARGV[3]] then n = tonumber(j[ARGV[3]]); else n = 0 end;\n" +
                "local r = n + tonumber(ARGV[1]);\n" +
                "local limit = tonumber(ARGV[2]);\n" +
                "if r >= 0 and (limit < 0 or r <= limit) then j[ARGV[3]] = r; " +
                "redis.call('hset', KEYS[1], ARGV[4], cjson.encode(j)); end;\n" +
                "j[ARGV[3]] = r;" +
                "return cjson.encode(j);";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE,
                Collections.singletonList(key), num, limit, field, uid);
    }

    @Override
    public String mUpdateJson(String key, List<String> field, List<Number> num, List<Long> limit) {
        if(field == null || num == null || limit == null) {
            throw new RuntimeException("mUpdateJson param error, has null, field: " + field  + " num:" + num + " limit: " + limit);
        }
        field = field.stream().filter(Objects::nonNull).collect(Collectors.toList());
        num = num.stream().filter(Objects::nonNull).collect(Collectors.toList());
        limit = limit.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if(field.size() > 100) {
            throw new RuntimeException("mUpdateJson operator field length not > 100!");
        }
        if(field.size() != num.size() || field.size() != limit.size()) {
            throw new RuntimeException("mUpdateJson param size error, fieldSize: " + field.size()  + " numSize:"
                    + num.size() + " limitSize: " + limit.size());
        }

        String script = "local t = redis.call('get', KEYS[1]);\n" +
                "local j; local err = false;\n" +
                "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" +
                "local n;\n" +
                "for i = 1, #ARGV, 3 do \n" +
                "if j[ARGV[i]] then n = tonumber(j[ARGV[i]]); else n = 0 end;\n" +
                "local r = n + tonumber(ARGV[i + 1]);\n" +
                "local limit = tonumber(ARGV[i + 2]);\n" +
                "if (r >= 0 or r > n) and (limit < 0 or r <= limit) then j[ARGV[i]] = r;\n" +
                "else j[ARGV[i]] = r; err = true; j['error'] = 1; end;\n" +
                "end; \n" +
                "local er = cjson.encode(j);\n" +
                "if err then return er;\n" +
                "else redis.call('set', KEYS[1], er); return er end;";
        RScript rscript = redissonClient.getScript();
        int size = field.size();
        Object[] param = new Object[size * 3];
        for(int i = 0;i < size;i++) {
            param[3 * i] = field.get(i);
            param[3 * i + 1] = num.get(i);
            param[3 * i + 2] = limit.get(i);
        }

        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE, Collections.singletonList(key), param);
    }

    @Override
    public void listDel(String key, String value) {
        String script = "local t = redis.call('LRANGE', KEYS[1], 0, -1); " +
                "if t then " +
                "local len = 0; " +
                "for i=1, #t do " +
                "   if (t[i] == ARGV[1]) then table.remove(t, i); len = len - 1; end;" +
                "   len = len + 1; " +
                "end; " +
                "redis.call('del', KEYS[1]); " +
                "if len > 0 then redis.call('RPUSH', KEYS[1], unpack(t)); end;" +
                "end;";
        RScript rscript = redissonClient.getScript();
        rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.BOOLEAN, Collections.singletonList(key), value);
    }

    @Override
    public List<Long> sAddAndExpire(String key, String field, long time, TimeUnit timeUnit) {
        String script = "local t = redis.call('sadd', KEYS[1], ARGV[1]);" +
                "local time = redis.call('ttl', KEYS[1]);" +
                "if not time or tonumber(time) <= 0 then redis.call('EXPIRE', KEYS[1], ARGV[2]); end;" +
                "return {t, redis.call('scard', KEYS[1])}; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.MULTI, Collections.singletonList(key), field, timeUnit.toMillis(time)/1000);
    }

    @Override
    public String hSetAndExpire(String key, String field, String value, long time, TimeUnit timeUnit) {
        String script = "local t = redis.call('hget', KEYS[1], ARGV[1]);" +
                "redis.call('hset', KEYS[1], ARGV[1], ARGV[2]);" +
                "local time = redis.call('ttl', KEYS[1]);" +
                "if not time or tonumber(time) <= 0 then redis.call('EXPIRE', KEYS[1], ARGV[3]); end;" +
                "return t; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE, Collections.singletonList(key), field, value, timeUnit.toMillis(time)/1000);
    }

    @Override
    public boolean zAddAndExpire(String key, String field, double score, long time, TimeUnit timeUnit) {
        String script = "local t = redis.call('zadd', KEYS[1], ARGV[1], ARGV[2]);" +
                "local time = redis.call('ttl', KEYS[1]);" +
                "if not time or tonumber(time) <= 0 then redis.call('EXPIRE', KEYS[1], ARGV[3]); end;" +
                "return t; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.BOOLEAN, Collections.singletonList(key), field, score, timeUnit.toMillis(time)/1000);
    }

    @Override
    public Long incrementAndExpire(String key, Long value, long time, TimeUnit timeUnit) {
        String script = "local t = redis.call('INCRBY', KEYS[1], ARGV[1]);" +
                "local time = redis.call('ttl', KEYS[1]);" +
                "if not time or tonumber(time) <= 0 then redis.call('EXPIRE', KEYS[1], ARGV[2]); end;" +
                "return t; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.INTEGER, Collections.singletonList(key), value, timeUnit.toMillis(time)/1000);
    }

    @Override
    public Long hIncrementAndExpire(String key, String field, Long score, long time, TimeUnit timeUnit) {
        String script = "local t = redis.call('HINCRBY', KEYS[1], ARGV[1], ARGV[2]);" +
                "local time = redis.call('ttl', KEYS[1]);" +
                "if not time or tonumber(time) <= 0 then redis.call('EXPIRE', KEYS[1], ARGV[3]); end;" +
                "return t; ";
        RScript rscript = redissonClient.getScript();
        return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.INTEGER, Collections.singletonList(key), field, score, timeUnit.toMillis(time)/1000);
    }

}

 

标签:redisson,return,String,redissonClient,key,Override,工具,public
From: https://www.cnblogs.com/math-and-it/p/17715226.html

相关文章

  • CI/CD 工具和技术:释放 DevOps 的力量
    在快节奏的软件开发世界中,持续集成和持续部署(CI/CD)已成为DevOps服务中不可或缺的实践。CI/CD使团队能够更频繁、更高效、更高质量地交付软件更新。为了实现这些目标,开发人员依靠一系列尖端工具和技术来简化工作流程并自动化开发过程的各个阶段。在这篇博文中,我们将探讨有助于Dev......
  • 常用的敏捷开发工具
    1、Leangoo领歌Leangoo领歌Scrum中文网(scrum.cn)旗下的一款永久免费的专业敏捷研发管理工具,它覆盖了敏捷项目研发全流程,包括小型团队敏捷开发,规模化敏捷SAFe,ScrumofScrums大规模敏捷。能够支持多种场景,如:敏捷研发管理、敏捷项目管理、工作流管理、轻量级项目群管理、任务管理等......
  • Web 开发工具
    代码编辑器高效编写代码是Web开发的一个关键方面。这是一个可以帮助您的创新工具:代码GPT通过CodeGPT利用人工智能的力量。这款人工智能驱动的代码编辑器可以协助完成编码任务、生成代码片段,甚至提供对编程概念的见解。借助codeGPT,您将能够更快、更高效地进行编码。无论您......
  • 三个工具助你提高工作效率!
    有两种途径能快速提高工作效率,一个是科学的工作方法,一个是适合自己的效率工具。今天我给大家介绍三个提高工作效率的方法。目标管理:SMART原则时间管理:四象限法则成长管理:标杆管理一、目标管理:SMART原则目标管理不仅让员工工作更明确,而且还是我们未来考核的一个指标。SMART原则就是......
  • 【接口工具ApiPost】使用Mock服务前端不用依赖后端接口实现API解耦(16)---(强力推荐本章
    释义:Mock:模仿,仿造。可理解为虚拟环境模拟数据Mock服务:模拟服务器提供API访问服务Mock服务使用路径:接口下面和Header、Query、Body、认证…Mock服务,如下图:使用前准备环境选择必须是Mock环境非mock环境mock服务是不起作用的。环境设置如下:路径:在小眼睛左边默认情况下是有一个官......
  • 【API接口工具】postman-Workspaces工作空间 VS Scratch Pad草稿面板
    ScratchPad是一个可以在未连接到Postman服务器的情况下工作的空间。当您未登录或没有网络连接时,您仍然可以离线访问某些Postman功能,例如创建集合和请求,或发送请求。ScratchPad中的所有工作都存储在本地,不会与Postman在线同步。在便笺簿中工作后,您可以稍后在登录后将工作移......
  • 迅为RK3588开发板修改编译工具路径
    1因为此章节以rknn_yolov5_demo在RK3588Linux64位平台上运行为例,所以修改examples/rknn_yolov5_demo/build-XXX.sh的编译工具路径,如下图所示:2修改build-linux_RK3588.sh文件,将TOOL_CHAIN修改为gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu的解压路径并保存修......
  • 有许多注册表监视工具可用于监视和跟踪Windows操作系统中的注册表更改
    有许多注册表监视工具可用于监视和跟踪Windows操作系统中的注册表更改。以下是几个常用的注册表监视工具:ProcessMonitor:由Sysinternals开发的免费工具,能够实时监视和记录Windows系统上的文件系统、注册表和进程/线程活动。它提供了强大的过滤和搜索功能,以便轻松筛选所需的注册表......
  • gitlab-代码管理工具的离线安装部署
    gitlab-代码管理工具的离线安装部署最低安装条件:资源最低限度推荐CPU2核4核内存4GB8GB解决安装依赖yum-yinstallpolicycoreutilsopenssh-serveropenssh-clientspostfixpolicycoreutils-pythongitlab的rpm包下载开始rpm安装[root@localhost~......
  • RubyMine 2023:高效Ruby编码工具,适用于macOS和Windows
    RubyMine是JetBrains开发的一款为Ruby开发者量身定制的集成开发环境(IDE)。它为Ruby语言提供了全面的支持,包括代码编辑、调试、测试和集成版本控制系统等功能,帮助开发者更加高效地进行Ruby编程。→→↓↓载RubyMine2023mac+win版代码编辑与自动补全:RubyMine提供了强大的代码编......