Add a CacheItem to the cache

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


public virtual CacheItemVersion Add(
	string key,
	CacheItem item,
	DSWriteOption dsWriteOption,
	DataSourceItemsAddedCallback onDataSourceItemAdded
Visual Basic
Public Overridable Function Add ( 
	key As String,
	item As CacheItem,
	dsWriteOption As DSWriteOption,
	onDataSourceItemAdded As DataSourceItemsAddedCallback
) As CacheItemVersion
Visual C++
virtual CacheItemVersion^ Add(
	String^ key, 
	CacheItem^ item, 
	DSWriteOption dsWriteOption, 
	DataSourceItemsAddedCallback^ onDataSourceItemAdded


Type: System..::..String
The cache key used to reference the item.
Type: Alachisoft.NCache.Web.Caching..::..CacheItem
The item that is to be stored
Type: Alachisoft.NCache.Web.Caching..::..DSWriteOption
Options regarding updating data source
Type: Alachisoft.NCache.Web.Caching..::..DataSourceItemsAddedCallback
A delegate that, if provided, is called when item is added to data source.

Return Value

Type: CacheItemVersion
An instance of CacheItemVersion


If CacheItem contains invalid values the related exception is thrown. See CacheItem for invalid property values and related exceptions


The following example demonstrates how to add an item to the cache with an absolute expiration of 2 minutes from now, a priority of high, and that notifies the application when the item is removed from the cache. First create a CacheItem.
 Copy imageCopy
object someData = new object();
CacheItem item = new CacheItem(someData);
item.AbsoluteExpiration = DateTime.Now.AddMinutes(2);
item.Priority = CacheItemPriority.High;
item.ItemRemoveCallback = onRemove;
Then add CacheItem to the cache
 Copy imageCopy
OnDataSourceItemAdded(IDictionary result)
Cache cache = NCache.InitializeCache("myCache");
   cache.Add("someData", item, DSWriteOption.WriteBehind, new DataSourceItemsAddedCallback(OnDataSourceItemAdded));

See Also