Class CacheItem
NCache uses a "key" and "value" structure for storing objects in cache. When an object is added in cache it is stored as value and metadata against the specified key. This combination of value and metadata is defined as CacheItem in NCache. The value of the objects stored in the cache can range from being simple string types to complex objects.
CacheItem class in NCache has properties that enable you to set metadata for the item to be added in cache in an organized way. In scenarios where multiple attributes have to be set while adding an item in cache using CacheItem is preferred.Using CacheItem class object removes the problem of using multiple API overloads on adding/updating data in cache.You can easily use the basic API overload and add/update data easily using CacheItem.
Inheritance
Namespace:
Assembly: Alachisoft.NCache.Client.dll
Syntax
public class CacheItem : ICloneable
Examples
You can create an instance of CacheItem class and
ICache cache = CacheManager.GetCache("demoCache");
object someData = new object();
CacheItem item = new CacheItem(someData);
item.Expiration = new Expiration(ExpirationType.Sliding,TimeSpan.FromMinutes(5));
item.Priority = CacheItemPriority.High;
cache.Add("someData", item);
Constructors
Name | Description |
---|---|
CacheItem(Object) | Initialize new instance of cache item. |
Properties
Name | Description |
---|---|
CreationTime | Specifies when the item was added in cache for the first time. |
Dependency | The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this property contains a null reference. |
Expiration | This property sets Expiration for the cache itme. After the specified timespan,
the item expires from cache.
|
Group | Groups help you create a logical partition of your cached data for easy retrieval. Group information can be added with an item by setting the Group property of CacheItem. This reduces the complication of using API overloads for adding groups at the time of adding/updating item in cache. |
LastModifiedTime | This property of CacheItem stores the last modified time of the cache item. If an item is updated in cache its last modified time is updated as well. Last modified time is checked when Least Recently Used based eviction is triggered. |
NamedTags | With Named Tags user is able to store additional information (of any type) required to query the object stored as string. Users are required to provide the list of named tags, each having two parameters, "key" (name of a tag) as string and "value" (assigned value) as any primitive type. NCache then allows you to search your objects through these named tags. Named tags can be specified by using this property of CacheItem. |
Priority | When the application's cache is full or runs low on memory, the Cache selectively purges items to free system memory. When an item is added to the Cache, you can assign it a relative priority compared to the other items stored in the Cache using this property. This eliminates the problem of using API overloads for setting the priority. Items you assign higher priority values to are less likely to be deleted from the Cache when the server is processing a large number of requests, while items you assign lower priority values are more likely to be deleted. |
ResyncOptions | This property is used to define the ResyncOptions for the cache item. |
SyncDependency | Synchronizes two separate caches so that an item updated or removed from one cache can have the same effect on the synchronized cache. For cache sync dependency, an item must exist in cache before another item can be added with a dependency on it. This property lets you set Cache sync dependency with a cache item. |
Tags | Using Tags, you can associate keywords(s) with your cache items. You can mark your data with tags which act as identifiers for your cache items. Using this property, you can easily set tags for a cache item. |
Version | NCache uses cache item versioning. CacheItemVersion is a property associated with every cache item. It is basically a numeric value that is used to represent the version of the cached item which changes with every update to an item. This property allows you to track whether any change occurred in an item or not. When you fetch an item from cache, you also fetch its current version in the cache. |
Methods
Name | Description |
---|---|
Clone() | Creates a shallow copy of CacheItem |
GetValue<T>() | Returns the value stored in the cache item. |
SetCacheDataNotification(CacheDataNotificationCallback, EventType, EventDataFilter) | You can use this to notify applications when their objects are updated or removed in the cache. Callbacks can be registered against EventType for the key the items is inserted to. Callback are overridden for the same EventType if called again. CacheDataNotificationCallback defines the callback to be used for notifications. EventType describes the type of event you want to register. If that event is triggered, a notification will be received. |
SetValue(Object) | Sets the value of the cache item. |