These two code snippets demonstrate basic Memcached CRUD operations, numeric increment and append operations. Let’s walk through the core APIs and execution logic first:
Set / Replace / Add
static void Main(string[] args)
{
MemcachedClient uniqueInstance = new MemcachedClient();
// 1. StoreMode.Set: Create new entry if key does not exist, overwrite if it already exists
uniqueInstance.Store(StoreMode.Set, "site", "foxdevelop.com");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Output: foxdevelop.com
// 2. StoreMode.Replace: Update only when the key **exists**, no‑op if key is missing
uniqueInstance.Store(StoreMode.Replace, "site", "foxdevelop.com2");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Output: foxdevelop.com2
// 3. StoreMode.Add: Create entry only when key **does not exist**, skip modification if key exists
uniqueInstance.Store(StoreMode.Add, "site", "www.google.com");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Key site already exists, no effect, still output: www.baidu.com
// 4. Call Set again to overwrite existing value
uniqueInstance.Store(StoreMode.Set, "site", "www.microsoft.com");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Output: www.microsoft.com
// Append: Append string to the end of existing value
// uniqueInstance.Append("site", "");
// Increment numeric value
uniqueInstance.Increment("count", 1, 2);
Console.Read();
}Code language: JavaScript (javascript)
Three StoreMode Modes Comparison
| Mode | Execution Rule |
|---|---|
StoreMode.Set | Universal, overwrite existing key, create new one if missing |
StoreMode.Add | Create‑only, skip write if key already exists |
StoreMode.Replace | Update‑only, skip write if key does not exist |
Remove Deletion and Increment
MemcachedClient uniqueInstance = new MemcachedClient();
uniqueInstance.Store(StoreMode.Set, "site", "foxdevelop.com");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Output: foxdevelop.com
// Remove: Delete specified key
uniqueInstance.Remove("site");
Console.WriteLine(uniqueInstance.Get<string>("site"));
// Key has been deleted, Get returns empty string
// Append commented out
// uniqueInstance.Append("site",);
// Increment(key, step, initialValue)
uniqueInstance.Increment("count", 1, 2);
Console.WriteLine(uniqueInstance.Get<string>("count"));Code language: JavaScript (javascript)
Key API Reference
Remove("key")
Delete specified key from cache; after deletionGet<T>cannot fetch data and returns default value (empty string for string type).Increment("count", 1, 2)
Parameter definitions:
- Param 1: Key name
count - Param 2: Increment step (+1 each time)
- Param 3: Initial value
- If
countdoes not exist → create key with initial value = 2 - If
countalready exists → add 1 to original numeric value
Incrementonly works with numeric‑formatted strings, it cannot be used for ordinary website text.
Append(key, data)
Append content to the tail of value mapped by target key; no effect when key does not exist.
There is also pairedPrependto insert content at value head.
CRUD Operations
Previous: C# Usage
Next: Object Persistence