• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Class CacheItem

NCache uses a "key" and "value" structure for storing objects in the 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 the cache in an organized way. In scenarios where multiple attributes have to be set while adding an item in the cache, using CacheItem is preferred. Using CacheItem class object removes the problem of using multiple API overloads on adding/updating data in the cache. You can easily use the basic API overload and add/update data easily using CacheItem.

Inheritance
System.Object
CacheItem
Assembly: Alachisoft.NCache.Client.dll
Syntax
public class CacheItem : ICloneable
Examples

The following example shows how you can create an instance of CacheItem class and add it to the ICache

ICache cache = CacheManager.GetCache("demoCache");

Product product = new Product();
CacheItem item = new CacheItem(product);
item.Expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));
item.Priority = CacheItemPriority.High;

cache.Add("productKey", item);

Constructors

CacheItem(Object)

Initialize new instance of cache item.

Declaration
public CacheItem(object value)
Parameters
Type Name Description
System.Object value

Actual object to be stored in cache.

Properties

CreationTime

Specifies when the item was added in the cache for the first time.

Declaration
public DateTime CreationTime { get; }
Property Value
Type Description
System.DateTime

This property specifies the DateTime when the item was first added in the cache.

Examples

The following example prints creation time of a CacheItem.

CacheItem item = cache.GetCacheItem("Product0");
Console.WriteLine("Creation Time: {0}", item.CreationTime.ToString());

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.

Declaration
public CacheDependency Dependency { get; set; }
Property Value
Type Description
CacheDependency

This property tracks cache dependencies, which can be files, directories, or keys to other objects in an application cache.

Examples

The following example sets a key dependency of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

item.Dependency = new KeyDependency("Product1");

Expiration

This property sets Expiration for the cache item. After the specified timespan, the item expires from cache. If expiration is not set then it is disabled.

Declaration
public Expiration Expiration { get; set; }
Property Value
Type Description
Expiration

This property specifies the time after which the message is expired from the cache.

Examples

The following example sets the sliding expiration of a CacheItem as 5 minutes.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

item.Expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));

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 the cache.

Declaration
public string Group { get; set; }
Property Value
Type Description
System.String

Name of a group.

Examples

The following example sets group of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);
item.Group = "group_name";

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.

Declaration
public DateTime LastModifiedTime { get; }
Property Value
Type Description
System.DateTime

This property specifies the last modification time of the cache.

Examples

The following example prints creation time of a CacheItem.

CacheItem item = cache.GetCacheItem("Product0");
Console.WriteLine("Last Modified Time: {0}", item.LastModifiedTime.ToString());

NamedTags

With Named Tags, the 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.

Declaration
public NamedTagsDictionary NamedTags { get; set; }
Property Value
Type Description
NamedTagsDictionary

Represents a dictionary so that items can be grouped together and retrieved efficiently.

Examples

The following example sets named tags of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

NamedTagsDictionary tags = new NamedTagsDictionary();
tags.Add("tag1", "alpha");
tags.Add("tag2", "beta");

item.NamedTags = tags;

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 that are assigned higher priority values are less likely to be deleted from the cache when the server is processing a large number of requests, while items assigned lower priority values are more likely to be deleted.

Declaration
public CacheItemPriority Priority { get; set; }
Property Value
Type Description
CacheItemPriority

The default value is CacheItemPriority.Default.

Remarks

This property will be used only when the eviction policy is set to priority in the configuration.

Examples

The following example sets CacheItemPriority of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);
item.Priority = CacheItemPriority.Normal;

ResyncOptions

This property is used to define the ResyncOptions for the cache item.

Declaration
public ResyncOptions ResyncOptions { get; set; }
Property Value
Type Description
ResyncOptions

This property is used to define the ResyncOptions for the cache item.

Examples

The following example sets ResyncOptions of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);
item.ResyncOptions = new ResyncOptions(true, "ProdProvider");

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 the cache sync dependency with a cache item.

Declaration
public CacheSyncDependency SyncDependency { get; set; }
Property Value
Type Description
CacheSyncDependency

This property keeps the items present in one cache synchronized with the items present in another cache.

Examples

The following example sets a CacheSyncDependency dependency of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

item.SyncDependency = new CacheSyncDependency("partitionedCache", "Product0");

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.

Declaration
public Tag[] Tags { get; set; }
Property Value
Type Description
Tag[]

This property associates keywords with your cache items so that they are logically grouped together and can be retrieved efficiently.

Examples

The following example sets multiple Tag of a CacheItem.

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

Tag[] tags = new Tag[2];
tags[0] = new Tag("alpha");
tags[1] = new Tag("beta");

item.Tags = tags;

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 from the cache.

Declaration
public CacheItemVersion Version { get; set; }
Property Value
Type Description
CacheItemVersion

A numeric value used to represent the version of the cached item.

Examples

The following example gets CacheItemVersion of a CacheItem.

CacheItem item = cache.GetCacheItem("Product0");
Console.WriteLine("Version: {0}", item.Version.Version);

Methods

Clone()

Creates a shallow copy of the cache item.

Declaration
public virtual object Clone()
Returns
Type Description
System.Object

A shallow copy of the cache item.

GetValue<T>()

Returns the value stored in the CacheItem.

Declaration
public T GetValue<T>()
Returns
Type Description
T

Value of the cache item with the type defined.

Type Parameters
Name Description
T

Specifies the type of value obtained from the cache item.

Remarks

This value must be serializable, otherwise System.ArgumentException is thrown, when you will try to add or insert the CacheItem.

Examples

The following example gets value of a CacheItem.

CacheItem item = cache.GetCacheItem("Product0");

Product product = item.GetValue<Product>();

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. Callbacks 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.

Declaration
public void SetCacheDataNotification(CacheDataNotificationCallback callback, EventType eventType, EventDataFilter datafilter = EventDataFilter.None)
Parameters
Type Name Description
CacheDataNotificationCallback callback

Callback to be raised when an item is updated or removed.

EventType eventType

EventType the callback is registered against.

EventDataFilter datafilter

Tells whether to receive metadata, data with metadata or none when a notification is triggered.

Examples

The following example sets item notification callback of a CacheItem.

private void DataModifiedCallback(string key, CacheEventArgs args)
{
   //
}

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

CacheItem item = new CacheItem(product);

item.SetCacheDataNotification(new CacheDataNotificationCallback(DataModifiedCallback), EventType.ItemAdded, EventDataFilter.DataWithMetadata);

SetValue(Object)

Sets the value of the cache item.

Declaration
public void SetValue(object value)
Parameters
Type Name Description
System.Object value

Object to be stored in the CacheItem.

Examples

The following example sets value of a CacheItem.

CacheItem item = cache.GetCacheItem("productKey");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

item.SetValue(product);

cache.Insert("productKey", item);

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Community
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top