Remove Existing Data Structure From Cache
You can remove an existing distributed data structure (such as a List, Queue, HashSet, Dictionary, or Counter) from the cache using the Remove method of DataTypeManager or the cache.Remove method. Both approaches safely remove the specified data structure without affecting other cache items, and if the key does not exist, the operation completes without returning a value.
Prerequisites
Before using the NCache client-side APIs, ensure that the following prerequisites are fulfilled:
- Install the following NuGet packages in your .NET client application:
- Enterprise: Alachisoft.NCache.SDK
- OpenSource: Alachisoft.NCache.Opensource.SDK
- Include the following namespaces in your application:
- The cache must be running.
- Make sure that the data being added is serializable.
- For API details, refer to: ICache, DataTypeManager, Contains, Count, Remove.
Remove Data Structures from Cache
Tip
One quick way to verify whether the data structure has been removed is to use either property of the Cache class:
Containsverifies if the specified key exists in the cache.Countreturns the number of items present in the cache.
The following example assumes the key exists against a data structure in the cache. The data structure is then removed from the cache using this key.
try
{
// Precondition: Cache is connected
// Key exists in cache
string key = "ProductIDList";
// Remove List from Cache
cache.DataTypeManager.Remove(key);
}
catch (OperationFailedException ex)
{
// NCache specific exception
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentException or ArgumentNullException
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides a sample application for removing data structures from cache on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Node.js: NamedTagsDictionary class.