Simple Cache shared in multiple application instances
-
Hi, I've implemented a very simple cache in a winforms-application using a static Colletion of objects. This collection, of course, is initialized at its first use. As the cache does not need to get updated through the whole application lifetime, that's all I need. My question now is: How to share this static collection through multiple instances of my application? I don't need an extra cache for each instance.. Thanks in adavance.
I don't think you can, at least not without negating the benfits of the cache. Each instance of the app has its own AppDomain, which has its own allocated memory. (You could achieve this in c++ for example). You 'd need to get a copy of the cache from one application via some sort of RPC or similar, but this would probably be slower than just getting the data again.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
Hi, I've implemented a very simple cache in a winforms-application using a static Colletion of objects. This collection, of course, is initialized at its first use. As the cache does not need to get updated through the whole application lifetime, that's all I need. My question now is: How to share this static collection through multiple instances of my application? I don't need an extra cache for each instance.. Thanks in adavance.
Thanks for your answers. What about moving the static collection (cache) to a small library-dll which is referenced from my application. this way, the library-dll would get loaded only one, wouldn't?
-
Thanks for your answers. What about moving the static collection (cache) to a small library-dll which is referenced from my application. this way, the library-dll would get loaded only one, wouldn't?
No, the library would load into the
AppDomain
of each calling application. This compartmentalisation is so that C# apps can't interfe whith eachother's memory spaces, producing "unexpected" results (e.g. mutex problems, datachanges etc). Objects must be marshalled across app domains, which is a heavy duty task.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
Hi, I've implemented a very simple cache in a winforms-application using a static Colletion of objects. This collection, of course, is initialized at its first use. As the cache does not need to get updated through the whole application lifetime, that's all I need. My question now is: How to share this static collection through multiple instances of my application? I don't need an extra cache for each instance.. Thanks in adavance.
Why do you want to share the cache between multiple instances? The reason I ask is because there may be another way to achieve the required end result (whatever that may be). As pointed out by others, you might want to share this cache if it is very large and takes up a lot of memory, or if it takes a particularly long time to load up, or if it is changing and needs to be synchronised across all the instances using it. Otherwise, I suspect that the pain of sharing this cache is going to outweight the benefits. If you decide really do need a shared or distributed cache, it might be worth investigating the caching support in the Enterprise Library Framework, or Spring.NET or similar, to see if they meet your needs.
-
Why do you want to share the cache between multiple instances? The reason I ask is because there may be another way to achieve the required end result (whatever that may be). As pointed out by others, you might want to share this cache if it is very large and takes up a lot of memory, or if it takes a particularly long time to load up, or if it is changing and needs to be synchronised across all the instances using it. Otherwise, I suspect that the pain of sharing this cache is going to outweight the benefits. If you decide really do need a shared or distributed cache, it might be worth investigating the caching support in the Enterprise Library Framework, or Spring.NET or similar, to see if they meet your needs.
My goal is to reduce the application's time of initialisation. Today, every instance creates its own cache, which takes about 30-60 seconds (depending on database's resources). I thought about reducing the time of initialisations to < 5s (for all except the first application's start).
-
My goal is to reduce the application's time of initialisation. Today, every instance creates its own cache, which takes about 30-60 seconds (depending on database's resources). I thought about reducing the time of initialisations to < 5s (for all except the first application's start).
Obvious, but worth asking: Have you considered lazy-loading the cache? i.e. only load the info absolutely requied at start up, then loading the rest when they are is accessed for the first time?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
Obvious, but worth asking: Have you considered lazy-loading the cache? i.e. only load the info absolutely requied at start up, then loading the rest when they are is accessed for the first time?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Yep, lazy-loading was implemented in former version of the application but didn't satisfy the users due to slow runtime performane.
-
Yep, lazy-loading was implemented in former version of the application but didn't satisfy the users due to slow runtime performane.
The hit should only be on the first call per cached element, so I'm surprised at this. Is it possible to explicitly lazy-load elemtents in the background while the system is idle?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
-
The hit should only be on the first call per cached element, so I'm surprised at this. Is it possible to explicitly lazy-load elemtents in the background while the system is idle?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Would be possible, but the application is quite useless until the cache is ready. Nevertheless, I'll invest some more thoughts in this approach. There are 5 flat tables, which each are loaded in a collection. Maybe I should try to parallelise these 5 requests. PS: the cache is used for filter criterions (and the application's main feature is searching/filtering data)
-
My goal is to reduce the application's time of initialisation. Today, every instance creates its own cache, which takes about 30-60 seconds (depending on database's resources). I thought about reducing the time of initialisations to < 5s (for all except the first application's start).
Well, one option is to use a memory mapped file to get shared access to the data. There is an article here about it: DevGlobalCache – A way to Cache and Share data between processes[^] That's C++ but you may be able to rework it to C# depending on how confident you feel about that sort of thing. Alternatively, you could have one central "cache manager" that loads the cache into memory on system start-up, then the other processes link to it using named pipes or something to load the cache into their local memory when they initialise (or lazy-load the cache as they need it). Or, you could load the cache once, then serialise it out to disk in an easy to read format that can be loaded back when needed. Depending on what is involved in loading the cache, reading a serialised stream in from local disk might be quicker than re-initialising the cache from the ground up each time. The problem with these solutions is keeping the cache synchronised across all instances, but from what you say that doesn't sound like it's going to be a problem for you.