Skip to content

Commit cdefff2

Browse files
author
Vitalii Cherkashyn
committed
redis lua scripting
1 parent 9bd048a commit cdefff2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

redis/lua-script.java.snippet

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class UpdateScript {
2+
private final Jedis jedis;
3+
private final String sha;
4+
private final static String script =
5+
"local key = KEYS[1] " +
6+
"local new = ARGV[1] " +
7+
"local current = redis.call('GET', key) " +
8+
"if (current == false) or " +
9+
" (tonumber(new) < tonumber(current)) then " +
10+
" redis.call('SET', key, new) " +
11+
" return 1 " +
12+
"else " +
13+
" return 0 " +
14+
"end";
15+
16+
public UpdateIfLowestScript(Jedis jedis) {
17+
this.jedis = jedis;
18+
this.sha = jedis.scriptLoad(script);
19+
}
20+
21+
public boolean updateIfLowest(String key, Integer newValue) {
22+
List<String> keys = Collections.singletonList(key);
23+
List<String> args = Collections.singletonList(String.valueOf(newValue));
24+
Object response = jedis.evalsha(sha, keys, args);
25+
return (Long)response == 1;
26+
}
27+
}

0 commit comments

Comments
 (0)