CacheManager是Ehcache的缓存主要管理类.支持单例模式和工厂模式. 主要用于管理Ehcache里面有的Cache. 可以使用CacheManager来创建Cache.
如:
CacheManager manager = CacheManager.newInstance("src/config/cache.xml");
manager.addCache("testCache");
Cache cache = singletonManager.getCache("testCache");
manager.removeCache("testCache");
具体实现如下:
单例模式:
/**
* The Singleton Instance.
*/
private static volatile CacheManager singleton;
/**
* A factory method to create a singleton CacheManager with default config, or return it if it exists.
* <p/>
* The configuration will be read, {@link Ehcache}s created and required stores initialized. When the {@link CacheManager} is no longer
* required, call shutdown to free resources.
*
* @return the singleton CacheManager
* @throws CacheException
* if the CacheManager cannot be created
*/
public static CacheManager create() throws CacheException {
if (singleton != null) {
LOG.debug("Attempting to create an existing singleton. Existing singleton returned.");
return singleton;
}
synchronized (CacheManager.class) {
if (singleton == null) {
singleton = newInstance();
} else {
LOG.debug("Attempting to create an existing singleton. Existing singleton returned.");
}
return singleton;
}
}
public static CacheManager getInstance() throws CacheException {
return CacheManager.create();
}
工厂模式:
CacheManager集合:
private static final Map<String, CacheManager> CACHE_MANAGERS_MAP = new HashMap<String, CacheManager>();
private static final IdentityHashMap<CacheManager, String> CACHE_MANAGERS_REVERSE_MAP = new IdentityHashMap<CacheManager, String>();
先判断集合中是否存在CacheManger,如里没有,创建一个新的CacheManager并存入集合中.
private static CacheManager newInstance(Configuration configuration, String msg) throws CacheException {
synchronized (CacheManager.class) {
String name = configuration.getName();
if (name == null) {
name = DEFAULT_NAME;
}
CacheManager cacheManager = CACHE_MANAGERS_MAP.get(name);
if (cacheManager == null) {
LOG.debug(msg);
cacheManager = new CacheManager(configuration);//此步会存入集合.
}
return cacheManager;
}
}
初始化Cache,并添加到ehcaches集合中:
/**
* Ehcaches managed by this manager.
*/
private final ConcurrentMap<String, Ehcache> ehcaches = new ConcurrentHashMap<String, Ehcache>();
private Ehcache addCacheNoCheck(final Ehcache cache, final boolean strict) throws IllegalStateException, ObjectExistsException,
CacheException {
if (cache.getStatus() != Status.STATUS_UNINITIALISED) {
throw new CacheException("Trying to add an already initialized cache." + " If you are adding a decorated cache, "
+ "use CacheManager.addDecoratedCache" + "(Ehcache decoratedCache) instead.");
}
if (cache.getCacheConfiguration().isTerracottaClustered() && terracottaClient.getClusteredInstanceFactory() == null) {
throw new CacheException(String.format("Trying to add terracotta cache %s but no <terracottaConfig> element was " +
"used to specify the Terracotta configuration on the CacheManager %s.",
cache.getName(), getName()));
}
Ehcache ehcache = ehcaches.get(cache.getName());
if (ehcache != null) {
if (strict) {
throw new ObjectExistsException("Cache " + cache.getName() + " already exists");
} else {
return ehcache;
}
}
initializingCaches.put(cache.getName(), cache);
try {
initializeEhcache(cache, true);
ehcache = ehcaches.putIfAbsent(cache.getName(), cache);
if (ehcache != null) {
throw new AssertionError();
}
} finally {
initializingCaches.remove(cache.getName());
}
// Don't notify initial config. The init method of each listener should take care of this.
if (status.equals(Status.STATUS_ALIVE)) {
cacheManagerEventListenerRegistry.notifyCacheAdded(cache.getName());
}
return cache;
}