Inserts a CacheItem to the cache if not already existing. Otherwise updates an existing item if it not already locked or if the correct lock-id is specified.

Namespace: Alachisoft.NCache.Web.Caching
Assembly: Alachisoft.NCache.Web (in Alachisoft.NCache.Web.dll) Version: 4.3.0.0 (4.3.0.0)

Syntax

C#
public virtual CacheItemVersion Insert(
	string key,
	CacheItem item,
	LockHandle lockHandle,
	bool releaseLock
)
Visual Basic
Public Overridable Function Insert ( 
	key As String,
	item As CacheItem,
	lockHandle As LockHandle,
	releaseLock As Boolean
) As CacheItemVersion
Visual C++
public:
virtual CacheItemVersion^ Insert(
	String^ key, 
	CacheItem^ item, 
	LockHandle^ lockHandle, 
	bool releaseLock
)

Parameters

key
Type: System..::..String
The cache key used to reference the item.
item
Type: Alachisoft.NCache.Web.Caching..::..CacheItem
The item that is to be stored
lockHandle
Type: Alachisoft.NCache.Web.Caching..::..LockHandle
An instance of LockHandle. If the item is locked, then it can be updated only if the correct lockHandle is specified.
releaseLock
Type: System..::..Boolean
A flag to determine whether or not release lock after operation is performed.

Return Value

Type: CacheItemVersion
The object item added to the Cache.

Remarks

If lockId does not match with the lockId associated with cached item, an exception will be thrown.

Examples

The following example demonstrates how to update a locked item in the cache. First create a CacheItem.
 Copy imageCopy
Cache theCache = NCache.InitializeCache("myreplicatedcache");
Add an item int the cache.
 Copy imageCopy
theCache.Add("cachedItemKey", new CacheItem("cachedItemValue"));
Create the objects for lockid and lockdate.
 Copy imageCopy
LockHandle lockHandle = new LockHandle();
Get the added item from cache and acquire a lock.
 Copy imageCopy
object cachedItem = theCache.Get("cachedItemKey", Cache.NoLockingExpiration, ref lockHandle, true);
if (cachedItem != null)
{
    try
    {
        theCache.Insert("cachedItemKey", new CacheItem("someothervalue"), lockHandle, true);
        string cachedValue = (string)theCache.Get("cachedItemKey");
    }
    catch (OperationFailedException ex)
    {
        //Do something
    }
}

See Also