0

I have 2 web applications in mvc5, webapp1, webapp2. By Autofac, I register this classes:

//cache managers
builder.RegisterType<MemoryCacheManager>().As<ICacheManager().Named<ICacheManager>("snt_cache_static").SingleInstance();
builder.RegisterType<SettingService>().As<ISettingService>().WithParameter(ResolvedParameter.ForNamed<ICacheManager>("snt_cache_static")).InstancePerLifetimeScope();

builder.RegisterType<TestController>()
            .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("snt_cache_static"));

When I modify and save into database a value with webapp2, I clear the cache and if I compare the new value with the stored in cache, ok, correspond, as once saved was deleted from chache.

However, if I compare the new value with the stored in cache through webapp1, it is still saved the old value.

the cache of WebApp1 should correspond to that of webapp2, because to read the cache in both sites I use the parameter "_settingService" or "_cacheManager" passed in controller by Autofac:

public TestController(
            ISettingService settingService,
            ICacheManager cacheManager
            )
        {
            this._settingService = settingService; 
            this._cacheManager = cacheManager;
        }

My question is: how do I share the cache in both applications?

1 Answers1

0

As is stated in this StackExchange answer, each web application has its own Application Domain (.NET construct). So they cannot share any in memory objects (technically they can but its not that simple) Only way to share data between two apps is to use cache in separate process (memcached for example) which inevitably leads to data serialization.

If your database shared between the apps is on SQL Server, maybe you can use build-in ASP.NET cache with SqlCacheDependency. That way each app will have its own separate cache, but this cache will be invalidated each time other app changes something in database...

Community
  • 1
  • 1
Michal Levý
  • 33,064
  • 4
  • 68
  • 86