Creating an Autofac Lifetimescope that will expire with time -
i have bank/collection caches instances of objects in memory each request doesn't need go datastore. i'd autofac provide instance of bank, expire after x seconds, new instance created on next request. i'm having trouble getting head around setting lifetimescope achieve this. i've read through this couple of times. bank object not subject unit of work. ideally reside 'above' units of work, caching objects within , across them.
i'm using approach below, isn't working i'd hoped.
can please point me in right direction?
.... builder.register(c => { return new ormapbank(c.resolve<iormaproot>()); }).instancepermatchinglifetimescope(expiretimetag.tag()); icontainer container = builder.build(); var timedcache= rootscope.beginlifetimescope(expiretimetag.tag()); dependencyresolver.setresolver(new autofacdependencyresolver(timedcache)); ....
public static class expiretimetag { static datetime d = datetime.now; static object tag = new object(); public static object tag() { if (d.addseconds(10) < datetime.now) { createtag(); return tag; } private static void createtag() { tag = new object(); } } thanks in advance.
it common use caching decorator achieve kind of behaviour. assuming iormaproot responsible getting data in question (but work same if ormapbank) following:
- create new type,
cachingormaprootimplementsiormaproot - add constructor takes expiry
timespan, instance of originaliormaprootimplementation. - implement members call underlying instance , cache results accordingly subsequent calls (implementation vary on cache technology).
- register type in container
iormaproot
this clean way implement such caching. makes easy switch between cached , non-cached implementations.
Comments
Post a Comment