JForum源代码研究—Cache,缓存

 

在JForum中,net.jforum.cache.CacheEngine接口提供了这些方法:

  1. 可以存放或取回多个(key,value)对(其中value的类型为非Map)
  2. 可以存放或取回多个(key,Map)对,而Map中又存放多个(key,value)对(其中value的类型为非Map)

我觉得,只有第一类方法,不要第二类方法也是可以的。之所以有第二类,可能是出于分类管理的考虑。例如,在SessionFacade类中,作者就定义了如下六类:

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";
}

 否则的话,看到cache.add(FQN, us.getSessionId(), us);这样的代码,可能会有疑惑:cache.add(us.getSessionId(), us);不就可以了吗?

 

 

有三个类实现了CacheEngine接口,如图:

DefaultCacheEngine没有使用第三方缓存框架,只是使用了Map cache = new HashMap()。其余两个实现就不多说了。如果谁想了解Ehcache和JBoss Cache的入门知识,就可以参考这两个类。

 

 

还要说的是net.jforum.cache.Cacheable接口,

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

 有11个类实现了该接口,但都类似于

 

public class SessionFacade implements Cacheable
{
	private static CacheEngine cache;

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

 好像没有什么意义。为什么要设计一个Cacheable接口?

 

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);
		}
	}
}

 作者将11个Cacheable实现类都写在配置文件中,并集中设置了setCacheEngine()了。可见,这是Cacheable接口的意义。

 

注意:这11个Cacheable实现类中,除setCacheEngine(CacheEngine engine)方法外,都是静态方法。否则,这种通过注入的方式初始化CacheEngine cache是不可行的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值