使用后写式缓存
本节介绍在部署和配置 Write-Through 提供程序后如何使用 Write-Behind 模式。 Write-Behind 在更新缓存存储后异步更新数据源。
重要
对于 Java,在部署 JAR 文件之前,您需要确保:
- JDK 17 安装。
- Java 的环境变量已设置。
先决条件
使用后写添加/更新
您可以在启用 Write-Behind 的情况下在缓存中添加/更新带有或不带有回调的缓存项。 添加回调会通知您所注册的事件类型。
添加无回调方法
以下示例在启用 Write-Behind 的情况下在缓存中添加一个项目,使用 Insert()
方法。 使用此方法,如果缓存中已存在项目,则新值将覆盖旧值。
// Pre-Condition: Cache is already connected
// Specify the key of the cacheItem
Product product = FetchProductByProductID(1001);
string key = $"product:ProductID";
var cacheItem = new CacheItem(product);
// Enable write through for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
// Add item in the cache with write-behind
cache.Insert(key, cacheItem, writeThruOptions);
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
使用回调方法添加
以下示例在缓存中添加一个已注册回调且事件类型为的项目 ItemAdded
启用后写。
// Specify the key of the cacheItem
Product product = FetchProductByProductID(1001);
string key = $"product:ProductID";
var cacheItem = new CacheItem(product);
// Enable write through for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemAdded);
// Add item in the cache with write-behind
cache.Insert(key, cacheItem, writeThruOptions);
使用后写删除现有数据
以下示例在启用 Write-Behind 的情况下从缓存中删除项目,使用 Remove
方法,对应于提供的键以及来自数据源的键,并为操作注册事件。
// Specify the key of the item
string key = "Product:1001";
// Enable write through for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemRemoved);
// Remove the item corresponding to the key with write-through enabled
cache.Remove(key, null, null, writeThruOptions);
批量操作
使用后写添加/更新批量项目
以下示例在启用 Write-Behind 的情况下在缓存中添加批量项目,使用 InsertBulk()
方法。 如果项目已存在于缓存中,则新值将覆盖现有值。
// Fetch all products from database
Product[] products = FetchProductsFromDB();
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemAdded);
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemUpdated);
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);
使用 Write-Behind 删除现有项目
以下示例在启用 Write-Behind 的情况下从缓存中删除大量项目,使用 RemoveBulk()
方法。
// 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>();
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemRemoved);
// Remove items with write-behind enabled
cache.RemoveBulk(keys, out removedItems, writeThruOptions);
备注
使用方法 OnDataSourceItemsRemoved()
从缓存中删除数据后执行操作。
异步操作
使用后写添加/更新项目
下面的示例在启用 Write-Behind 的情况下在缓存中异步添加项目,使用 InsertAsync()
方法。
// 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.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemAdded);
// Add Product object to cache
Task task = cache.InsertAsync(key, cacheItem, writeThruOptions);
删除带有后写功能的项目
以下示例在启用 Write-Behind 的情况下从缓存中异步删除现有项目,使用 RemoveAsync()
方法。
// Unique cache key of product to remove
string key = $"Product:{product.ProductID}";
// Enable write behind for the cacheItem created
var writeThruOptions = new WriteThruOptions();
writeThruOptions.Mode = WriteMode.WriteBehind;
writeThruOptions.SetDataSourceNotification(DataSourceModifiedCallBack, EventType.ItemRemoved);
// Asynchronously remove items from cache
Task<Product> task = cache.RemoveAsync<Product>(key, writeThruOptions);
使用带有后写功能的数据结构
以下示例在指定项目上使用启用了 Write-Behind 的不同数据结构。
// Specify the key of the item
string key = "Product:1001";
var dataTypeAttributes = new DataTypeAttributes();
var writeThruOptions = new WriteThruOptions(WriteMode.WriteBehind, 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;
}
更多资讯
NCache 为 Write-Behind on 提供示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。运行 命名空间。
Java的: COM。alachisoft.ncache.runtime.datasourceprovider 命名空间。