Method AddAsync
AddAsync(String, Object, AsyncItemAddedCallback, String, String)
Add the object to the cache asynchronously.
Declaration
[TargetMethod(1)]
public virtual void AddAsync(string key, object value, AsyncItemAddedCallback onAsyncItemAddCallback, string group, string subGroup)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
System.Object | value | The value that is to be stored |
AsyncItemAddedCallback | onAsyncItemAddCallback | A delegate that, if provided, is called to inform about the status of the operation. |
System.String | group | The data group of the item |
System.String | subGroup | The subGroup of the item |
Examples
The following example demonstrates how to add an object to the cache with group, subGroup and a delegate that notifies the application about the operation status.
First create the AsyncItemAddedCallback.
AsyncItemAddedCallback asyncItemAdded = new AsyncItemAddedCallback(OnAsyncItemAdded);
OnAsyncItemAdded(string key, object result)
{
...
}
Then add the object to the cache.
Cache cache = NCache.InitializeCache("myCache");
cache.AddAsync("timestamp", timestamp, asyncItemAdded, "group-name", "subGroup-name");
AddAsync(String, CacheItem, DSWriteOption, DataSourceItemsAddedCallback)
Add a CacheItem to the cache asynchronously
Declaration
[TargetMethod(2)]
public virtual void AddAsync(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
CacheItem | item | The item that is to be stored |
DSWriteOption | dsWriteOption | Options regarding updating data source |
DataSourceItemsAddedCallback | onDataSourceItemAdded | A delegate that, if provided, is called when item is added to data source. |
Remarks
If CacheItem contains invalid values the related exception is thrown. See CacheItem for invalid property values and related exceptions
Examples
The following example demonstrates how to add an item to the cache with a sliding expiration of 5 minutes, a priority of high, and that notifies the application when the item is removed from the cache.
First create a CacheItem.
CacheItem item = new CacheItem(timeStamp);
item.SlidingExpiration = new TimeSpan(0,5,0);
item.Priority = CacheItemPriority.High;
item.ItemRemoveCallback = onRemove;
Then add CacheItem to the cache
OnDataSourceItemAdded(IDictionary result)
{
...
}
Cache cache = NCache.InitializeCache("myCache");
cache.AddAsync("timestamp", item, DSWriteOption.WriteBehind, new DataSourceItemsAddedCallback(OnDataSourceItemAdded));
AddAsync(String, CacheItem, DSWriteOption, String, DataSourceItemsAddedCallback)
Declaration
[TargetMethod(3)]
public virtual void AddAsync(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemAdded)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | |
CacheItem | item | |
DSWriteOption | dsWriteOption | |
System.String | providerName | |
DataSourceItemsAddedCallback | onDataSourceItemAdded |