File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments