📄 cachemanager.java
字号:
/**
* Copyright 2003-2006 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.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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.
*
* there is one CacheManager in jdon container, you can get it from the container,
* do not need create it.
*
* @author banq
*/
public class CacheManager implements Startable {
public static String module = CacheManager.class.getName();
private Cache cache;
/**
* save the relation CacheKey with DataKey
* so later, we can clear all cache about the DataKey
*
* */
private Map cacheKeyMap;
public CacheManager(Cache cache) {
this.cache = cache;
//cacheKeyMap = Collections.synchronizedMap(new HashMap());
cacheKeyMap = new ConcurrentHashMap(); //for above jdk.15
}
public void start() {
Debug.logVerbose("[JdonFramework]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("[JdonFramework]<-cache->try to get cache: " + key, module);
Object o = cache.get(key.toString());
if (o != null)
Debug.logVerbose("[JdonFramework]<-cache->got it, hashcode=" + o.hashCode(), 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("[JdonFramework]<-cache->save cache: " + key + ", cache size:" + cache.size(),
module);
}
}
/**
* 清除缓存
* @param cacheKey
*/
public synchronized void removeObect(StringKey key) {
cache.remove(key.toString());
Debug.logVerbose("[JdonFramework]<-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;
Collection dels = cacheKeyFactory.getAllCacheKey(dataKey);
Iterator iter = dels.iterator();
while (iter.hasNext()) {
cacheKeyStr = (String) iter.next();
Debug.logVerbose("[JdonFramework]<-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("[JdonFramework] 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 + -