CacheTower.ContextActivators.SimpleInjector
August 11, 2021 ยท View on GitHub
What is this?
This is a SimpleInjector context activator for CacheTower.
This allows you to resolve a context via the DI container where the scope of the injected dependencies are managed by the container.
This is often important for background refreshing which can run out of band and at any time.
Usage
Define a context, or use a pre-registered context
public class SessionContext
{
public IStatsClient StatsClient { get; }
public ISession Session { get; }
public SessionContext(IStatsClient statsClient, ISession session)
{
StatsClient = statsClient;
Session = session;
}
}
Make sure you register your context (use interfaces if preferred, concrete shown for speed)
container.Register<SessionContext>(Lifestyle.Scoped);
Configure your cache stack:
var myCacheStack = new CacheStack<SessionContext>(
new SimpleInjectorContextActivator<SessionContext>(container),
new ICacheLayer[]
{
new MemoryCacheLayer(),
};,
new ICacheExtension[]
{
new AutoCleanupExtension(TimeSpan.FromSeconds(30)),
});
container.RegisterInstance<ICacheStack>(myCacheStack);
container.RegisterInstance<ICacheStack<SessionContext>>(myCacheStack);
Note that both ICacheStack and ICacheStack
Finally, consume your context as required:
public class MyClass
{
private ICacheStack<SessionContext> _cacheStack;
public MyClass(ICacheStack<SessionContext cacheStack)
{
_cacheStack = cacheStack;
}
public async Task DoSomething()
{
var value = _cacheStack.GetOrSetAsync<TData>(
"key",
async (oldVal, context) =>
{
return await context.Session.Query<TData>().Where(x => x.Id == 1).SingleOrDefaultAsync);
},
new CacheSettings(TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(15)));
// Do something with value
}
}