Method Remove
Remove(String, LockHandle, CacheItemVersion, WriteThruOptions)
Removes the specified item from the ICache. You can also specify the write option such that the item may be removed from both cache and data source. If version is specified then item will only be removed if the specified version is still the most recent version in the cache.
Declaration
void Remove(string key, LockHandle lockHandle = null, CacheItemVersion version = null, WriteThruOptions writeThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique key of the item to be removed. |
LockHandle | lockHandle | If the item is locked, it can be removed only if the correct lockHandle is specified. lockHandle should be the same which was used initially to lock the item, otherwise you will get the 'OperationFailedException'. |
CacheItemVersion | version | The version of the item to be removed. The item is removed from the cache only if this is still the most recent version in the cache. |
WriteThruOptions | writeThruOptions | WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or None. |
Examples
Example demonstrates how to remove a locked item in the cache with write through options.
ICache cache = CacheManager.GetCache("demoCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
string key = "Product0";
CacheItemVersion version = cache.Add(key, product);
LockHandle lockHandle = new LockHandle();
object item = cache.Get<Product>(key, true, TimeSpan.Zero, ref lockHandle);
if (item != null)
{
try
{
WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");
cache.Remove(key, lockHandle, version, writeThruOptions);
}
catch (OperationFailedException ex)
{
...
}
}
Remove<T>(String, out T, LockHandle, CacheItemVersion, WriteThruOptions)
Removes the specified item from the ICache and returns it to the application as an out parameter. You can also specify the write option such that the item may be removed from both cache and data source. If version is specified then item will only be removed if the specified version is still the most recent version in the cache.
Declaration
bool Remove<T>(string key, out T removedItem, LockHandle lockHandle = null, CacheItemVersion version = null, WriteThruOptions writeThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique key of the item to be removed. |
T | removedItem | out Parameter through which the removed item from cache is returned |
LockHandle | lockHandle | If the item is locked, it can be removed only if the correct lockHandle is specified. lockHandle should be the same which was used initially to lock the item, otherwise you will get the 'OperationFailedException'. |
CacheItemVersion | version | The version of the item to be removed. The item is removed from the cache only if this is still the most recent version in the cache. |
WriteThruOptions | writeThruOptions | WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or None. |
Returns
Type | Description |
---|---|
System.Boolean | Flag which determines the status of remove operation. True if item was removed successfully from the cache or False if remove opeartaion failed. |
Type Parameters
Name | Description |
---|---|
T | Specifies the type of value obtained from the cache. |
Examples
Example demonstrates how you can remove an item from cache
ICache cache = CacheManager.GetCache("demoCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
string key = "Product0";
CacheItemVersion version = cache.Add(key, product);
LockHandle lockHandle = new LockHandle();
object item = cache.Get<Product>(key, true, TimeSpan.Zero, ref lockHandle);
WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");
try
{
Product removedProduct = null;
if (cache.Remove<Product>(key, out removedProduct, lockHandle, version, writeThruOptions))
{
//Removed successfully
}
else
{
//Error Occured
}
}
catch(Exception ex)
{
...
}