Try Playground
Show / Hide Table of Contents

Method Insert

Insert(String, Object)

Inserts an item (object) into the cache.

Declaration
CacheItemVersion Insert(string key, object value)
Parameters
Type Name Description
System.String key

Unique key to identify the cache item.

System.Object value

The item (object) that is to be inserted into the cache.

Returns
Type Description
CacheItemVersion

Represents the version of each cache item.

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 insert an item (object) into the cache.

Cache cache = CacheManager.GetCache("demoClusteredCache");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

string key = "Product0";

cache.Insert(key,product);

Insert(String, CacheItem, LockHandle, Boolean)

Inserts a CacheItem into the cache.

Declaration
CacheItemVersion Insert(string key, CacheItem item, LockHandle lockHandle, bool releaseLock)
Parameters
Type Name Description
System.String key

Unique key to identify the cache item.

CacheItem item

The CacheItem that is to be inserted into the cache.

LockHandle lockHandle

An instance of LockHandle that holds the lock information. If the item is locked, then it can only be updated if the correct lockHandle is specified.

System.Boolean releaseLock

A flag to determine whether or not the lock should be released after operation is performed.

Returns
Type Description
CacheItemVersion

Represents the version of each cache item.

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. 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 to the cache with a sliding expiration of 5 minutes, a priority of high.

ICache cache = CacheManager.GetCache("demoClusteredCache");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);
item.Priority = CacheItemPriority.Low;

string key = "Product0";

cache.Add(key, item);

LockHandle lockHandle = new LockHandle();

CacheItem cachedItem = cache.Get<CacheItem>("cachedItemKey", true, new TimeSpan(0, 5, 0), ref lockHandle);

if (cachedItem != null)
{
    try
    {
       cachedItem.Priority = CacheItemPriority.High;
       cachedItem.Expiration = new Expiration(ExpirationType.Sliding, new TimeSpan(0, 2, 0));

       cache.Insert(key, cachedItem,  lockHandle, true);
    }
    catch (OperationFailedException ex)
    {
    ...
    }
}

Insert(String, CacheItem, WriteThruOptions, LockHandle, Boolean)

Inserts a CacheItem into the cache, along with allowing to specify the WriteThruOptions.

Declaration
CacheItemVersion Insert(string key, CacheItem item, WriteThruOptions writeThruOptions = null, LockHandle lockHandle = null, bool releaseLock = false)
Parameters
Type Name Description
System.String key

Unique key to identify the CacheItem.

CacheItem item

The CacheItem that is to be inserted into the cache.

WriteThruOptions writeThruOptions

WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or none.

LockHandle lockHandle

An instance of LockHandle that holds the lock information. If the item is locked, then it can only be updated if the correct lockHandle is specified.

System.Boolean releaseLock

A flag to determine whether or not the lock should be released, after operation is performed.

Returns
Type Description
CacheItemVersion

Represents the version of each cache item.

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. If CacheItem contains invalid values the related exception is thrown. The functionality of lockhandle with WriteThru is not supported. See CacheItem for invalid property values and related exceptions.

Examples

The following example demonstrates how to insert an item to the cache with a sliding expiration of 5 minutes and a high priority.

ICache cache = CacheManager.GetCache("demoClusteredCache");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);
item.Priority = CacheItemPriority.Low;

string key = "Product0";

cache.Add(key, item);

LockHandle lockHandle = new LockHandle();

CacheItem cachedItem = cache.Get<CacheItem>("cachedItemKey", true, new TimeSpan(0, 5, 0), ref lockHandle);

if (cachedItem != null)
{
    try
    {
       cachedItem.Priority = CacheItemPriority.High;
       cachedItem.Expiration = new Expiration(ExpirationType.Sliding, new TimeSpan(0, 2, 0));

       WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");

       cache.Insert(key, cachedItem, writeThruOptions, lockHandle, true);
    }
    catch (OperationFailedException ex)
    {
    ...
    }
}
In This Article
  • Insert(String, Object)
  • Insert(String, CacheItem, LockHandle, Boolean)
  • Insert(String, CacheItem, WriteThruOptions, LockHandle, Boolean)

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 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top