• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Method Get

Get<T>(String, ReadThruOptions)

Retrieves the specified object from the Cache, with ReadThruOptions available. If the option of ReadThru 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 object, or a null 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("demoCache");

string key = "Product0";F

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 if it is not already locked. Otherwise it returns null. This is different from the basic Get operation where an object 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.

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 object, or a null 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);
Exceptions
Type Condition
System.ArgumentNullException

key contains a null reference.

System.ArgumentException

key is an empty string.

Get<T>(String, ref CacheItemVersion, ReadThruOptions)

Retrieves the specified object from the Cache, with ReadThruOptions available. In case the option of ReadThru 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.

In case a 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 object, or a null 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 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);
Back to top Copyright © 2017 Alachisoft