Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple Cache shared in multiple application instances

Simple Cache shared in multiple application instances

Scheduled Pinned Locked Moved C#
questioncsharpwinformstutoriallearning
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    buchstaben
    wrote on last edited by
    #1

    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.

    N K B D 4 Replies Last reply
    0
    • B buchstaben

      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.

      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      Unless your cache is huge or you will be running many instances of your app, I suggest you just load the cache in each process. If you do need to share your cache, you will have to implement some sort of inter-process communication, which will get messy. Nick

      ---------------------------------- Be excellent to each other :)

      1 Reply Last reply
      0
      • B buchstaben

        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.

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B buchstaben

          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.

          B Offline
          B Offline
          buchstaben
          wrote on last edited by
          #4

          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?

          K 1 Reply Last reply
          0
          • B buchstaben

            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?

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B buchstaben

              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.

              D Offline
              D Offline
              David Skelly
              wrote on last edited by
              #6

              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.

              B 1 Reply Last reply
              0
              • D David Skelly

                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.

                B Offline
                B Offline
                buchstaben
                wrote on last edited by
                #7

                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).

                K D 2 Replies Last reply
                0
                • B buchstaben

                  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).

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  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.

                  B 1 Reply Last reply
                  0
                  • K Keith Barrow

                    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.

                    B Offline
                    B Offline
                    buchstaben
                    wrote on last edited by
                    #9

                    Yep, lazy-loading was implemented in former version of the application but didn't satisfy the users due to slow runtime performane.

                    K 1 Reply Last reply
                    0
                    • B buchstaben

                      Yep, lazy-loading was implemented in former version of the application but didn't satisfy the users due to slow runtime performane.

                      K Offline
                      K Offline
                      Keith Barrow
                      wrote on last edited by
                      #10

                      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.

                      B 1 Reply Last reply
                      0
                      • K Keith Barrow

                        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.

                        B Offline
                        B Offline
                        buchstaben
                        wrote on last edited by
                        #11

                        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)

                        1 Reply Last reply
                        0
                        • B buchstaben

                          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).

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups