过去,专家们曾尝试通过使用系统缓存来消除性能瓶颈,但随着用户流量的增加,对高速应用程序的需求变得很重要。 因此,内存中、线性可扩展的分布式缓存解决方案,如 NCache 越来越流行,增强 系统性能 多倍的。
通常,通过在系统架构中添加缓存层,应用程序负责维护双跳数据完整性——在每次写入请求时,都需要在缓存和后端数据源中更新数据。 因此,如果您有多个应用程序,访问相同的缓存和数据库,您将不得不在所有应用程序中复制您的持久性代码。 这增加了应用程序级别的代码复杂性。
NCache 更多信息 NCache 直写文档 NCache 直写式管理指南
什么是直写?
直写是一种缓存技术,当应用程序将数据写入缓存并配置支持源时,它也会更新数据库中的记录。
NCache 提供了一个 直写 您可以实现的接口,添加所有可能存在于客户端应用程序中的持久性代码逻辑。 您需要通过以下方式在所有缓存服务器上部署代码 NCache 网络管理员。
这样,每当对缓存执行写入操作时,都会调用已部署的提供程序并根据您的逻辑更新数据库。
要了解如何配置直写提供程序,请参阅 官方 NCache 直写提供程序文档。
有时,您的直写操作可能会失败。发生这种情况的原因有多种,例如网络连接问题或接口实现逻辑中的错误。由于 Write-Through 是同步的, NCache 向应用程序抛出异常。您还会在缓存日志中找到记录的错误。
而且,由于 NCache 还提供操作状态——它为用户提供了处理各种预定场景的灵活性。 例如,如果操作失败并且您想重试,您可以设置相关选项,并且 NCache 将为您执行退休。
有关这些操作的更多详细信息,请参阅 NCache 配套文档.
直写提供程序接口
如前面提到的, NCache 暴露一个 直写 期望用户实现然后在服务器端部署的接口。 下面的代码片段显示了 IWriteThuProvider 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public interface IWriteThruProvider { // Perform tasks associated with freeing, releasing, or resetting resources. void Dispose(); // Perform tasks like allocating resources or acquiring connections void Init(IDictionary parameters, string cacheId); //Responsible for write operations on data source. OperationResult WriteToDataSource(WriteOperation operation); ICollection WriteToDataSource(ICollection operations); ICollection WriteToDataSource(ICollection dataTypeWriteOperations); } |
存在三个重载 写入数据源 方法。 一个迎合原子数据库更新请求,而另一个处理批量数据库写入请求。 NCache 还公开了一种方法,您可以在其中添加代码逻辑以进行更新 NCache- 支持的数据结构 到支持源。
应用程序中的直写
前端应用程序只需访问缓存以进行任何写入操作,并具有以下选项 直写 指定以便缓存通过直写提供程序在数据库上应用该操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Pre-Condition: Cache is already connected // Fetch product with the given ProductID Product product = FetchProductByProductID(1001); // Specify the key of the item string key = $"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); |
什么是后写缓存?
后写 在分布式缓存中与直写缓存相同,只是它异步更新数据库。 这意味着应用程序永远不必等待数据源更新,从而提高应用程序性能,因为对后端数据源的更新是最慢的应用程序操作。
请记住,虽然假设数据将更新到数据库是安全的,但如果您正在处理高度敏感的数据,建议您选择直写。 此外,由于它是一种异步机制,因此 write-behind 中的失败会在缓存日志中记录为异常和错误,但不会被抛出给应用程序。 NCache 提供以与 Write-Through 相同的方式处理故障的选项,因为它还返回操作状态。
参考 NCache 文档 有关这些操作的详细信息。
应用程序中的后写
Write-behind 是一种更快的方式来执行 Write-Through 操作,因为它使用相同的 Write-Through 提供程序但异步。 应用程序方面的唯一变化是将 Write-Through 选项指定为 WriteBehind 而不是 WriteThru。
看看下面的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Pre-Condition: Cache is already connected // Fetch product with the given ProductID Product product = FetchProductByProductID(1001); // Specify the key of the item string key = $"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.WriteBehind; // Add the item in the cache with WriteThru enabled CacheItemVersion itemVersion = cache.Insert(key, cacheItem, writeThruOptions); |
NCache 更多信息 直写式缓存文档 在缓存中使用 Write-Behind
结论
使用分布式缓存解决方案,例如 NCache 现在提供 Write-Through 和 Write-Behind 等功能,后端数据库的更新更加简单且更易于管理。它将所有持久性代码逻辑封装在单个提供程序中。这使您可以在提供程序级别维护数据库层并在应用程序级别维护缓存层。所以,与 NCache,您将获得两全其美的效果。您可以获得读取操作的缓存性能和写入操作的数据维护性能。因此,借助企业解决方案(例如 NCache。联系我们,让我们的专家为您提供帮助!