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
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, allowing to specify the Write-Through option.
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 cache item. |
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. Functionality of lockhandle with WriteThru is not supported. See CacheItem for invalid property values and related exceptions.
Examples
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));
WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");
cache.Insert(key, cachedItem, writeThruOptions, lockHandle, true);
}
catch (OperationFailedException ex)
{
...
}
}