Method AddAsync
AddAsync(String, Object)
Adds an object into the cache asynchronously with a cache key to reference its location.
Declaration
Task<CacheItemVersion> AddAsync(string key, object value)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique key to identify the cache item. |
System.Object | value | The item (object) to be stored in the cache. |
Returns
Type | Description |
---|---|
Task<CacheItemVersion> | Task that performs an add operation in the background. Once task is completed it will returns version of cache item that was added in cache. Task contain status of the operation which performed in background. That status can be one out of them completed, canceled, running or faulted. |
Examples
Example demonstrates how to add an object to the cache asynchronously with registering a Task to monitor the status of the operation.
ICache cache = CacheManager.GetCache("demoClusteredCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
string key = "Product0";
cache.AddAsync(key, product).ContinueWith(task => OnItemAdded(key, task));
private static void OnItemAdded(string key, Task<CacheItemVersion> task)
{
if (task.Status == TaskStatus.RanToCompletion)
{
...
}
if (task.Status == TaskStatus.Faulted)
{
...
}
if (task.Exception != null)
{
...
}
}
AddAsync(String, CacheItem, WriteThruOptions)
Adds a CacheItem into the cache asynchronously with a cache key to reference its location and WriteThruOptions.
Declaration
Task<CacheItemVersion> AddAsync(string key, CacheItem item, WriteThruOptions writeThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique key to identify the cache item. |
CacheItem | item | CacheItem that is to be stored in the cache. |
WriteThruOptions | writeThruOptions | WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or None. |
Returns
Type | Description |
---|---|
Task<CacheItemVersion> | Task that performs an add operation in the background. Once completed returns version of cache item that was added in cache. Task Status property can be used to determine status of the task that can be IsCanceled, IsCompleted, and IsFaulted. |
Examples
Example demonstrates how to add an CacheItem to the cache asynchronously with registering a Task to monitor the status of the operation along with WriteThruOptions.
ICache cache = CacheManager.GetCache("demoClusteredCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
CacheItem item = new CacheItem(product);
string key = "Product0";
WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");
cache.AddAsync(key, item, writeThruOptions).ContinueWith(task => OnItemAdded(key, task));
private static void OnItemAdded(string key, Task<CacheItemVersion> task)
{
if (task.Status == TaskStatus.RanToCompletion)
{
...
}
if (task.Status == TaskStatus.Faulted)
{
...
}
if (task.Exception != null)
{
...
}
}