KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > controller > cache > CacheManager


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.controller.cache;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.concurrent.ConcurrentHashMap JavaDoc;
22
23 import com.jdon.container.pico.Startable;
24 import com.jdon.util.Debug;
25
26 /**
27   * Cacahe Managerment Pattern
28   *
29   * Client objects request objects from a CacheManager object by calling
30   * its fetchObject method. The argument to the fetchObject method is an CacheKey object
31   * that identifies the object to fetch. The fetchObject method works by first calling
32   * the Cache object’s fetchObject method.
33   *
34   * there is one CacheManager in jdon container, you can get it from the container,
35   * do not need create it.
36   *
37  * @author banq
38  */

39 public class CacheManager implements Startable {
40   public static String JavaDoc module = CacheManager.class.getName();
41
42   
43   private Cache cache;
44
45   /**
46    * save the relation CacheKey with DataKey
47    * so later, we can clear all cache about the DataKey
48    *
49    * */

50   private Map JavaDoc cacheKeyMap;
51
52
53   public CacheManager(Cache cache) {
54     this.cache = cache;
55     //cacheKeyMap = Collections.synchronizedMap(new HashMap());
56
cacheKeyMap = new ConcurrentHashMap JavaDoc(); //for above jdk.15
57
}
58
59   public void start() {
60     Debug.logVerbose("[JdonFramework]CacheFactory start .....", module);
61     clear();
62   }
63
64   public void stop() {
65     clear();
66     cache = null;
67     cacheKeyMap = null;
68   }
69
70   public void clear() {
71       if (cache != null)
72         cache.clear();
73       if (cacheKeyMap != null)
74         cacheKeyMap.clear();
75     }
76
77
78   /**
79    * 从缓存中获得缓存
80    * @param cacheKey
81    * @return
82    */

83   public Object JavaDoc fetchObject(StringKey key) {
84     Debug.logVerbose("[JdonFramework]<-cache->try to get cache: " + key, module);
85     Object JavaDoc o = cache.get(key.toString());
86     if (o != null)
87       Debug.logVerbose("[JdonFramework]<-cache->got it, hashcode=" + o.hashCode(), module);
88     return o;
89   }
90
91   /**
92    * ä¿?存到缓存中
93    * @param cacheKey
94    * @param value
95    */

96   public void putObect(StringKey key, Object JavaDoc value) {
97     if (key == null) return;
98    if (!cache.contain(key.toString())){
99      cache.put(key.toString(), value);
100      Debug.logVerbose("[JdonFramework]<-cache->save cache: " + key + ", cache size:" + cache.size(),
101                       module);
102    }
103   }
104
105   /**
106    * 清除缓存
107    * @param cacheKey
108    */

109   public synchronized void removeObect(StringKey key) {
110     cache.remove(key.toString());
111     Debug.logVerbose("[JdonFramework]<-cache->remove the object of " + key + " from cache",
112                      module);
113   }
114
115   /**
116    * 清除缓存中该dataKey的相关所有缓存数æ?®
117    * 当该dataKey相关的数æ?®å¢žåˆ æ”¹æ—¶ï¼Œè°ƒç”¨æœ¬æ–¹æ³•ã€‚以便å?Šæ—¶æ¸…除缓存。
118    *
119    * dataKey是数æ?®çš„ID,如ProductId , ItemIdç­‰
120    * @param dataKey
121    * @param formName
122    */

123   public synchronized void removeCache(Object JavaDoc dataKey, CacheKeyFactory cacheKeyFactory) {
124     if (dataKey == null)
125       return;
126
127     String JavaDoc cacheKeyStr = null;
128
129     Collection JavaDoc dels = cacheKeyFactory.getAllCacheKey(dataKey);
130     Iterator JavaDoc iter = dels.iterator();
131     while (iter.hasNext()) {
132       cacheKeyStr = (String JavaDoc) iter.next();
133       Debug.logVerbose("[JdonFramework]<-cache->remove cache:" + cacheKeyStr, module);
134       removeObject(cacheKeyStr, cacheKeyFactory);
135     }
136   }
137
138   private synchronized void removeObject(String JavaDoc cacheKeyStr, CacheKeyFactory cacheKeyFactory) {
139     try {
140       cache.remove(cacheKeyStr);
141       cacheKeyFactory.removeCacheKey(cacheKeyStr);
142     } catch (Exception JavaDoc e) {
143       Debug.logError("[JdonFramework] remove cache error:" + e + " cacheKey=" + cacheKeyStr,
144                      module);
145     }
146   }
147
148   public Map JavaDoc getCacheKeyMap() {
149     return cacheKeyMap;
150   }
151
152   public void setCacheKeyMap(Map JavaDoc cacheKeyMap) {
153     this.cacheKeyMap = cacheKeyMap;
154   }
155
156   public Cache getCache() {
157     return cache;
158   }
159
160   public void setCache(Cache cache) {
161     this.cache = cache;
162   }
163
164 }
165
Popular Tags