Skip to content

Instantly share code, notes, and snippets.

@borbit
Last active March 3, 2021 11:46
Show Gist options
  • Save borbit/4739255 to your computer and use it in GitHub Desktop.
Save borbit/4739255 to your computer and use it in GitHub Desktop.
CAS implementation for Redis.
var LUA_GETS =
'return {'
+ 'redis.call(\'get\', KEYS[1]),'
+ 'redis.call(\'get\', KEYS[1]..\'_ver\')'
+ '}';
var LUA_CAS =
'local ver = redis.call(\'get\', KEYS[1]..\'_ver\') '
+ 'if not ver or ver == KEYS[3] '
+ 'then '
+ 'redis.call(\'incr\', KEYS[1]..\'_ver\') '
+ 'return redis.call(\'set\', KEYS[1], KEYS[2]) '
+ 'else '
+ 'return redis.error_reply(\'LATE\') '
+ 'end';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment