Method Delete
Delete(String)
Removes the object from the Cache.
Declaration
public virtual void Delete(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
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.Delete("timestamp");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
Delete(String, DSWriteOption, DataSourceItemsRemovedCallback)
Removes the object from the Cache.
Declaration
public virtual void Delete(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. |
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 |
|
Delete(String, DSWriteOption, String, DataSourceItemsRemovedCallback)
Removes the object from the Cache.
Declaration
public virtual void Delete(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. |
Remarks
System.ArgumentNullException System.ArgumentException
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");
Delete(String, LockHandle)
Removes an item from cache if it is not already locked or if the correct lock-id is specified.
Declaration
public virtual void Delete(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. |
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
}
}
Delete(String, CacheItemVersion)
Removes an item from cache if the specified version is still the most recent version in the cache.
Declaration
public virtual void Delete(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. |
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
}
}