Retrieve Existing Cache Data
While NCache also supports querying the cache data, it utilizes the key-value architecture of its store to maximize the ways of data retrieval. You can retrieve a custom object, CacheItem, or bulk items from the cache using the unique key associated with the item. NCache provides various overloads of the Get
method to fetch items from the cache data based on the specified key.
Prerequisites to Retrieve Existing Cache Data
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, CacheItem, GetCacheItem, Get, GetList, GetBulk, GetCacheItemBulk.
Retrieve Cache Data
Note
This feature is also available in NCache Professional.
You can fetch a custom object from the cache data using various overloads of the Get
method by specifying the key of the cache item. The object is retrieved as a template, so it needs to be type-cast accordingly if it is a custom class object.
Important
If the key does not exist in the cache, a null value is returned.
The following example fetches an existing item of the Customer class with the specified key. Keep in mind that Get
returns a template that needs to be cast accordingly, so the object is cast to the Customer
type in this example.
// Precondition: Cache is already connected
string customerKey = "Customer:ALFKI";
// Retrieve the Customer object
Customer customer = cache.Get<Customer>(customerKey);
if (customer != null)
{
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Retrieve CacheItem From Cache
Note
This feature is also available in NCache Professional.
NCache allows retrieving an existing CacheItem
from the cache using the dedicated GetCacheItem
API. This returns a CacheItem
associated with the specified key. Its Value
property encapsulates the data.
The following example retrieves an existing CacheItem
with specified key and checks if the result is of Customer type and type casts it accordingly.
string customerKey = "Customer:ALFKI";
// Retrieve the CacheItem
CacheItem retrievedCacheItem = cache.GetCacheItem(customerKey);
if (retrievedCacheItem != null)
{
Customer customer = retrievedCacheItem.GetValue<Customer>();
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
Retrieve Bulk Items From Cache
Note
This feature is also available in NCache Professional.
NCache allows synchronous bulk retrieval of items in a single call to reduce network costs. Various overloads of the GetBulk
method retrieve objects from the cache data for the cache keys specified. Note that the value is retrieved as a template so it needs to be type cast accordingly if it is a custom class object.
Important
If the keys exist in the cache, a dictionary of cache items and their keys is returned.
The following example retrieves existing CacheItems containing objects of the Customer class. The result is returned in an IDictionary
of keys and values, which can be enumerated to get the actual values of the keys. Since GetBulk
returns a template (that needs to be cast), the object is cast to the Customer
type in this example. If the specified key does not exist in the cache, a null value is returned.
// Create an array of all keys to fetch
String[] keys = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT",
"Customer:BERGS"
};
// Get items from cache
IDictionary<string, Customer> retrievedItems = cache.GetBulk<Customer>(keys);
// Retrieve customers and their addresses from dictionary
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedItems)
{
Console.WriteLine($"Customer: {retrievedItem.Value.ContactName}, Address : { retrievedItem.Value.Address}");
}
Retrieve Bulk of CacheItem's From Cache
Note
This feature is also available in NCache Professional.
You can also retrieve a bulk of CacheItem
using the GetCacheItemBulk
method. Here is an example:
// Create an array of all keys to fetch
String[] keys = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT",
"Customer:BERGS"
};
// Get items from cache
IDictionary<string, Customer> retrievedItems = cache.GetBulk<Customer>(keys);
// Retrieve customers and their addresses from dictionary
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedItems)
{
Console.WriteLine($"Customer: {retrievedItem.Value.ContactName}, Address : { retrievedItem.Value.Address}");
}
Additional Resources
NCache provides a sample application for Basic Operations on GitHub.
See Also
.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Python: ncache.client class.
Node.js: Cache class.