RedisService.java 1.5 KB
package com.viontech.fanxing.commons.service;

import com.viontech.fanxing.commons.constant.RedisKeys;
import org.redisson.api.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/7/26
 */

@Service
public class RedisService {

    @Resource
    private RedissonClient redissonClient;


    public RMap<String, String> getTaskVaServerMap() {
        return redissonClient.getMap(RedisKeys.SCHEDULING_TASK_VASERVER_MAP);
    }

    public RScoredSortedSet<String> getToBeExecutedTaskUnidSet() {
        return redissonClient.getScoredSortedSet(RedisKeys.SCHEDULING_TO_BE_EXECUTED_TASK_UNID_SET);
    }

    public RScoredSortedSet<String> getToBeTerminatedTaskUnidSet() {
        return redissonClient.getScoredSortedSet(RedisKeys.SCHEDULING_TO_BE_TERMINATED_TASK_UNID_SET);
    }


    public <T> RBucket<T> getValue(String key) {
        return redissonClient.getBucket(key);
    }

    public RLock getLockMust(String lockName) {
        RLock lock = redissonClient.getLock(lockName);
        lock.lock();
        return lock;
    }

    public RLock tryLock(String lockName) {
        RLock lock = redissonClient.getLock(lockName);
        boolean success = false;
        try {
            success = lock.tryLock(30, TimeUnit.SECONDS);
        } catch (InterruptedException ignore) {
        }
        return success ? lock : null;
    }

    public RedissonClient getClient() {
        return redissonClient;
    }
}