Last active
December 27, 2020 07:48
-
-
Save mohak1712/97e20266923767ee4e343bcfe093785c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String acquire(String entityId) { | |
String lock = UUID(); | |
for (int i = 0; i < attempts; i++) { | |
if ("OK".equals(poolManager.withRedis(j -> j.set(entityId, lock, "NX", "EX", timeToLive)))) { | |
//lock obtained if key entityId does not already exist with an expiry of timeToLive | |
return entityId; | |
} | |
// error obtaining lock | |
} | |
} | |
public void release(String lockKey) { | |
poolManager.withRedis(j -> j.del(lockKey)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment