CRUD Operations

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

ModeExecution Rule
StoreMode.SetUniversal, overwrite existing key, create new one if missing
StoreMode.AddCreate‑only, skip write if key already exists
StoreMode.ReplaceUpdate‑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

  1. Remove("key")
    Delete specified key from cache; after deletion Get<T> cannot fetch data and returns default value (empty string for string type).
  2. Increment("count", 1, 2)
    Parameter definitions:
  • Param 1: Key name count
  • Param 2: Increment step (+1 each time)
  • Param 3: Initial value
  • If count does not exist → create key with initial value = 2
  • If count already exists → add 1 to original numeric value

Increment only works with numeric‑formatted strings, it cannot be used for ordinary website text.

  1. Append(key, data)
    Append content to the tail of value mapped by target key; no effect when key does not exist.
    There is also paired Prepend to insert content at value head.

CRUD Operations

Leave a Reply

Your email address will not be published. Required fields are marked *