Method Get
Get<T>(String, ReadThruOptions)
Retrieves the specified item from the Cache object, with read-through caching option available. If the option of read-through has been set, the object will be fetched from the data source if it does not exist in cache.
Declaration
T Get<T>(string key, ReadThruOptions readThruOptions = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The unique identifier for the cache item to be retrieved. |
ReadThruOptions | readThruOptions | ReadThruOptions to read from data source. These can be either ReadThru, ReadThruForced or None. |
Returns
Type | Description |
---|---|
T | The retrieved cache item, or a null reference if the key is not found. |
Type Parameters
Name | Description |
---|---|
T | Specifies the type of value obtained from the cache. |
Remarks
If the key does not exists in the cache then null is returned.
Examples
Example demonstrates how to retrieve the value from cache
ICache cache = CacheManager.GetCache("demoClusteredCache");
string key = "Product0";
ReadThruOptions readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
Product product = cache.Get<Product>(key,readThruOptions);
Get<T>(String, Boolean, TimeSpan, ref LockHandle)
Retrieves the specified item from the Cache if it is not already locked. Otherwise it returns null. This is different from the basic Get operation where an item is returned ignoring the lock altogether.
Declaration
T Get<T>(string key, bool acquireLock, TimeSpan lockTimeout, ref LockHandle lockHandle)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Unique identifier for the cache item to be retrieved. |
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 |
---|---|
T | The retrieved cache item, or a null reference if the key is not found. |
Type Parameters
Name | Description |
---|---|
T | Specifies the type of value obtained from the cache. |
Examples
Example demonstrates how to retrieve the cached value and acquire a lock at the same time for minutes.
ICache cache = CacheManager.GetCache("demoCache");
Product product = new Product();
product.Id = 1;
product.Name = "Chai";
string key = "Product0";
cache.Add(key, product);
LockHandle lockHandle = new LockHandle();
object cachedItem = cache.Get<Product>(key, true, new TimeSpan(0, 2, 0), ref lockHandle);
Get<T>(String, ref CacheItemVersion, ReadThruOptions)
Retrieves the specified item from the Cache object, with read-through caching option available. If the option of read-through has been set, the object will be fetched from the data source if it does not exist in 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
T Get<T>(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 version of the object. |
ReadThruOptions | readThruOptions | ReadThruOptions to read from data source. These can be either ReadThru, ReadThruForced or None. |
Returns
Type | Description |
---|---|
T | The retrieved cache item, or a null reference if the key is not found. |
Type Parameters
Name | Description |
---|---|
T | Specifies the type of value obtained from the cache. |
Examples
Example demonstrates how to retrieve the value cached with read thru option and version
ICache cache = CacheManager.GetCache("demoCache");
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);
Product product = cache.Get<Product>(key,ref version, readThruOptions);