📄 cachemanager.java
字号:
/**
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.controller.cache;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.jdon.container.pico.Startable;
import com.jdon.util.Debug;
/**
* Cacahe Managerment Pattern
*
* Client objects request objects from a CacheManager object by calling
* its fetchObject method. The argument to the fetchObject method is an CacheKey object
* that identifies the object to fetch. The fetchObject method works by first calling
* the Cache object’s fetchObject method.
*
*
*<br>
* 本缓存工厂特点有两个:<br>
* 1. 缓存的Key必须由本类产生<br>
* 2. 本类的remove将自动清除与该Model相关的所有缓存对象。<br>
*<br>
* 凡是需要被缓存的对象,需要两步:<br>
* 1. 在cacheTypes增加相应名称,<br>
* 2. 提供获取该对象Key的名称<br>
*
*
* @author banq
*/
public class CacheManager implements Startable {
public static String module = CacheManager.class.getName();
private Cache cache;
/** 将CacheKey和DataKey 对应关系保存起来
* 可以根据DataKey寻找到不同的CacheKey
* 可以清除DataKey有关的所有缓存
*
* 例如:1875 对应两种缓存:Model和Image,
* 以1875为Key,可以清除它的所有缓存。
*
* */
private Map cacheKeyMap;
/**
* 用于EJB容器
* @return CacheFactory
*/
public CacheManager(Cache cache) {
this.cache = cache;
cacheKeyMap = Collections.synchronizedMap(new HashMap());
}
public void start() {
Debug.logVerbose("CacheFactory start .....", module);
clear();
}
public void stop() {
clear();
cache = null;
cacheKeyMap = null;
}
public void clear() {
if (cache != null)
cache.clear();
if (cacheKeyMap != null)
cacheKeyMap.clear();
}
/**
* 从缓存中获得缓存
* @param cacheKey
* @return
*/
public Object fetchObject(StringKey key) {
Debug.logVerbose("<-cache->try to get cache: " + key, module);
Object o = cache.get(key.toString());
if (o != null)
Debug.logVerbose("<-cache->got it", module);
return o;
}
/**
* 保存到缓存中
* @param cacheKey
* @param value
*/
public void putObect(StringKey key, Object value) {
if (key == null) return;
if (!cache.contain(key.toString())){
cache.put(key.toString(), value);
Debug.logVerbose("<-cache->save cache: " + key + ", size:" + cache.size(),
module);
}
}
/**
* 清除缓存
* @param cacheKey
*/
public synchronized void removeObect(StringKey key) {
cache.remove(key.toString());
Debug.logVerbose("<-cache->remove the object of " + key + " from cache",
module);
}
/**
* 清除缓存中该dataKey的相关所有缓存数据
* 当该dataKey相关的数据增删改时,调用本方法。以便及时清除缓存。
*
* dataKey是数据的ID,如ProductId , ItemId等
* @param dataKey
* @param formName
*/
public synchronized void removeCache(Object dataKey, CacheKeyFactory cacheKeyFactory) {
if (dataKey == null)
return;
String cacheKeyStr = null;
Iterator iter = cacheKeyFactory.getAllCacheKey(dataKey).iterator();
while (iter.hasNext()) {
cacheKeyStr = (String) iter.next();
Debug.logVerbose("<-cache->remove cache:" + cacheKeyStr, module);
removeObject(cacheKeyStr, cacheKeyFactory);
}
}
private synchronized void removeObject(String cacheKeyStr, CacheKeyFactory cacheKeyFactory) {
try {
cache.remove(cacheKeyStr);
cacheKeyFactory.removeCacheKey(cacheKeyStr);
} catch (Exception e) {
Debug.logError(" remove cache error:" + e + " cacheKey=" + cacheKeyStr,
module);
}
}
public Map getCacheKeyMap() {
return cacheKeyMap;
}
public void setCacheKeyMap(Map cacheKeyMap) {
this.cacheKeyMap = cacheKeyMap;
}
public Cache getCache() {
return cache;
}
public void setCache(Cache cache) {
this.cache = cache;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -