Cannot get DI to provide the static cache to a service

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hey all, I wonder if I could get some help, I've found simlar topics on the forum but none fix my situation.

I want to use the static cache in service ProductService, so in DependencyRegistwer I;

            builder.RegisterType<ProductService>().As<IProductService>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
                .SingleInstance();

..but every request I get an empty cache again, I tried the above with .InstancPerRequest and .InstanceLifetimeScope but same behaviour, my .Set() always goes to the PerRequestCacheManager not the static one.

Testing it with SettingsService (I think it was, one that has a ForNamed static set up), it uses the static cache fine but however I tinker with the declaration above I can't get ProductService to use the static cache - any ideas please ?

The cache declarations are fine, untouched;

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

...after years of using Nop as the basis for a very changed app, I now realise my cacheing has been fruitless ! - any help much appreciated.

This one is close, but has a hack as the fix :)
https://www.nopcommerce.com/boards/t/27890/bug-or-wrong-debugging-on-nop-caching-cachemanagersetkey-result-cachetime.aspx

Cheers,

Chris

(registered user of NopCommence if that helps/is required, thanks)
6 years ago
Still no joy with this, I keep having a go at itevery day or two but stuck, any ideas anyone ? - thanks
5 years ago
Hey trawangantech,

I just ran into the same issue after upgrading to 4.0. It looks like the NOP team went away from requiring you to specify which cache manager to use when registering your services in the DependencyRegister.

So instead of registering your service like this in the the DependencyRegister

builder.RegisterType<MyCustomService>().As<IMyCustomService>()
                            .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
                            .InstancePerLifetimeScope();


You will now just register it like any other service

  builder.RegisterType<MyCustomService>().As<IMyCustomService>().InstancePerLifetimeScope();

And in your service instead of injecting CacheManager

        private readonly ICacheManager _cacheManager;

You’ll now inject IStaticCacheManager

        private readonly IStaticCacheManager _staticCacheManager;

The StaticCacheManager gives you the ability to cache between HTTP Requests

You can see an example of this in the PriceCalculationService.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.