The soultion I came up with was to use the HttpContext cache to cache the instance of 'IObjectCache' and use the cache timeout - when the item is evicted from the cache we can singal to StructureMap the current life cycle is over and new will be required next time an instance of the service is required.
public class HttpContextCacheLifeStyle : ILifecycle
{
private readonly int _cacheExpirtyInMinutes;
public HttpContextCacheLifeStyle(int cacheExpirtyInMinutes)
{
_cacheExpirtyInMinutes = cacheExpirtyInMinutes;
}
public void EjectAll()
{
FindCache().DisposeAndClear();
}
public IObjectCache FindCache()
{
var objectCache = HttpContext.Current.Cache[Scope
] as IObjectCache;
if (objectCache == null)
{
objectCache = new MainObjectCache();
HttpContext.Current.Cache.Add(Scope
,
objectCache,
null,
DateTime.Now.AddMinutes(_cacheExpirtyInMinutes),
Cache.NoSlidingExpiration,
CacheItemPriority.High, LifeStyleEvicted);
}
return objectCache;
}
private void LifeStyleEvicted(string key, object value, CacheItemRemovedReason reason)
{
((IObjectCache) value).DisposeAndClear();
}
public string Scope
{
get { return GetType().Name; }
}
}
0 comments:
Post a Comment