10 Replies Latest reply on May 14, 2010 1:41 PM by mircea.markus

    RemoteCacheManager doesn't implement CacheManager?

    supinko

      Hello,

       

      I just started investigating using Infinispan as a data grid and ran into this puzzlement. I have a setup where we may or may not have a HotRod cluster running in dev. When it is available, I wanted to use RemoteCacheManager, but if not, I wanted to use DefaultCacheManager. My first instinct was to have some logic like:

       

      CacheManager cacheMgr;

      if (usingRemoteGrid) {

        cacheMgr = new RemoteCacheManager();

      } else {

        cacheMgr = new DefaultCacheManager();

      }

       

      ...

      void shutdown() {

        cacheMgr.stop();

      }

       

      Surprisingly, this won't compile because RemoteCacheManager doesn't implement CacheManager. Sure, I could use CacheContainer, but then I wouldn't be able to call cacheMgr.stop() because that's in the Lifecycle interface. Is there some compelling reason why RemoteCacheManager can't implement CacheManager?

       

      Thanks,

      Supin

        • 1. Re: RemoteCacheManager doesn't implement CacheManager?
          mircea.markus

          The reason why RemoteCacheManager doesn't directly subclass CacheManager (but its parent CacheContainer) was the fact that CacheManager also have methods specific to a local cache manager: defineConfiguration, isCoordinator etc

          A way to solve you problem is to work with caches directly, and hold a reference to Lifecycle (both RemoteCM and DefaultCM implement Lifecycle):

           

          Cache c;
          Lifecycle cmLifecycle;
          
          if (usingRemoteGrid) {
            CacheManager cacheMgr  cacheMgr =  new RemoteCacheManager();
            c = cacheMgr.getCache("someName");
            cmLifecycle = cacheMgr;
          } else {
            CacheManager cacheMgr = new  DefaultCacheManager();
            c =  cacheMgr.getCache("someName");
            cmLifecycle = cacheMgr;
          }
          
          c.put("k","v");
          ...
          void shutdown() {
           cmLifecycle .stop();
          }
          

           

           

          But now looking at this example it might make sense indeed to have RemoteCacheManager implement CacheManager and throw these exception. Opinions ?

          • 2. Re: RemoteCacheManager doesn't implement CacheManager?
            meetoblivion

            that sounds like a better idea to me, i nearly broke my keyboard in half until I realized it wasn't inheriting CacheManager (plus, the naming seems weird).  Since it does implement Lifecycle we should have a way to be able to identify a LifecycleCacheContainer maybe?

            • 3. Re: RemoteCacheManager doesn't implement CacheManager?
              manik

              Well if CacheContainer extended Lifecycle we could just do:

               

              Cache c;
              CacheContainer cc;
              
              if (usingRemoteGrid) {
                cc = new RemoteCacheManager();
              } else {
                cc = new DefaultCacheManager();
              }
              
              c = cc.getCache("cacheName");
              
              ... 
              
              void shutdown() {
                cc.stop();
              }
              
              • 4. Re: RemoteCacheManager doesn't implement CacheManager?
                supinko

                Having CacheContainer extend Lifecycle would solve my problem in a pretty low impact way, and it also makes a lot of sense because all CacheContainers need to start and stop anyway.

                 

                The only oddity is that the names of RemoteCacheManager and DefaultCacheManager are so similar to CacheManager that I think the normal instinct would be to use CacheManager as a common interface. One way to accomplish this would be to insert a LocalCacheManager interface or abstract class between CacheManager and DefaultCacheManager. LocalCacheManager would contain all the "extra" methods that aren't in RemoteCacheManager.

                 

                Thanks,

                Supin

                • 5. Re: RemoteCacheManager doesn't implement CacheManager?
                  meetoblivion

                  Would that be a necessary extension point? E.g. would any implementation of CacheContainer need to be a Lifecycle (are there cases where it wouldn't/shouldn't/can't be?

                  • 6. Re: RemoteCacheManager doesn't implement CacheManager?
                    manik

                    Yeah I see what you mean.  Perhaps it makes sense to do something like:

                     

                    cachemanagers.png

                    I don't like using the word LocalCacheManager as it insinuates that you only create local-mode caches from such a manager whereas you can create replicated and distributed caches too.  Embedded is a more correct term as caches created by such a cache manager run within the same JVM.

                    • 7. Re: RemoteCacheManager doesn't implement CacheManager?
                      mircea.markus

                       

                      that sounds like a better idea to me, i nearly broke my keyboard in half until I realized it wasn't inheriting CacheManager (plus, the naming seems weird).  Since it does implement Lifecycle we should have a way to be able to identify a LifecycleCacheContainer maybe?

                      Yep, I think that would make more sense. Sorry for the keyboard.

                      • 8. Re: RemoteCacheManager doesn't implement CacheManager?
                        mircea.markus

                        Yeah I see what you mean.  Perhaps it makes sense to do something like:

                        Yep, that looks as the way it should be, IMO. It brakes the backward-compatibility of CacheManager through. What about going for a compromise solution in 4.1 ( CacheContainer extended Lifecycle) and do the full API change in 5.0 ?

                        • 9. Re: RemoteCacheManager doesn't implement CacheManager?
                          manik

                          I'm not too fussed about doing this in 4.1.0 TBH.  It's still early days and in 4.0.0, people always used DefaultCacheManager anyway (since that was all we had!)  Care to create a JIRA?

                          • 10. Re: RemoteCacheManager doesn't implement CacheManager?
                            mircea.markus

                            here it is:  ISPN-442