Does anyone 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.
Asked
Active
Viewed 669 times
1 Answers
3
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.

Attila Kiskó
- 46
- 3
-
Thanks Attila, will use your hacks to craft a ContainsKey extension method with proper housekeeping. :) – MindWire Dec 01 '10 at 20:06
-
if you can send it to me as a pull reqiest i'll include in the next version. – Attila Kiskó Dec 01 '10 at 23:09