Try Playground
Show / Hide Table of Contents

Method InsertAsync

InsertAsync(String, Object)

Inserts an object into the cache asynchronously, with a cache key to reference its location.

Declaration
Task<CacheItemVersion> InsertAsync(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
System.Threading.Tasks.Task<CacheItemVersion>

Task that performs an insert operation in the background. Once completed, returns a version of CacheItem that was added in cache. Task Status property can be used to determine status of the Task that can be IsCanceled, IsCompleted, and IsFaulted.

Remarks

If the key already exists, this overload overwrites the values of the existing ICache item. If the key does not exist, it adds the item to the cache.

Examples

The following 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.InsertAsync(key, product).ContinueWith(task => OnItemInserted(key, task));

private static void OnItemInserted(string key, Task<CacheItemVersion> task)
{
    if (task.Status == TaskStatus.RanToCompletion)
    {
    ...
    }
    if (task.Status == TaskStatus.Faulted)
    {
    ...
    }
    if (task.Exception != null)
    {
    ...
    }
}

InsertAsync(String, CacheItem, WriteThruOptions)

Inserts a CacheItem into the cache asynchronously, with a cache key to reference its location and WriteThruOptions.

Declaration
Task<CacheItemVersion> InsertAsync(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
System.Threading.Tasks.Task<CacheItemVersion>

Task that performs an insert 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

The following 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.InsertAsync(key, item, writeThruOptions).ContinueWith(task => OnItemInserted(key, task));

private static void OnItemInserted(string key, Task<CacheItemVersion> task)
{
    if (task.Status == TaskStatus.RanToCompletion)
    {
    ...
    }
    if (task.Status == TaskStatus.Faulted)
    {
    ...
    }
    if (task.Exception != null)
    {
    ...
    }
}

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top