Method Remove
Remove(String)
Removes the object from the Cache.
Declaration
public virtual object Remove(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
Returns
Type | Description |
---|---|
System.Object | The item removed from the Cache. If the value in the key parameter is not found, returns a null reference (Nothing in Visual Basic). |
Remarks
Note: If exceptions are enabled through the ExceptionsEnabled setting, this property throws exception in case of failure.
Examples
The following example demonstrates how you can remove an item from your application's Cache object.
Cache cache = NCache.InitializeCache("myCache");
cache.Remove("timestamp");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
Remove(String, DSWriteOption, DataSourceItemsRemovedCallback)
Removes the object from the Cache.
Declaration
public virtual object Remove(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
DSWriteOption | dsWriteOption | Options regarding updating the data source. |
DataSourceItemsRemovedCallback | onDataSourceItemRemovedCallback | A delegate that, if provided, is called when item is removed from data source. |
Returns
Type | Description |
---|---|
System.Object | The item removed from the Cache. If the value in the key parameter is not found, returns a null reference (Nothing in Visual Basic). |
Remarks
Note: If exceptions are enabled through the ExceptionsEnabled setting, this property throws exception in case of failure.
Examples
The following example demonstrates how you can remove an item from your application's Cache object.
Cache cache = NCache.InitializeCache("myCache");
cache.Remove("timestamp", DSWriteOption.None, null, "group-name", "subGroup-name");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
Remove(String, DSWriteOption, String, DataSourceItemsRemovedCallback)
Removes the object from the Cache.
Declaration
public virtual object Remove(string key, DSWriteOption dsWriteOption, string providerName, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
DSWriteOption | dsWriteOption | Options regarding updating the data source. |
System.String | providerName | Provider name. |
DataSourceItemsRemovedCallback | onDataSourceItemRemovedCallback | A delegate that, if provided, is called when item is removed from data source. |
Returns
Type | Description |
---|---|
System.Object | The item removed from the Cache. If the value in the key parameter is not found, returns a null reference (Nothing in Visual Basic). |
Remarks
Note: If exceptions are enabled through the ExceptionsEnabled setting, this property throws exception in case of failure.
Examples
The following example demonstrates how you can remove an item from your application's Cache object.
Cache cache = NCache.InitializeCache("myCache");
cache.Remove("timestamp", DSWriteOption.None, null, "group-name", "subGroup-name");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
Remove(String, LockHandle)
Removes an item from cache if it is not already locked or if the correct lock-id is specified.
Declaration
public virtual object Remove(string key, LockHandle lockHandle)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | key of item to be removed |
LockHandle | lockHandle | If the item is locked then, it can be removed only if the correct lockHandle is specified. |
Returns
Type | Description |
---|---|
System.Object | The item removed from the Cache. If the value in the key parameter is not found, returns a null reference (Nothing in Visual Basic). |
Examples
The following example demonstrates how to remove a locked item from the cache.
First create a CacheItem.
Cache theCache = NCache.InitializeCache("myreplicatedcache");
Add an item int the cache.
theCache.Add("cachedItemKey", "cachedItemValue");
Create the lock-handle.
LockHandle lockHandle = new LockHandle();
Get the added item from cache and acquire a lock.
object cachedItem = theCache.Get("cachedItemKey", ref lockHandle, true);
if (cachedItem != null)
{
try
{
//Now remove the cached item using lockHandle acquired earlier.
object removedItem = theCache.Remove("cachedItemKey", lockHandle);
}
catch (OperationFailedException ex)
{
//Do something
}
}
Remove(String, CacheItemVersion)
Removes an item from cache if the specified version is still the most recent version in the cache.
Declaration
public virtual object Remove(string key, CacheItemVersion version)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | key of item to be removed |
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. |
Returns
Type | Description |
---|---|
System.Object | The item removed from the Cache. If the value in the key parameter is not found, returns a null reference (Nothing in Visual Basic). |
Examples
The following example demonstrates how to remove a locked item from the cache.
First create a CacheItem.
Cache theCache = NCache.InitializeCache("myreplicatedcache");
Add an item in the cache.
theCache.Add("cachedItemKey", new "cachedItemValue");
Create the CacheItemVersion.
CacheItemVersion version = new CacheItemVersion();
Get the added item from cache and get the item version.
object cachedItem = theCache.Get("cachedItemKey", DSReadOption.None, ref version);
if (cachedItem != null)
{
try
{
//Now remove the cached item using version acquired earlier.
object removedItem = theCache.Remove("cachedItemKey", version);
}
catch (OperationFailedException ex)
{
//Do something
}
}