Method GetCacheItem
GetCacheItem(String, ReadThruOptions)
Retrieves the specified CacheItem from the Cache object. This overload also allows specifying the read-through option. 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
Example demonstrates how to retrieve the cache item with read thru option and cache item version
ICache cache = CacheManager.GetCache("myCache");
string key = "Product0";
ReadThruOptions readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
CacheItem cacheItem = cache.GetCacheItem(key, readThruOptions);
GetCacheItem(String, Boolean, TimeSpan, ref LockHandle)
Get the cache item stored in cache. Loack handle can be given with this and a flag can be set if you want to acquire lock.
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. |
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
Example demonstrates how to retrieve cache item with lock handle, timeout and flag box server control.
ICache cache = CacheManager.GetCache("myCache");
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 read-through option. 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
Example demonstrates how to retrieve the cache item with read thru option and cache item version
ICache cache = CacheManager.GetCache("myCache");
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);