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

Read-Through Usage with Cache Operation

Note

This feature is only available in NCache Enterprise Edition.

This section will explain the use of Read-Through provider after configuring and deploying it. NCache supports multiple Read-Through providers with an application.

NCache provides Alachisoft.NCache.Client.DSReadOption enum to specify Read thru option in APIs.

Multiple Read-Through providers can be configured through NCache. Default Read-Through provider will be called if specific provider name is not mentioned through API. You can also use providers other than default by using provider specific overloads of APIs.

Important

For Java, before deploying your JAR files, you need to make sure that:

  • You have JDK 11 installed.
  • Your Environment Variable for Java is set.

Pre-Requisites

  • .NET/.NET Core
  • Java
  • Install the following NuGet package in your application.
    • Alachisoft.NCache.SDK
  • To utilize the APIs, include the following namespace in your application:
    • Alachisoft.NCache.Runtime.DatasourceProviders
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Exceptions
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details refer to: CacheItem, Get, ReadMode, GetBulk , CacheConnectionOptions.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <artifactId>ncache-client</artifactId>
    <version>5.2.0</version>
</dependency>

-Import the following packages in your application:

  • import com.alachisoft.ncache.client.*
  • import com.alachisoft.ncache.runtime.exceptions.*
  • import com.alachisoft.ncache.runtime.datasourceprovider.*
    • The application must be connected to cache before performing the operation.
    • Cache must be running.
    • For API details refer to: CacheItem , get, getBulk.
    • Make sure that the data being added is serializable.
    • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
    • To handle any unseen exceptions, refer to the Troubleshooting section.

Using Get Method

The following example retrieves an item with read-through enabled, corresponding to the specified key "Product:1001" using the Get<> method.

  • .NET/.NET Core
  • Java
try
{
  // Pre-condition: Cache is already connected

  // Specify the key of the item
  string key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions();
  readThruOptions.Mode = ReadMode.ReadThru;

  // Retrieve the data of the corresponding item with reads thru enabled
  Product data = cache.Get<Product>(key, readThruOptions);

  if (data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex)
{
  if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
  // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
  // Pre-condition: Cache is already connected
  // Specify the key of the item
  String key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
  readThruOptions.setReadMode(ReadMode.ReadThru);

  // Retrieve the data of the corresponding item with reads thru enabled
  Product data = cache.get(key,readThruOptions,Product.class);
  if (data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex)
{
  if (ex.getErrorCode() == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE) 
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
  // Any generic exception like NullPointerException or IllegalArgumentException
}

Using ForcedRead-Through

NCache provides with an option of Forced Read-Through which forces NCache to get the data from backing source forcefully. It means that the data will not be looked upon for in the cache and will directly be fetched from the backing source.

You can enable forced read-through by specifying the ReadMode as ReadThruForced.

The following example gets an item from the cache using the ReadThruForced option by forcefully enabling read-through on it.

  • .NET/.NET Core
  • Java
try
{
  // Pre-condition: Cache is already connected

  // Specify the key of the item
  string key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions();
  readThruOptions.Mode = ReadMode.ReadThruForced;

  // Retrieve the data of the corresponding item with reads thru enabled
  Product data = cache.Get<Product>(key, readThruOptions);

  if (data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex)
{
  if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
  // Pre-condition: Cache is already connected
  // Specify the key of the item
  String key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions(ReadMode.ReadThruForced);
  readThruOptions.setReadMode(ReadMode.ReadThruForced);

  // Retrieve the data of the corresponding item with reads thru enabled
  Product data = cache.get(key,readThruOptions,Product.class);
  if (data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex) 
{
  if (ex.getErrorCode() == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE) 
  {
    // Backing source is not available
  }
  else 
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
  // Any generic exception like NullPointerException or IllegalArgumentException
}

Using Read-Through with Bulk Operations

For better understanding of the these operations review Bulk Operations.

The following example retrieves a dictionary of products corresponding to the keys specified with read-through enabled using the GetBulk method.

  • .NET/.NET Core
  • Java
try
{
  // Pre-Condition: Cache is already connected

  // Create a new array of keys 
  String[] keys = new string[4];
  keys[1] = "Product:1001";
  keys[2] = "Product:1002";
  keys[3] = "Product:1003";
  keys[4] = "Product:1004";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions();
  readThruOptions.Mode = ReadMode.ReadThru;

  // Retrieve the dictionary of Products with corresponding products
  IDictionary<string, Product> retrievedItems = cache.GetBulk<Product>(keys, readThruOptions);

  // IDictionary contains cached keys and values
}
catch (OperationFailedException ex)
{
  if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
  // Pre-Condition: Cache is already connected

  // Create a new array of keys
  List<String> keys = new ArrayList<>();

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
  readThruOptions.setReadMode(ReadMode.ReadThru);

  // Retrieve the dictionary of Products using Map with corresponding products
  Map<String, Product> retrievedItems = cache.getBulk(keys,readThruOptions,Product.class);
}
catch (OperationFailedException ex) 
{
  if (ex.getErrorCode() == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE) 
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
  // Any generic exception like NullPointerException or IllegalArgumentException
}

Using Read-Through with CacheItem

The following example retrieves a CacheItem with read-through enabled corresponding to the key specified.

  • .NET/.NET Core
  • Java
try
{
  // Pre-condition: Cache is already connected

  // Specify the key of the item
  string key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions();
  readThruOptions.Mode = ReadMode.ReadThru;

  // Retrieve the data of the corresponding item with reads thru enabled
  CacheItem data = cache.GetCacheItem(key, readThruOptions);

  if (data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex)
{
  if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
  {
    // Backing source is not available
  }
  else
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
  // Pre-condition: Cache is already connected
  // Specify the key of the item
  String key = "Product:1001";

  // Specify the readThruOptions for read through operations
  var readThruOptions = new ReadThruOptions(ReadMode.ReadThru);
  readThruOptions.setReadMode(ReadMode.ReadThru);

  // Retrieve the data of the corresponding item with reads thru enabled
  CacheItem data = cache.getCacheItem(key,readThruOptions);

  if(data != null)
  {
    // Perform operations accordingly
  }
}
catch (OperationFailedException ex) 
{
  if (ex.getErrorCode() == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE) 
  {
    // Backing source is not available
  }
  else 
  {
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
  }
}
catch (Exception ex) 
{
  // Any generic exception like NullPointerException or IllegalArgumentException
}

You can specify the default provider through NCache Web Manager or through client.ncconf file placed in the config folder of the NCache installation directory. If the provider name is not provided in both the API and client.ncconf, the default provider will automatically be used.

<cacheid="mycache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True">
  ...
</cache>

CacheConnectionOptions can also be used to specify providers. NCache logs errors/exceptions in cache logs in case of an exception during loading the provided assemblies.

Additional Resources

NCache provides sample application for read-through on GitHub.

See Also

Implement Read-Through Provider
Read-Through Provider Configuration and Implementation
Write-Through Caching
Cache Startup Loader and Refresher
Custom Cache Dependencies

Back to top Copyright © 2017 Alachisoft