Try Playground
Show / Hide Table of Contents

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

key contains a null reference.

In This Article
  • GetCacheItem(String, ReadThruOptions)
  • GetCacheItem(String, Boolean, TimeSpan, ref LockHandle)
  • GetCacheItem(String, ref CacheItemVersion, ReadThruOptions)

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
� Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top