About Cache, Cache

The JForum in, net.jforum.cache.CacheEngine interface provides these methods:

  1. Can store or retrieve multiple (key, value) pairs (where value of the type of non-Map)
  2. Can store or retrieve multiple (key, Map) pairs, while the Map in another store multiple (key, value) pairs (where value of the type of non-Map)

I think, only the first class method, do not second class methods are also possible. The reason why there is a second class, probably because of classified management considerations. For example, in SessionFacade class, the author defines the following six categories:

public class SessionFacade
{       
        private static final String FQN = "sessions";
        private static final String FQN_LOGGED = FQN + "/logged";
        private static final String FQN_COUNT = FQN + "/count";
        private static final String FQN_USER_ID = FQN + "/userId";
        private static final String ANONYMOUS_COUNT = "anonymousCount";
        private static final String LOGGED_COUNT = "loggedCount";
}

Otherwise, see cache.add (FQN, us.getSessionId (), us); such a code, there may be wondering: cache.add (us.getSessionId (), us); not that make it?

There are three class implements CacheEngine interface, shown in Figure:

JForum source code study (7)

DefaultCacheEngine do not use third-party caching framework, and is using the Map cache = new HashMap (). The remaining two achieved no more to say. Anyone who wants to understand the Ehcache and JBoss Cache introductory knowledge, you can refer to these two classes.

Would also like to say is that net.jforum.cache.Cacheable interface,

public interface Cacheable
{
        /**
         * Sets the cache engine instance.
         * @param engine The instance of the cache engine
         */
        public void setCacheEngine(CacheEngine engine);
}

There are 11 class implements this interface, but all similar to the

public class SessionFacade implements Cacheable
{
        private static CacheEngine cache;

        public void setCacheEngine(CacheEngine engine)
        {
                cache = engine;
        }
}       

Does not seem to make sense. Why design a Cacheable interface?

See

package net.jforum;

public class ConfigLoader {     

public static void startCacheEngine()
        {
                try {
                        String cacheImplementation = SystemGlobals.getValue(ConfigKeys.CACHE_IMPLEMENTATION);
                        logger.info("Using cache engine: " + cacheImplementation);
                        
                        cache = (CacheEngine)Class.forName(cacheImplementation).newInstance();
                        cache.init();
                        
                        String s = SystemGlobals.getValue(ConfigKeys.CACHEABLE_OBJECTS);
                        if (s == null || s.trim().equals("")) {
                                logger.warn("Cannot find Cacheable objects to associate the cache engine instance.");
                                return;
                        }
                        
                        String[] cacheableObjects = s.split(",");
                        for (int i = 0; i < cacheableObjects.length; i++) {
                                logger.info("Creating an instance of " + cacheableObjects[i]);
                                Object o = Class.forName(cacheableObjects[i].trim()).newInstance();
                                
                                if (o instanceof Cacheable) {
                                        ((Cacheable)o).setCacheEngine(cache);
                                }
                                else {
                                        logger.error(cacheableObjects[i] + " is not an instance of net.jforum.cache.Cacheable");
                                }
                        }
                }
                catch (Exception e) {
                        throw new CacheEngineStartupException("Error while starting the cache engine", e);
                }
        }
}

The authors achieved 11 Cacheable classes are written in the configuration file, and concentrated set setCacheEngine () has. Can be seen, this is the meaning of Cacheable interface.

Note: This 11 Cacheable implementation class, in addition to setCacheEngine (CacheEngine engine) methods, all static methods. Otherwise, this by injecting a way to initialize CacheEngine cache is not feasible.