Method InsertAsync
InsertAsync(String, Object, AsyncItemUpdatedCallback, String, String)
Inserts an object into the Cache asynchronously with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.
Declaration
[TargetMethod(1)]
public virtual void InsertAsync(string key, object value, AsyncItemUpdatedCallback onAsyncItemUpdateCallback, string group, string subGroup)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
System.Object | value | The item to be added to the cache. |
AsyncItemUpdatedCallback | onAsyncItemUpdateCallback | A delegate that can be used to get the result of the Asynchronous update operation. |
System.String | group | An string that logically groups the data togehter. |
System.String | subGroup | The name of the subGroup within the group. |
Remarks
This is similar to Insert(String, Object) except that the operation is performed asynchronously. A ItemUpdated event is fired upon successful completion of this method.It is not possible to determine if the actual operation has failed, therefore use this operation for the cases when it does not matter much.
Note: If exceptions are enabled through the ExceptionsEnabled setting, this property throws exception incase of failure.
Examples
The following example demonstrates how to insert an item into the cache asynchronously with associated group and subGroup. It also registers a delegate to get the result of the asynchronous operation. it into your application's Cache object.
Cache cache = NCache.InitializeCache("myCache");
cache.InsertAsync("DSN", connectionString, new AsyncItemUpdatedCallback(onAsyncItemUpdated), "group-name", "subGroup-name");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
InsertAsync(String, CacheItem, DSWriteOption, DataSourceItemsUpdatedCallback)
Insert a CacheItem to the cache asynchoronously
Declaration
[TargetMethod(2)]
public virtual void InsertAsync(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
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 |
DataSourceItemsUpdatedCallback | onDataSourceItemUpdatedCallback | A delegate that, if provided, is called when item is updated in 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 insert an item into the cache asynchronously. It also provides the option to udpate the data source and registers a delegate to get the result of the data source operation.
CacheItem item = new CacheItem(connectionString);
item.AbsoluteExpiration = DateTime.Now.AddMinutes(2);
item.SlidingExpiration = TimeSpan.Zero;
item.Priority = CacheItemPriority.High;
item.ItemRemoveCallback = onRemove;
item.AsyncItemUpdateCallback = OnAsyncItemUpdated;
Then insert CacheItem to the cache
Cache cache = NCache.InitializeCache("myCache");
cache.InsertAsync("DSN", item, DSWriteOption.WriteThru, new DataSourceItemsUpdatedCallback(onDSItemsUpdated));
InsertAsync(String, CacheItem, String, DSWriteOption, DataSourceItemsUpdatedCallback)
Insert a CacheItem to the cache asynchoronously
Declaration
[TargetMethod(3)]
public virtual void InsertAsync(string key, CacheItem item, string providerName, DSWriteOption dsWriteOption, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The cache key used to reference the item. |
CacheItem | item | The item that is to be stored |
System.String | providerName | The datasource name to use for write-through operation. |
DSWriteOption | dsWriteOption | Options regarding updating data source |
DataSourceItemsUpdatedCallback | onDataSourceItemUpdatedCallback | A delegate that, if provided, is called when item is updated in data source. |