简体   繁体   English

在nopCommerce插件中使用CacheManager

[英]Using CacheManager inside a nopCommerce plugin

I have created a nopCommerce v3.5 plugin and want load a text file under a Controller Action to _cacheManager then reuse it in next requests. 我创建了一个nopCommerce v3.5 plugin并希望在Controller Action下将load a text file_cacheManager然后在下一个请求中重用它。

But _cacheManager always return NULL to me for my request Key . 但是_cacheManager总是为我的请求Key返回NULL

This is some part of my codes: 这是我的代码的一部分:

public class AbcController : Controller   
{
    private readonly ICacheManager _cacheManager;

    public AbcController(ICacheManager cacheManager)
    {
        this._cacheManager = cacheManager;
    }

    public ActionResult Test(string title,string titleLink, string backColor,  string textColor, string timeColor)
    {
        try
        {
           // myText is always NULL here >>   :(
           var myText = _cacheManager.Get<string>("myText");

           if (string.IsNullOrEmpty(jsText))
           {
              string path = HttpContext.Server.MapPath("~/plugins/Misc.Test/App_Data/text.txt");
              myText = System.IO.File.ReadAllText(path);

              // Fill cacheManager >> 
              _cacheManager.Set("myText", myText, int.MaxValue);
            }

            // Other codes ......
        }

        // Other codes ......
     }
}
  • Do i forget some part of codes inside other classes? 我会忘记其他类中的某些代码吗? like register someone or...??? 喜欢注册某人或... ???

  • What is my mistake? 我怎么了

NopCommerce has two cache managers.Both are declared at DependencyRegistrar.cs: NopCommerce有两个缓存管理器,两个都在DependencyRegistrar.cs中声明:

builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_static").SingleInstance();
builder.RegisterType<PerRequestCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_per_request").InstancePerHttpRequest();

The default cache is the PerRequestCacheManager and you'll get an instance just by adding it to your controller constructor. 默认的缓存是PerRequestCacheManager,您只需将其添加到控制器构造函数中即可获得一个实例。 If you want to use the static cache, you need to instruct Autofac to inject it when you configure the dependencies for your controller. 如果要使用静态缓存,则在配置控制器的依赖项时需要指示Autofac注入它。

DependencyRegistrar.cs, DependencyRegistrar.cs,

builder.RegisterType<MyController>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));

You really should use DI instead of adding an static reference to MemoryCacheManager. 您确实应该使用DI而不是向MemoryCacheManager添加静态引用。 This way you could change the cache provider in the future if needed. 这样,您将来可以根据需要更改缓存提供程序。

NopCommerce has two cache managers. NopCommerce有两个缓存管理器。 You are getting an instance that works while processing the same http request, but does not span to other requests. 您将获得一个实例,该实例在处理相同的http请求时可以正常工作,但不会跨越其他请求。

You need to inject an instance of the static cache manager. 您需要注入静态缓存管理器的实例。 You can find all the info in my reply to this other question: Nopcommerce cache seems empty after adding a list . 您可以在我对另一个问题的答复中找到所有信息: 添加列表之后,Nopcommerce缓存似乎为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM