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 a null value is returned.
Examples
The following example demonstrates how to retrieve the value from the cache.
ICache cache = CacheManager.GetCache("demoClusteredCache");
string key = "Product0";
ReadThruOptions readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
Product product = cache.Get<Product>(key,readThruOptions);
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Key contains a null reference. |
System.ArgumentException | Key is an empty string. |
Get<T>(String, Boolean, TimeSpan, ref LockHandle)
Retrieves the specified object from the cache and locks it simulataneously, if it is not already locked. It returns null, if the object is already locked. This is different from the basic Get operation, where an object is returned ignoring the lock altogether. Use this method if you're using NCache item locking features.
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. |
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 |
---|---|
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
The following 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);
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
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
The following example demonstrates how to retrieve the value cached with ReadThruOptions 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);