版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z69183787/article/details/79033670
lua脚本:
-
local current = redis.call(
'incrBy',KEYS[1],ARGV[1]);
" +
-
"
if current == tonumber(ARGV[1])
then
" +
-
"
local t = redis.call(
'ttl',KEYS[1]);
" +
-
"
if t == -1
then
" +
-
" redis.call(
'expire',KEYS[1],ARGV[2])
" +
-
" end;
" +
-
" end;
" +
-
"
return current
";
JedisCluter
-
public Long incrBy(final String key, final long step, final int defaultExpire) {
-
return
new JedisClusterCommand<Long>(connectionHandler, maxRedirections, Protocol.Command.INCR) {
-
@Override
-
public Long execute(Jedis connection) {
-
if (defaultExpire >
0) {
-
Object val = connection.eval(ScriptConstant.INCRBY_1_EXPIRE,
1,
new String[]{key, String.valueOf(step), String.valueOf(defaultExpire)});
-
return Long.valueOf(val.toString());
-
}
else {
-
return connection.incrBy(key, step);
-
}
-
}
-
}.run(key);
-
}