6 Replies Latest reply on Mar 14, 2007 11:44 AM by atijms

    Caching data per user - multiple instances or not?

    augustientje

      Hi,

      First of all I'm a beginner with JbossCache, so please bear with me ;) What I would like to do is cache data in the web tier per user, i.e. as a replacement for some stuff I now store in the HttpSession.

      What would be the best strategy when using JBossCache for this? Would I create a JBoss cache instance per user and associate that with her session, or would I create a single JBoss Cache instance, associate that with the application context and create a node per user?

      The advantage of the first strategy would be that the lifetime of the cache and all of its contents are directly linked to the lifetime of the session. When the user logs of the cache is certainly cleared for this user.

      On the other hand, I don't see a lot of articles or posts about JBoss Cache that advice something like this. The FAQ touches on the subject by saying that there are "some scenarios" for multiple cache instances. It would seem that caching data per user is not just some scenario but actually a major one.

      Could someone please advise me in this matter?

        • 1. Re: Caching data per user - multiple instances or not?
          manik

          Which app server do you use? If you are using JBoss AS, we use JBoss Cache behind the scenes anyway to replicate your user session data.

          • 2. Re: Caching data per user - multiple instances or not?
            augustientje

             

            "manik.surtani@jboss.com" wrote:
            Which app server do you use?


            The code in question is meant to be app server independent, so that's why I didn't mention a specific one.


            If you are using JBoss AS, we use JBoss Cache behind the scenes anyway to replicate your user session data.


            This surely sounds interesting. It's probably the reason why with JBoss AS you don't have to explicitely put an object again in the Http session after you mutated it in order to get it replicated, right?

            • 3. Re: Caching data per user - multiple instances or not?
              manik

               


              It's probably the reason why with JBoss AS you don't have to explicitely put an object again in the Http session after you mutated it in order to get it replicated, right?


              Depending on how you've configured JBoss session replication, yes.

              The JBoss http session replication codebase is probably where you want to look if you're interested in implementing something similar yourself in an app-server independent manner using JBoss Cache.

              • 4. Re: Caching data per user - multiple instances or not?
                augustientje

                 

                "manik.surtani@jboss.com" wrote:

                It's probably the reason why with JBoss AS you don't have to explicitely put an object again in the Http session after you mutated it in order to get it replicated, right?

                The JBoss http session replication codebase is probably where you want to look if you're interested in implementing something similar yourself in an app-server independent manner using JBoss Cache.


                Well I don't really need the replication. My usecase is that the existing application I'm using JbossCache with is storing a lot of stuff in the session. It's really an overstuffed session anti-pattern, but I can't easily fix that now. Where I would like to use JbossCache for, is for putting an automatic limit on the number of objects in the session. Its eviction policies are probably exactly what I need.

                So my original question remains; what would you suggest? Creating a cache instance per user or one global instance with a node per user?

                • 5. Re: Caching data per user - multiple instances or not?
                  manik

                  Global instance, node per user.

                  • 6. Re: Caching data per user - multiple instances or not?
                    atijms

                     

                    "manik.surtani@jboss.com" wrote:
                    Global instance, node per user.



                    Indeed, you definitely need to go with a global instance. The thing is that if you're using an EvictionPolicy, JbossCache will create a Timer thread for each instance. If you go with the instance per user idea you'll probably end up with a lot of threads in your system.

                    The disadvantage of the single instance is indeed that you need to clear the cache yourself when the user's session ends. Two ways to help with that is setting the time to live for each node programmatically to the same value as your session timeout and installing an HtppSessionListener that removes the entire user node at the sessionDestroyed event.