码迷,mamicode.com
首页 > 其他好文 > 详细

Redission 中 RPermitExpirableSemaphore 用法

时间:2018-09-05 23:56:15      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:.com   tps   lease   for   perm   false   final   ini   semaphore   

关于该类,https://github.com/redisson/redisson 上的解释如下

基于Redis的Java 分布式Semaphore对象,每个获取的许可证具有租用时间参数支持。每个许可证由自己的id标识,并且只能使用其id发布。

8.7. PermitExpirableSemaphore
Redis based distributed Semaphore object for Java with lease time parameter support for each acquired permit. Each permit identified by own id and could be released only using its id.

RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
String permitId = semaphore.acquire();
// acquire permit with lease time = 2 seconds
String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
// ...
semaphore.release(permitId);

 

private final RPermitExpirableSemaphore initSemaphore(Config config) {
        RPermitExpirableSemaphore semaphore = redissonClient.getPermitExpirableSemaphore(config.getId());
        semaphore.delete();
        semaphore = redissonClient.getPermitExpirableSemaphore(config.getId());
        semaphore.trySetPermits(config.getLimit());
        return semaphore;
    }

 

lua脚本很简单,如果keys1不存在,给keys1赋值,发布,并且返回1;如果keys1存在,直接返回0

   @Override
    public RFuture<Boolean> trySetPermitsAsync(int permits) {
        return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
                "local value = redis.call(‘get‘, KEYS[1]); " +
                "if (value == false or value == 0) then "
                    + "redis.call(‘set‘, KEYS[1], ARGV[1]); "
                    + "redis.call(‘publish‘, KEYS[2], ARGV[1]); "
                    + "return 1;"
                + "end;"
                + "return 0;",
                Arrays.<Object>asList(getName(), getChannelName()), permits);
    }

关于 commandExecutor 执行成功与失败 

Redission 中 RPermitExpirableSemaphore 用法

标签:.com   tps   lease   for   perm   false   final   ini   semaphore   

原文地址:https://www.cnblogs.com/number7/p/9594518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!