Thanks Joshua, I accepted your advice. Here the solution I ended up with seems to work fine. Any feedback is appreciated.
public class TenantLifecycle : ILifecycle { private readonly ConcurrentDictionary<string, MainObjectCache> _tenantCaches = new ConcurrentDictionary<string, MainObjectCache>(); public IObjectCache FindCache() { var cache = _tenantCaches.GetOrAdd(TenantKey, new MainObjectCache()); return cache; } public void EjectAll() { FindCache().DisposeAndClear(); } public string Scope { get { return "Tenant"; } } protected virtual string TenantKey { get { var requestHost = HttpContext.Current.Request.Url.Host; var normalisedRequestHost = requestHost.ToLowerInvariant(); return normalisedRequestHost; } } }
With StructureMap configuration:
ObjectFactory.Initialize( x => x.For<ISiteSettings>() .LifecycleIs(new TenantLifecycle()) .Use<SiteSettings>() );
source share