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.1.0.0 (4.1.0.0)

Syntax

C#
public CacheItemVersion Insert(
	string key,
	CacheItem item,
	LockHandle lockHandle,
	bool releaseLock
)
Visual Basic
Public Function Insert ( _
	key As String, _
	item As CacheItem, _
	lockHandle As LockHandle, _
	releaseLock As Boolean _
) As CacheItemVersion
Visual C++
public:
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

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.
CopyC#
Cache theCache = NCache.InitializeCache("myreplicatedcache");
Add an item int the cache.
CopyC#
theCache.Add("cachedItemKey", new CacheItem("cachedItemValue"));
Create the objects for lockid and lockdate.
CopyC#
LockHandle lockHandle = new LockHandle();
Get the added item from cache and acquire a lock.
CopyC#
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