3

i want to use caching in customer controller (Login action)(Nop.Web).

so please help me.

i am using this way but not usefull

create one class in nop.core(domain folder) CustomStoreCache.cs

 public partial class CustomStoreCache
    {
        public ObjectCache StoreCache
        {
            get
            {
                return MemoryCache.Default;
            }
        }
    }

and implement cache in customer controller login method and in GetAuthenticatedCustomer() method FormsAuthenticationService.cs

string cachekey = "Id-" + customer.Id.ToString();
var store = CustomStoreCache.StoreCache[cachekey];

but it gives error on this line CustomStoreCache.StoreCache[cachekey];

regards, jatin

1 Answers1

7

First, you don't need to create a new 'CacheStore', you need to inject the proper cache instance to your controller.

First thing you need to know is that NopCommerce has two cache managers. Both are declared at 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 manager, only holds data for the current HTTP request. The second one, the static cache, spans to other HTTP requests.

The default cache is the PerRequestCacheManager and you'll get an instance just by adding it to your controller constructor. If you want to use the static cache, you need to instruct Autofac to inject it when you configure the dependencies for your controller. Look at DependencyRegistrar.cs, there are several examples for this, for instance:

builder.RegisterType<ProductTagService>().As<IProductTagService>()
            .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
            .InstancePerHttpRequest();

You really should use DI instead of adding an static reference to MemoryCacheManager. This way you could change the cache provider in the future if needed.

I suggest you to use the nopcommerce conventions to access the cache and use this syntax:

return _cacheManager.Get(cacheKey, () =>
        { ..... get and return a brand new instance if not found; } );

There are plenty of examples for this...

Marco Regueira
  • 1,045
  • 8
  • 17
  • Hello **@Marco** thanks for reply..you are right but my cache key **concat** with **customer id** so when we get value from caching..then we need to cachekey and cachekey need customer id so how to get customer id in **dependencyRegister**. see my another question for this topic http://stackoverflow.com/questions/24647672/how-to-use-caching-in-dependencyregisternop-web-framework –  Jul 09 '14 at 09:44
  • You're making a wrong assumption. Generate the key at the place where you consume the caching service. In this case in your controller. Check the CategoryService class, for a sample on how to do this; for instance check the method GetAllCategoriesByParentCategoryId, it fits your requirement entirely. – Marco Regueira Jul 09 '14 at 09:53
  • @Marco.. for each customer one cachekey generate when they login. in every cahce key there is different store id from customer_store_mapping table.. its working fine then for every instance http request different store id will get return in BuildRegistration on dependencyRegister i need to generate cache key to get store id from caching..so i want to get current customer in dependencyRegister. –  Jul 09 '14 at 10:08
  • and as per you said we follow GetAllCategoriesByParentCategoryId okay? but in there when we use _workcontext.currentcustomer.Id and when call this method. then again circular reference error. –  Jul 09 '14 at 10:14
  • 1st. You don't need different cache stores for customer/store, therefore you don't need different keys when registering dependencies. If you're doing like that you need to refactor your code and review how caching works as you're making wrong assupmtions. – Marco Regueira Jul 09 '14 at 10:32
  • 2nd. For the dependency circular reference. I can't imagine why are you getting this as it can be done easily (if you're really doing it at the controller). The exception will give you the complete list of items in the circular reference so you can debug. Please consider closing this question and opening a new one if you have questions about DI. – Marco Regueira Jul 09 '14 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57007/discussion-between-jatin-gadhiya-and-marco-regueira). –  Jul 09 '14 at 10:50