读取缓存使用情况
本节介绍在配置和部署 Read-Through 缓存提供程序后如何使用它。 NCache 提供 Alachisoft.NCache.Runtime.Caching.ReadThruOptions
类来指定 API 中的 Read-Thru 选项。 这 ReadThruOptions
类包含枚举 ReadMode
, 可以是 ReadThru
or ReadThruForced
,解释如下。
ReadThru
检查缓存中是否存在项目,如果未找到,则从数据源获取数据。ReadThruForced
从数据源获取项目,无论其是否存在于缓存中。
可以通过以下方式配置多个只读缓存提供程序 NCache。如果未通过 API 提及特定的提供程序名称,则将调用默认的 Read-Through 缓存提供程序。您还可以通过使用特定于提供程序的 API 重载来使用默认提供程序以外的提供程序。
重要
对于 Java,在部署 JAR 文件之前,您需要确保:
- JDK 17 安装。
- Java 的环境变量已设置。
使用读取缓存的先决条件
通过 Get 操作使用 Read-Through
以下示例检索启用了 Read-Through 的项目,对应于使用指定的键 Get
方法。
// Pre-condition: Cache is already connected
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
使用强制通读
NCache 提供强制通读的选项,通过它 NCache 强制从数据源获取数据。 这意味着数据不会在缓存中检查,而是直接从数据源中获取。 您可以通过指定来启用强制通读 ReadMode
as ReadThruForced
.
以下示例使用 ReadThruForced
通过强制启用 Read-Through 选项。
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThruForced;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
将通读与批量操作结合使用
以下示例检索启用了 Read-Through 的产品字典,对应于使用指定的键 GetBulk
方法。
Tips:
为了更好地理解这些操作回顾 批量操作.
// Create a new array of keys
String[] keys = new string[];
// 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
对 CacheItem 使用直读
以下示例检索一个 CacheItem
启用了 Read-Through,对应于指定的键。
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
CacheItem data = cache.GetCacheItem(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
对数据结构使用通读
以下示例检索启用了 Read-Through 且与指定键对应的 Counter、List、Queue、HashSet 和 Dictionary。
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
switch(mainMenu)
{
case mainMenu.GetDistributedCounter:
// Retrieve the count of the corresponding item with read thru enabled
ICounter counter = cache.DataTypeManager.GetCounter(key, readThruOptions);
break;
case mainMenu.GetDistributedDictionary:
// Retrieve dictionary of the corresponding item with read thru enabled
IDistributedDictionary<string, object> distributedDictionary = cache.DataTypeManager.GetDictionary<string, object>(key,readThruOptions);
break;
case mainMenu.GetDistributedList:
// Retrieve the list of the corresponding item with read thru enabled
IDistributedList<object> distributedList = cache.DataTypeManager.GetList<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedQueue:
// Retrieve the queue of the corresponding item with read thru enabled
IDistributedQueue<object> distributedQueue = cache.DataTypeManager.GetQueue<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedHashSet:
// Retrieve the HashSet of the corresponding item with read thru enabled
IDistributedHashSet<int> distributedHashSet = cache.DataTypeManager.GetHashSet<int>(key, readThruOptions);
break;
}
配置默认通读提供程序
您可以通过以下方式指定默认提供程序 NCache 管理中心或通过 客户端.ncconf 文件。 如果 API 和 客户端.ncconf, 然后将自动使用默认提供程序。
<cache id="demoCache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True">
...
</cache>
CacheConnectionOptions
也可用于指定提供者。 NCache 在加载提供的程序集期间出现异常时,在缓存日志中提供错误。
更多资讯
NCache 提供了一个用于 Read-Through 的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。运行 命名空间。
Java的: COM。alachisoft.ncache.runtime.datasourceprovider 命名空间。