Using Write-Through with Cache Operations
Note
This feature is only available in NCache Enterprise Edition.
This section will explain the use of Write-Through Provider after configuring and deploying it. NCache supports multiple Write-Through Providers with applications.
Pre-Requisites for Using WriteThrough
- Install the following NuGet package in your application.
- To utilize the APIs, include the following namespaces 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, Insert(), Remove(), IWriteThruProvider.
- 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.
Multiple Write-Through Providers can be configured through NCache. Default provider will be called if a 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.
Add/Update with Write-Through
The following example creates a new cacheItem
and adds this item in the cache with write-through enabled using the Insert() method. Using this method if the item is already present in the cache, the old value will be over-written with the new value.
try
{
// Pre-Condition: Cache is already connected
// Fetch product with the given ProductID
Product product = FetchProductByProductID(1001);
// Specify the key of the item
string key = $"product.ProductID";
product.UnitPrice = 200;
// Create a new cacheItem with the product
var cacheItem = new CacheItem(product);
// Enable write through for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;
// Add the item in the cache with WriteThru enabled
CacheItemVersion itemVersion = cache.Insert(key, cacheItem, writeThruOptions);
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
{
// Backing source is not available
}
else if (ex.ErrorCode == NCacheErrorCodes.SYNCHRONIZATION_WITH_DATASOURCE_FAILED)
{
// Synchronization of data with backing source is failed
// due to any error
}
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
}
Remove an Existing Item with Write-Through
The following example removes an item from the cache with write-through enabled on it using the Remove() method.
try
{
// Pre-Condition: Cache is already connected
// Specify the key of the item
string key = "Product:1001";
// Enable write through for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;
// Remove the item corresponding to the key with write-through enabled
cache.Remove(key, null, null, writeThruOptions);
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.BACKING_SOURCE_NOT_AVAILABLE)
{
// Backing source is not available
}
else if (ex.ErrorCode == NCacheErrorCodes.SYNCHRONIZATION_WITH_DATASOURCE_FAILED)
{
// Synchronization of data with backing source is failed
// due to any error
}
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
}
Additional Resources
NCache provides sample application for write-through on GitHub.
See Also
Write-Through Provider Configuration and Implementation
Read Through Caching
Using Write-Behind with Basic Operations
Configuring Write-Through Provider