Method GetCacheItem
GetCacheItem(String, ReadThruOptions)
Retrieves the specified CacheItem from the cache object. This overload also allows specifying the ReadThruOptions. If read-through is set and the object does not exist in the cache, the object will be fetched from the data source and added to the cache.
Declaration
CacheItem GetCacheItem(string key, ReadThruOptions readThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique identifier for the cache item to be retrieved. |
ReadThruOptions | readThruOptions | ReadThruOptions regarding reading from data source. It can be either ReadThru, ReadThruForced or none. |
Returns
Type | Description |
---|---|
CacheItem | The specified CacheItem. If the key does not exist, it returns a null reference. |
Examples
The following example demonstrates how to retrieve the cache item with ReadThruOptions and CacheItemVersion.
ICache cache = CacheManager.GetCache("demoClusteredCache");
string key = "Product0";
ReadThruOptions readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
CacheItem cacheItem = cache.GetCacheItem(key, readThruOptions);
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Key contains a null reference. |
GetCacheItem(String, Boolean, TimeSpan, ref LockHandle)
Retrieves the cache item from the cache and locks it if it is not already locked. If the item is already locked, it returns null. This differs from the basic GetCachItem operation, which returns an item (while ignoring the lock). If you're using the NCache item locking features, use this approach.
Declaration
CacheItem GetCacheItem(string key, bool acquireLock, TimeSpan lockTimeout, ref LockHandle lockHandle)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Key used to reference the desired object. |
System.Boolean | acquireLock | A flag to determine whether to acquire a lock or not. |
System.TimeSpan | lockTimeout | The TimeSpan after which the lock is automatically released. |
LockHandle | lockHandle | An instance of LockHandle to hold the lock information. |
Returns
Type | Description |
---|---|
CacheItem | The retrieved cache item. If key is not found, a null reference. |
Examples
The following example demonstrates how to retrieve the cache item with lock handle, timeout and flag for aquiring a lock.
ICache cache = CacheManager.GetCache("demoClusteredCache");
string key = "Product0";
LockHandle lockHandle = new LockHandle();
CacheItem item = cache.GetCacheItem(key, true, TimeSpan.FromSeconds(30), ref lockHandle);
GetCacheItem(String, ref CacheItemVersion, ReadThruOptions)
Retrieves the specified CacheItem from the cache object. This overload also allows specifying the ReadThruOptions. If read-through is set and the object does not exist in the cache, the object will be fetched from the data source and added to the cache. It accepts the CacheItemVersion by reference.
If null is passed for CacheItemVersion, then the version of the object from the cache is returned. If non-null CacheItemVersion is passed, then object is returned from the cache only if that is the current version of the object in the cache.
Declaration
CacheItem GetCacheItem(string key, ref CacheItemVersion version, ReadThruOptions readThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique identifier for the cache item to be retrieved. |
CacheItemVersion | version | The CacheItemVersion of the object. |
ReadThruOptions | readThruOptions | Options regarding reading from data source. It can be either ReadThru or none. |
Returns
Type | Description |
---|---|
CacheItem | The specified CacheItem. If the key does not exist, it returns a null reference. |
Examples
The following example demonstrates how to retrieve the CacheItem with ReadThruOptions and CacheItemVersion.
ICache cache = CacheManager.GetCache("demoClusteredCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
string key = "Product0";
CacheItemVersion version = cache.Add(key, product);
ReadThruOptions readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
CacheItem item = cache.GetCacheItem(key, ref version, readThruOptions);
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|