Removing Data from Cache
NCache provides two methods (remove, delete) to delete an object from the cache.
Delete Method | Remove Method |
---|---|
Returns void after deletion. | Deletes data from the cache and returns status to the application. |
For Delete/Remove API calls, if a backing source is configured and Write-through is enabled, all the keys passed onto the cache via the API will first be looked into and deleted from the cache and irrespective of the key mapping, the call will be forwarded to the backing source.
If the desired mode is set to Write-Behind, and call backs are configured, you will receive them for the successful execution of the provider upon all the given keys.
Using Remove() Method
Remove
removes the key from the cache and
returns the removed object to the cache.
string key = "Product:1001";
Product product = null;
try{
// null is returned if key does not exist in cache
object result = cache.Remove(key);
if (result != null)
{
if (result is Product)
{
product = (Product)result;
}
}
}
catch (OperationFailedException ex){
// handle exception
}
Using Delete() Method
Delete
deletes the key from the cache without returning the object.
string key = "Product:1001";
try
{
cache.Delete(key);
}
catch (OperationFailedException ex)
{
// handle exception
}