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);
}