• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Using Write-through

This section explains the use of the Write-through provider after configuring and deploying it. NCache provides the WriteThruOptions class to specify WriteThru options in APIs. This class allows you to set the following two modes:

  1. WriteThru updates data in the data source synchronously after updating the cache-store.
  2. WriteBehind updates data in the data source asynchronously after updating the cache-store.

Multiple Write-through providers can be configured through NCache. Default Write-through 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.

Prerequisites

  • .NET
  • Legacy API
  • To learn about the standard prerequisites required to work with all NCache server-side features please refer to the given page on Server Side API Prerequisites.
  • For API details refer to: ICache, CacheItem, Get, Insert, InsertAsync, Remove, RemoveAsync, IWriteThruProvider.
  • Install either of the following NuGet packages in your .NET client application:
    • Enterprise: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
    • Professional: Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Web.Caching and Alachisoft.NCache.Runtime.DatasourceProviders namespaces in your application.

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.

  • .NET
  • Legacy API
// Precondition: Cache is already connected

// Fetch product with the given ProductID
Product product = FetchProductByProductID(1001);

// Specify the key of the item
string key = $"Product:{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);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected

// Fetch product with the given ProductID
Product product = FetchProductByProductID(1001);

// Specify the key of the item
string key = $"Product:{product.ProductID}";
product.UnitPrice = 200;

// Create a new cacheItem with the product
var cacheItem = new CacheItem(product);

// Add the item in the cache with WriteThru enabled
CacheItemVersion itemVersion = cache.Insert(key, cacheItem, DSWriteOption.WriteThru, null);
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Bulk Operations

Add/Update Bulk Items

The following example adds bulk items in the cache with Write-through enabled, using the InsertBulk() method. If the items are already present in the cache, new values overwrite the existing values.

  • .NET
// Precondition: Cache is already connected

// Fetch all products from database
Product[] products = FetchProductsFromDB();

var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;

IDictionary<string, CacheItem> dictionary = new Dictionary<string, CacheItem>();
foreach(var product in products)
{
  string key = $"Product:{product.ProductID}";
  var cacheItem = new CacheItem(product);
  dictionary.Add(key, cacheItem);
}

IDictionary<string, Exception> keysFailedToUpdate = cache.InsertBulk(dictionary, writeThruOptions);

Remove Items in Bulk

The following example removes a bulk of items from the cache with Write-behind enabled, using the RemoveBulk() method.

  • .NET
// Precondition: Cache is already connected

// Get Keys
Product[] products = FetchProductsFromDB();

// Specify keys to remove from cache
string[] keys = new string[products.Length];
int index = 0;

foreach (var product in products)
{
  keys[index] = $"Product:{product.ProductID}";
  index++;
}

// Create dictionary to store removed items
IDictionary<string, Product> removedItems = new Dictionary<string,Product>();

// Configure write thru
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;

// Remove items with write-behind enabled
cache.RemoveBulk(keys, out removedItems, writeThruOptions);
Note

Use the method OnDataSourceItemsRemoved() to perform operations after data removal.

Remove an Existing Item

The following example removes an item from the cache with Write-through enabled, using the Remove() method.

  • .NET
  • Legacy API
// Precondition: 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);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected

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

// Remove the item corresponding to the key with Write-through enabled
Object returnObject = cache.Remove(key, DSWriteOption.WriteThru, null);

Asynchronous Operations

Add/Update Item

The following example adds an item asynchronously in the cache with Write-through enabled, using the InsertAsync() method.

  • .NET
// Precondition: Cache is already connected

// Get product from database against given product ID
Product product = FetchProductFromDB(1001);

// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";

var cacheItem = new CacheItem(product);

// Enable write behind for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;

// Add Product object to cache
Task task = cache.InsertAsync(key, cacheItem, writeThruOptions);

Remove Item

The following example removes existing items asynchronously from the cache with Write-through enabled, using the RemoveAsync() method.

  • .NET
// Precondition: Cache is already connected

// Unique cache key of product to remove
string key = "Product:1001";

// Enable write behind for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteThru;

// Asynchronously remove items from cache
Task<Product> task = cache.RemoveAsync<Product>(key, writeThruOptions);

Use Data Structures

The following example uses different data structures with Write-through enabled, on the specified item.

  • .NET
// Precondition: Cache is already connected

// Specify the key of the item
string key = "Product:1001";
var dataTypeAttributes = new DataTypeAttributes();
var writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, WriteThruProviderName);

switch(mainMenu)
{
  case mainMenu.GetDistributedCounter:
    // Modify or add count of the corresponding item with write thru enabled
    var distributedCounter = cache.DataTypeManager.CreateCounter("counter", dataTypeAttributes, 0, writeThruOptions);

    // Perform operations on counter
    break;
  case mainMenu.GetDistributedDictionary:
    // Modify or add dictionary of the corresponding item with write thru enabled
    var distributedDictionary = cache.DataTypeManager.CreateDictionary<string, int>(key, dataTypeAttributes, writeThruOptions);

    // Perform operations on dictionary
    break;
  case mainMenu.GetDistributedList:
    // Modify or add the list of the corresponding item with write thru enabled
    var distributedList = cache.DataTypeManager.CreateList<int>("list", dataTypeAttributes, writeThruOptions);

    // Perform operations on list
    break;
  case mainMenu.GetDistributedQueue:
    // Modify or add the queue of the corresponding item with read thru enabled
    var distributedQueue = cache.DataTypeManager.CreateQueue<int>("queue", dataTypeAttributes, writeThruOptions);

    // Perform operations on queue
    break;
  case mainMenu.GetDistributedHashSet:
    // Modify or add the HashSet of the corresponding item with read thru enabled
    var distributedHashSet = cache.DataTypeManager.CreateHashSet<int>("hashset", dataTypeAttributes, writeThruOptions);

    // perform operations on hashset
    break;
}

Additional Resources

NCache provides sample application for Write-through on GitHub.

See Also

.NET: Alachisoft.NCache.Runtime namespace.

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Community
  • 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 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top