Plugin caching issue

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hello,

I am developing a plugin for nopCommerce 3.1.

I use the ICacheManager to cache some models in my controller but the problem is that nothing is actually cached.

I tried to debug the CacheExtensions to see if something is added, and I see that my key and model are added to the cache collection.

The problem is that at the next request it seems that nothing is cached.

I am also sending a snippet of the code:

public ActionResult PublicInfo()
{
....
string cacheKey ="My-cache-key{0}-{1}";
var cachedModel = _cacheManagerr.Get(cacheKey,100, () =>
                                                                  {
                                                                     return new MySampleMode(){
                                                                            Products = products;
                                                                      }
....

}


}
10 years ago
johnya wrote:
Hello,

I am developing a plugin for nopCommerce 3.1.

I use the ICacheManager to cache some models in my controller but the problem is that nothing is actually cached.

I tried to debug the CacheExtensions to see if something is added, and I see that my key and model are added to the cache collection.

The problem is that at the next request it seems that nothing is cached.

I am also sending a snippet of the code:

public ActionResult PublicInfo()
{
....
string cacheKey ="My-cache-key{0}-{1}";
var cachedModel = _cacheManagerr.Get(cacheKey,100, () =>
                                                                  {
                                                                     return new MySampleMode(){
                                                                            Products = products;
                                                                      }
....

}


}


I believe you need to explicitly register for the cache manager in DependencyRegistrar, since there's a few implementation of cache managers in nopCommerce. Try searching for the following in Nop.Web.Framework.DependencyRegistrar, and do the same in the DependencyRegistrar of your plugin. :)

builder.RegisterType<AclService>().As<IAclService>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
                .InstancePerHttpRequest();
10 years ago
Thank you so much for the reply.

Problex fixed
4 years ago
hi,  Dear sir  I need cache plugin,  http://a2000.ddns.net:81/en/  I have this web on demo server , I have to optimize its speed but I am unable to found any cache plugin on net, can you send me your plugin.




Regards,

Umar
[email protected]
4 years ago
ozi wrote:
hi,  Dear sir  I need cache plugin,  http://a2000.ddns.net:81/en/  I have this web on demo server , I have to optimize its speed but I am unable to found any cache plugin on net, can you send me your plugin.




Regards,

Umar
[email protected]


There is no specific cache plugin. But you can cache your plugin viewmodel per httprequest, to do that you have to use  IStaticCacheManager by giving a unique cachekey name like


//It is better to use a more unique and specific key depending on your requirement.

var myPluginCacheByProductId_KEY=string.Format("Nop.myplugin.cache-{0}", productid);
var myPluginCacheByProductId_PATTERN_KEY = "Nop.myplugin.cache";
var cacheManager = EngineContext.Current.Resolve<IStaticCacheManager>();
return cacheManager.Get(myPluginCacheByProductId_KEY, () =>
            {
                //Do your stuff and return your viewmodel
               return yourviewmodel;
            });


There is a lot example of using static and memory cache in the source code. Just keep in mind that for httprequest you have to use IStaticCacheManager, others are the same whatever it is plugin or source code. Also, consider that, you may need to clear the cache depending on some interaction. Then you have to use


_cacheManager.RemoveByPattern(myPluginCacheByProductId_PATTERN_KEY);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.