Skip to content

Instantly share code, notes, and snippets.

@mohak1712
Last active December 27, 2020 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohak1712/97e20266923767ee4e343bcfe093785c to your computer and use it in GitHub Desktop.
Save mohak1712/97e20266923767ee4e343bcfe093785c to your computer and use it in GitHub Desktop.
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