Memcached gets 命令
gets 是带 CAS 版本号的 get,配合 cas 做乐观锁。
Key Pointsgets 返回末尾多一个 cas 唯一标识,写回时当作 cas 的参数。
用法
Shell
set k 0 0 1
9
gets kOutput
STORED
VALUE k 0 1 42
9
ENDThe trailing 42 in VALUE k 0 1 42 is the cas token. Remember it and use cas k 0 0 1 42 to write back; if changed meanwhile, the token differs and cas returns EXISTS.
gets and get differ only by the token, nearly identical cost. Use gets+cas whenever you need a concurrency-safe write; get is enough for reads.用法
Shell
set k 0 0 1
9
gets kOutput
STORED
VALUE k 0 1 42
9
ENDVALUE k 0 1 42 末尾的 42 就是 cas token。把它记住,稍后用 cas k 0 0 1 42 写回即可;期间若被别人改过,token 变,cas 会返回 EXISTS。
gets 与 get 只差一个 token,开销几乎一样。需要并发安全写就统一用 gets+cas,只读场景用 get 即可。