Other Operations

1. FlushAll() Clear Cache

mClient.Store(StoreMode.Set, "key1", 1234L);
Console.Write(mClient.Get("key1")); // Retrieve 1234 normally
mClient.FlushAll();
Console.Write(mClient.Get("key1")); // Output nullCode language: JavaScript (javascript)
Key Notes
  1. FlushAll(): Wipes all keys on the Memcached server‑handle with great care!
  • Never invoke this in production; it erases all business‑related cache entries.
  • It invalidates existing records without freeing memory (Memcached lazy eviction).

2. TryGet Safe Cache Retrieval

object obj = null;
var isSuc = mClient.TryGet("key1", out obj );
// Key exists: isSuc=true, obj =1234

isSuc = mClient.TryGet("key1", out obj );
// Key missing: isSuc=false, obj remains nullCode language: JavaScript (javascript)

Get() vs TryGet()

MethodCharacteristicsUse‑case
mClient.Get(key)Returns null for missing keys; cannot tell apart a null‑valued cache entry from a non‑existent keyBasic read operations where key existence does not matter
mClient.TryGet(key, out obj)Boolean return value indicates key presence; distinguishes two kinds of null scenarios preciselyRecommended for logic requiring cache‑hit detection

Generic overload is available: mClient.TryGet<T>(key, out T value)

Important Remarks

  • FlushAll() is a server‑side command. Under cluster setup it purges data across every node by default;
  • Memcached offers no persistence. All data vanishes once the service restarts;
  • Prefer TryGet for cache‑penetration guard checks to prevent flood of database queries.

Other Operations

Leave a Reply

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