Does a开发者_如何学运维nyone know how to check if a key exists using Membase Client (Enyim)? I dont want to pull the entire object from the cache, just check if its in there.
Unfortunately there is no "exists" operation in memcached, but there are some hacks you can do:
- do a
client.Store(StoreMode.Add, keyToCheck, null, new Date(2000, 1, 1));
Add fails if the item already exists, and will return false in this case. - you can
client.Append(keyToCheck, new ArraySegment<byte>(new byte[0]))
This will returntrue
if the key exists, but its value will not change. - or
client.Cas(StoreMode.Set, keyToCheck, null, UInt64.MaxValue)
This will also fail if the item exists.
Please keep in mind that all three operations will create an item if the key does not exist, so you need to clean up after them.
Also, if multiple threads are checking for the same item, then it's possible that one thread will treat another threads temp item as the real one.
精彩评论