实现缓存加载器和刷新器
要使用缓存启动加载器和刷新器, ICacheLoader
需要先实现接口。 然后可以使用以下方式配置Loader和Refresher的自定义逻辑 NCache 管理中心 or 命令行工具. NCache 根据自定义逻辑从配置的数据源加载和刷新数据。 在缓存启动时, NCache 呼叫 Init
Cache Startup Loader 的方法来初始化它。 成功初始化Cache Startup Loader,调用已实现的 LoadDatasetOnStartup
方法将数据加载到缓存中。 然后它使用 刷新数据集 方法来刷新加载在缓存中的数据在确定 刷新间隔.
在下面的例子中,客户端实现了 ICacheLoader
接口来配置其自定义加载器和刷新器逻辑。 如果您配置了两个数据集 NCache 管理中心: 热销产品 和 供应商. 以下实现负载 热销产品 和 供应商 在缓存启动时。 此外,它还会按照指定的刷新时间间隔刷新这些数据集。
更多关于Cache Loader组件的详细信息,请参考章节 缓存启动加载器和刷新器的组件.
缓存加载器和刷新器先决条件
- 了解与所有人员合作所需的标准先决条件 NCache 服务器端功能请参考给定页面 服务器端 API 先决条件.
- 有关 API 详细信息,请参阅: 缓存, 缓存项, 缓存加载器.
- 这应该是一个类库项目。
- 确保 配置缓存加载器 使用 NCache 管理中心或命令行工具 NCache 簇。
初始化缓存加载器和刷新器
下面的代码展示了如何实现 Init
的方法 ICacheLoader
界面。 此方法将参数作为输入并针对它们分配值。
public void Init(IDictionary<string, string> parameters, string cacheName)
{
cache = CacheManager.GetCache(cacheName);
connectionString = parameters.ContainsKey("ConnectionString") ? parameters["ConnectionString"] : null;
if (connectionString != null)
{
connection = new SqlConnection(connectionString);
}
}
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
在缓存启动时加载数据
下面的实现 LoadDatasetOnStartup
方法获取一个 产品 从数据源中获取并将其添加到缓存中。 这 LoadDatasetOnStartup
返回一个用户上下文,其中保存有关缓存中加载的数据的信息。 当缓存开始预加载缓存中的数据时,会调用该方法。
public object LoadDatasetOnStartup(string dataset)
{
// Create a list of datasets to load at cache startup
IList<object> datasetToLoad;
switch (dataset.ToLower())
{
// If dataset is products, fetch products from data source to load in cache
case "products":
datasetToLoad = FetchProductsFromDataSource();
// Insert fetched products in the cache
foreach (var product in datasetToLoad)
{
string key = $"ProductID:{product.Id}";
cache.Insert(key, product);
}
break;
// If dataset is suppliers, fetch suppliers from data source to load in cache
case "suppliers":
datasetToLoad = FetchSuppliersFromDataSource();
// Insert fetched suppliers in the cache
foreach (var supplier in datasetToLoad)
{
string key = $"SupplierID:{supplier.Id}";
cache.Insert(key, supplier);
}
break;
default:
// Invalid dataset
}
// User context is the time at which datasets were loaded in the cache
object userContext = DateTime.Now;
return userContext;
}
刷新数据集
以下代码实现了如何在调用 Cache Refresher 时在启动时刷新已加载到缓存中的数据。 这 刷新数据集 方法使用返回的用户上下文 启动时加载数据集 方法来验证要刷新的数据。
public object RefreshDataset(string dataset, object userContext)
{
DateTime? lastRefreshTime;
switch (dataset.ToLower())
{
// If dataset is products, fetch updated products from data source
case "products":
lastRefreshTime = userContext as DateTime?;
IList<Product> productsToRefresh = FetchUpdatedProducts(lastRefreshTime) as IList<Product>;
// Insert updated products in the cache
foreach (var product in productsToRefresh)
{
string key = $"ProductID:{product.Id}";
CacheItem cacheItem = new CacheItem(product);
_cache.Insert(key, cacheItem);
}
break;
// If dataset is supplier, fetch updated suppliers from data source
case "suppliers":
lastRefreshTime = userContext as DateTime?;
IList<Supplier> suppliersToRefresh = FetchUpdatedSuppliers(lastRefreshTime) as IList<Supplier>;
// Insert updated suppliers in the cache
foreach (var supplier in suppliersToRefresh)
{
string key = $"SupplierID:{supplier.Id}";
CacheItem cacheItem = new CacheItem(supplier);
_cache.Insert(key, cacheItem);
}
break;
default:
// Invalid dataset
}
// User context is the time at which datasets were refreshed
userContext = DateTime.Now;
return userContext;
}
获取要刷新的数据集
获取数据集刷新 方法实现自定义逻辑,以在运行时刷新预配置的数据集 RefreshPreference
。 该方法获取用户上下文并分配一个 刷新偏好 取决于指定的数据集。
public IDictionary<string, RefreshPreference> GetDatasetsToRefresh(IDictionary<string, object> userContexts)
{
DateTime? lastRefreshTime;
bool datasetHasUpdated;
// Create a dictionary for datasets to refresh with their Refresh Preference
IDictionary<string, RefreshPreference> DatasetsToRefresh = new Dictionary<string, RefreshPreference>();
foreach (var dataset in userContexts.Keys)
{
switch (dataset.ToLower())
{
// If dataset is products, check if dataset has been updated in data source
// if yes, then refresh the dataset now
case "products":
lastRefreshTime = userContexts[dataset] as DateTime?;
datasetHasUpdated = HasProductDatasetUpdated(dataset, lastRefreshTime);
if (datasetHasUpdated)
{
DatasetsToRefresh.Add(dataset, RefreshPreference.RefreshNow);
}
break;
// If dataset is suppliers, check if dataset has been updated in data source
// if yes, then refresh dataset on next time of day
case "suppliers":
lastRefreshTime = userContexts[dataset] as DateTime?;
datasetHasUpdated = HasSupplierDatasetUpdated(dataset, lastRefreshTime);
if (datasetHasUpdated)
{
DatasetsToRefresh.Add(dataset, RefreshPreference.RefreshOnNextTimeOfDay);
}
break;
default:
// Invalid dataset
}
}
// Return the dictionary containing datasets to refresh on polling with their refresh preferences
return DatasetsToRefresh;
}
处置所有资源
最后,调用 Dispose
方法确保您已关闭/删除所有资源以防止浪费。
备注
配置缓存加载器/刷新器 NCache 通过参考 配置缓存加载器和刷新器 在管理员指南中寻求帮助。
更多资讯
NCache 提供了 Cache Loader 和 Refresher 的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。运行 命名空间。
Java的: COM。alachisoft.ncache.runtime.cacheloader 命名空间。