Method Insert
Insert(String, Object)
Inserts an item (object) into the cache.
Declaration
CacheItemVersion Insert(string key, object value)
Parameters
Type | Name | Description |
---|---|---|
System. |
key | Unique key to identify the cache item. |
System. |
value | The item (object) that is to be inserted into the cache. |
Returns
Type | Description |
---|---|
Cache |
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 Cache
Declaration
CacheItemVersion Insert(string key, CacheItem item, LockHandle lockHandle, bool releaseLock)
Parameters
Type | Name | Description |
---|---|---|
System. |
key | Unique key to identify the cache item. |
Cache |
item | The CacheItem that is to be inserted into the cache. |
Lock |
lockHandle | An instance of Lock |
System. |
releaseLock | A flag to determine whether or not the lock should be released after operation is performed. |
Returns
Type | Description |
---|---|
Cache |
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 Cache
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 Cache
Declaration
CacheItemVersion Insert(string key, CacheItem item, WriteThruOptions writeThruOptions = null, LockHandle lockHandle = null, bool releaseLock = false)
Parameters
Type | Name | Description |
---|---|---|
System. |
key | Unique key to identify the CacheItem. |
Cache |
item | The CacheItem that is to be inserted into the cache. |
Write |
writeThruOptions | Write |
Lock |
lockHandle | An instance of Lock |
System. |
releaseLock | A flag to determine whether or not the lock should be released, after operation is performed. |
Returns
Type | Description |
---|---|
Cache |
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 Cache
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)
{
...
}
}