Remove Cache Data
NCache provides the following two overloads with different return types to remove the object(s) from the cache data by accepting the cache item key(s) as the input parameter.
bool Remove<T>
void Remove
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, Count, Remove, Contains, RemoveBulk, RemoveAsync.
Remove Object From Cache
Note
This feature is also available in NCache Professional.
An object of a custom class can be removed from the cache data using any of the Remove
methods.
Important
If an item does not exist in the cache data, null is returned.
Tip
One quick way to verify whether an item has been removed is to use either of the following properties of the cache class:
Count
returns the number of items present in the cache.Contains
verifies if a specified key exists in the cache.
The following example removes the item corresponding to the specified key and casts the returned object into a Customer object to check whether the object is of Customer type or not. If not, it is added back to the cache.
// Precondition: Cache is already connected
// Unique cache key of product to remove
string key = $"Product:{product.ProductID}";
string customerKey = $"Customer:ALFKI";
// Create an object to store removed item
Customer customerRemoved = null;
// Remove specified item from cache
bool isItemRemoved = cache.Remove(customerKey, out customerRemoved);
if (isItemRemoved)
{
Console.WriteLine($"Customer with ID {customerRemoved.CustomerID} has been removed");
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
If you want to save your computational cost, you can use the void Remove
function that does not return anything and thus performs faster. Here is how you can use it.
Remove Bulk Items From Cache
Note
This feature is also available in NCache Professional.
NCache provides a RemoveBulk
method to remove a bulk of cache items against the specified array of cache keys. It returns a dictionary of the keys and objects removed from the cache.
Important
If the specified items exist in the cache, a dictionary of the keys and objects removed is returned.
Tip
One quick way to verify whether an item has been removed is to use either of the following properties of the cache class:
Count
returns the number of items present in the cache.Contains
verifies if a specified key exists in the cache.
The following example removes a bulk of existing cache items and casts the returned object into Customer objects.
// Create an array of all keys to remove
String[] keysToRemove = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT", "Customer:BERGS"
};
// Create dictionary to store removed items
IDictionary<string, Customer> removedItems;
// Remove items from DB
if (DeleteFromDB(keysToRemove))
{
// Remove bulk items from cache
cache.RemoveBulk(keysToRemove, out removedItems);
if (removedItems.Count != keysToRemove.Length)
Console.WriteLine($"Failed to remove {keysToRemove.Length - removedItems.Count} items from cache.");
}
Remove Objects From Cache With Asynchronous API
RemoveAsync
returns object of the Task class that can be further used according to the business needs of the client application. NCache provides three different status flags to notify the success or failure of the operation.
Important
Unlike Remove
and RemoveBulk
, RemoveAsync
does not generally return the removed objects to the application as it is an asynchronous operation and has to be fetched.
string customerKey = $"Customer:ALFKI";
// Remove specified item from cache
Task<Customer> task = cache.RemoveAsync<Customer>(customerKey);
//This task object can be used as per your business needs
if (task.IsCompleted)
{
// Get Customer object from task result
Customer customer = task.Result;
Console.WriteLine($"Item {customer.CustomerID} has been removed.");
}
Additional Resources
NCache provides the sample application for Basic Operations on GitHub.
See Also
.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Python: ncache.client class.
Node.js: Cache class.