CacheItem

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.


Constructor

# new CacheItem(value, type)

Initialize new instance of cache item.

Parameters:
Name Type Default Description
value Any null

Actual object to be stored in cache.

type JsonDataType null
Example
const ncache  = require('ncache-client');

//Initialize the Cache Cluster 
let cache = await ncache.CacheManager.getCache("test-Cache");

//Create new CacheItem
let cacheItem = new ncache.CacheItem("Value");

//set cache item expiration
let timeSpan = new ncache.TimeSpan(0, 0, 0, 1, 0, 0);
let expiration = new ncache.Expiration(ExpirationType.Absolute, timeSpan);
cacheItem.setExpiration(expiration);

//set cacahe item Priority
cacheItem.setCacheItemPriority(CacheItemPriority.High);

//Add cache item in Cache 
await cache.add('key', cacheItem);

//close the cache
await cache.close();

Methods

# addCacheDataNotificationListener(listener, eventType, datafilter)

You can use this to notify applications when their objects are updated or removed in the cache. Listeners can be registered against event type/types for the key the items is inserted to. Listeners are overridden for the same event type/types if called again.

Parameters:
Name Type Description
listener CacheDataModificationListener

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

eventType Array

EventType the callback is registered against

datafilter number

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

# getCacheItemPriority() → {CacheItemPriority}

Gets the relative priority for cache item which is kept under consideration whenever cache starts to free up space and evicts items.

Returns:

CacheItemPriority associated with cache item.

Type
CacheItemPriority

# getCacheItemVersion() → {CacheItemVersion}

Gets the version associated with the cache item.Version is basically a numeric value that is used to represent the version of the cached item which changes with every update to an item.

Returns:

The version associated with the cache item.

Type
CacheItemVersion

# getCreationTime() → {Date}

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

Returns:

The date of creation of the cache item.

Type
Date

# getDependency() → {CacheDependency}

Gets the Cache Dependency instance that contains all dependencies associated with cache item.

Returns:

The Cache Dependency instance associated with cache item.

Type
CacheDependency

# getExpiration() → {Expiration}

Gets the expiration mechanism for cache item.

Returns:

instance that contains info about cache item expiration mechanism.

Type
Expiration

# getGroup() → {string}

Gets the group associated with the cache item. It can be queryed on the basis of Groups.

Returns:

The group associated with cache item.

Type
string

# getLastModifiedTime() → {Date}

Gets the lastModifiedTime of the cache item.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.

Returns:

The last modification time of the cache item.

Type
Date

# getNamedTags() → {NamedTagsDictionary}

Gets the NamedTags information associated with the cache item, it can be queried on the basis of NamedTags provided.

Returns:

NamedTags associated with cache item.

Type
NamedTagsDictionary

# getResyncOptions() → {ResyncOptions}

Gets the ResyncOptions specific to the cache item.

Returns:

ResyncOptions specific to the cache item.

Type
ResyncOptions

# getTags() → {Array.<Tag>}

Gets the tags information associated with the cache item, it can be queried on the basis of Tags provided.

Returns:

List of Tags associated with cache item.

Type
Array.<Tag>

# getValue(classOb)

Returns the value stored in the cache item.

Parameters:
Name Type Description
classOb JsonDataType

Specifies the type of value obtained from the cacheItem.

Returns:

Object

# removeCacheDataNotificationListener(listener, eventEnumSet)

Unregisters the user from cache data modification notifications.

Parameters:
Name Type Description
listener CacheDataModificationListener

The listener that is registered for notifications.

eventEnumSet Array

Event type/types the listener is unregistered against.

# setCacheItemPriority(value)

Sets the CacheItemPriority of the cache item.

Parameters:
Name Type Description
value number

Sets the CacheItemPriority of the cache item.

# setCacheItemVersion(value)

Sets the CacheItemVersion of the cache item.

Parameters:
Name Type Description
value CacheItemVersion

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.

# setDependency(value)

sets CacheDependency of CacheItem

Parameters:
Name Type Description
value CacheDependency

sets CacheDependency of CacheItem

# setExpiration(value)

Sets the Expiration of the cache item.

Parameters:
Name Type Description
value Expiration

This property sets Expiration for the cache itme. After the specified timespan, the item expires from cache.

# setGroup(value)

Sets the group associated with the cache item. It can be queryed on the basis of Groups.

Parameters:
Name Type Description
value string

The group to be associated with cache item.

# setNamedTags(value)

sets NamedTags of CacheItem

Parameters:
Name Type Description
value NamedTagsDictionary

NCache then allows you to search your objects through these named tags. Named tags can be specified by using this property of CacheItem.

# setResyncOptions(value)

sets ResyncOption of CacheItem

Parameters:
Name Type Description
value ResyncOptions

sets ResyncOptions of a cache item

# setTags(value)

Sets the tags information associated with the cache item, it can be queried on the basis of Tags provided.

Parameters:
Name Type Description
value Array.<Tag>

List of Tags to be associated with cache item.

# setValue(value, type)

Sets the value of the cache item.

Parameters:
Name Type Default Description
value Any

Actual object to be stored in cache.

type string null