当今大多数蓬勃发展的电子商务企业在过去的某个时候都遭受过网站反应时间缓慢的困扰。 这部分是由于用户偏好的范式转变——更倾向于在线购买。 因此,传统机制无法维持大量涌入的客户请求。
随着内存、线性可扩展、分布式缓存解决方案的出现,如 NCache,业务响应时间有了显着改善。 大多数繁荣的企业在其系统架构中都有一个缓存层部分——这最初给企业带来了巨大的繁荣。
NCache 更多信息 NCache 加载程序文档 NCache 装载机管理员指南
什么是缓存加载器?
在我继续之前,让我们考虑一个场景——您的企业计划在午夜推出限时抢购折扣。 届时,用户请求将开始涌入。 那么,在销售正式开始之前,您如何使用相关的折扣详细信息加载您的缓存。 这样,一旦您被用户请求轰炸,您将拥有相关信息来处理它们。
企业缓存解决方案,例如 NCache 通过结合特殊的缓存功能来帮助克服这种初始性能滞后 - 缓存启动加载器 预加载缓存。 NCache 暴露一个 缓存加载器 您可以实现的接口,然后 部署在服务器上. 根据您的逻辑,然后在启动时将数据作为后台进程预加载到缓存中。
NCache 缓存加载器 接口公开了以下三种方法—— 在里面, 部署及 启动时加载数据集 您可以根据您的业务需求自定义代码:
1 2 3 |
public void Init(IDictionary<string, string> parameters, string cacheName); public object LoadDatasetOnStartup(string dataset); public void Dispose(); |
缓存加载器功能
具有丰富的功能集, NCache 启动装载机 有助于提升用户体验。 它的开发在考虑面向客户的问题的同时,也在寻找解决这些问题的方法。
为了帮助您对主题有更深入的了解,让我们来看看核心 NCache 装载机特点如下:
易于配置
ICacheLoader
界面易于配置。 您只需要实现它,然后通过 NCache 服务器端的管理器。 通过启用缓存加载器功能,您的代码会在缓存启动时自动执行。
逻辑数据集
如果您正在处理多节点缓存集群,您可以将数据划分为逻辑集和 NCache 通过以循环方式将这些组分配给集群节点来加速数据加载过程。
假设您有一个双节点集群,并且您的业务需要您加载两个数据集: products
和 suppliers
. 只需将它们包含在您的界面逻辑中并指定这些 数据集 在您的 缓存配置. NCache 将开始在两个集群节点上并行加载数据。
下面是该方法的示例实现 启动时加载数据集:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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 product 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": // load suppliers from database and add the cache. break; default: // Invalid dataset } // User context is the time at which datasets were loaded in the cache object userContext = DateTime.Now; return userContext; } |
专门的装载机服务
NCache 在每个服务器节点上使用专用的加载器服务来帮助提高整体缓存速度。 这是一个关键 NCache 功能,因为如果大型数据集需要在缓存中预加载,单独的加载器服务可确保这不会影响常规缓存性能。
NCache 更多信息 ICacheLoader 接口文档 NCache 部署提供者指南
使用 NCache 刷新以重新加载缓存数据
虽然我们知道预加载缓存有多大帮助,但如果后端数据源有定期更新,缓存数据很可能会过时。 在这种情况下,缓存中填充的数据没有意义——完全无法实现预加载缓存的目的。
继续上一节中的电子商务示例,假设在第一阶段,您为选定商品提供 25% 的固定折扣,但突然间,您决定将折扣百分比提高到 50%,并将其应用于整个stock——现有的缓存数据会怎样? 它现在已经过时了——这意味着您需要一种机制来刷新缓存中的相关数据集。
NCache 针对这种情况有一个解决方案——它允许其用户使用源中的最新数据定期刷新缓存。 您可以通过以下方式执行此操作 调度您的数据集 或 调用复习 一经请求。
如果您可以预测何时需要刷新缓存,则可以实现 RefreshDataset
方法,并选择适当的时间间隔,可以是小时、天、周或月。
下面是该方法的示例实现 刷新数据集:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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?; // fetch all suppliers updated since ‘lastRefreshTime’ and insert in cache. break; default: // Invalid dataset } // User context is the time at which datasets were refreshed userContext = DateTime.Now; return userContext; } |
另一方面,如果您不确定缓存数据何时会过时,则可以实现 获取数据集刷新 中的方法 ICacheLoader
界面以编程方式查找已更改的数据集并刷新它们。 您还可以使用 PowerShell 调用刷新数据集 cmdlet 按需刷新缓存。
1 |
public IDictionary<string, RefreshPreference> GetDatasetsToRefresh(IDictionary<string, object> userContexts); |
结论
缓存加载器不再只是一种选择——对于希望在全球范围内扩展客户的企业来说,它是一项必备功能,因为在这个快节奏、竞争激烈的时代,您的业务永远不会失败。 因此,如果您正在寻找分布式缓存解决方案,例如 NCache 帮助您发展业务, 联络方式 与我们一起,让我们的技术专家为您提供帮助!