我正在开发 NopCommerce 2.40 。我想使用CacheManager在主页上设置缓存。
var cacheModel = _cacheManager.Get(cacheKey, () =>
{
var model = new HomePageProductsModel()
{
....
....
}
return model;
});
当我调试这段代码时。它命中了MemoryCacheManager中的Get方法。现在,在管理端,当我更新任何产品时,我想通过 key 删除此缓存。下面调用的Remove方法会点击PerRequestCacheManager的Remove。
_cacheManager.Remove(string.Format("product.hometemplate-{0}-{1}", storeid, true));
所以主页上的这个缓存没有被删除。有什么办法解决吗
最佳答案
Nop.Web项目中的一些 Controller 使用MemoryCacheManager。这些具有静态缓存 (MemoryCacheManager) 的 Controller 在\Nop.Web\Infrastruct\DependencyRegistrar.cs 文件中定义。在此文件中,我们定义应注入(inject)哪一个 ICacheManager 实现。
但我建议您使用 nopCommerce 中使用的相同方法。使用事件。订阅\Nop.Web\Infrastruct\Cache\ModelCacheEventConsumer.cs 文件中的产品插入/删除/更新事件并在那里重置缓存。只需看看它是如何完成的。
关于c# - Nopcommerce PerRequestCacheManager 与 MemoryCacheManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776686/