KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > cache > ModelCacheManager


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

14
15 package com.jdon.model.cache;
16
17 import com.jdon.container.pico.Startable;
18 import com.jdon.controller.cache.CacheKey;
19 import com.jdon.controller.cache.CacheKeyFactory;
20 import com.jdon.controller.cache.CacheManager;
21 import com.jdon.controller.model.ModelIF;
22 import com.jdon.model.config.ModelMapping;
23 import com.jdon.model.factory.ModelHandlerClassBuilder;
24 import com.jdon.util.Debug;
25
26 public class ModelCacheManager implements Startable {
27
28     private final static String JavaDoc module = ModelCacheManager.class.getName();
29
30     protected CacheManager cacheManager;
31
32     private CacheKeyFactory cacheKeyFactory;
33
34     private ModelHandlerClassBuilder modelFactory;
35
36     public ModelCacheManager(ModelHandlerClassBuilder modelFactory, CacheManager cacheManager) {
37         this.cacheManager = cacheManager;
38         this.modelFactory = modelFactory;
39         this.cacheKeyFactory = new ModelCacheKeyFactory(cacheManager);
40     }
41
42     public void start() {
43         Debug.logVerbose("[JdonFramework]ModelCacheManager start ...", module);
44     }
45
46     public void stop() {
47
48     }
49
50     /**
51      * 将数æ?®ID, Formå??称组å?ˆæˆ?Key, 将对应的Modelä¿?存到缓存
52      *
53      * 基于一个Formå??称对应一个Model类型
54      *
55      * @param dataKey
56      * @param formName
57      * @param model
58      */

59     public void setCache(Object JavaDoc dataKey, String JavaDoc formName, ModelIF model) {
60         CacheKey cachKey = getCacheKey(dataKey, formName);
61         if (cachKey != null)
62             saveToCache(cachKey, model);
63     }
64
65     private void saveToCache(CacheKey cachKey, ModelIF model) {
66         if ((model != null) && (model.isCacheable())) { //ä¿?存到缓存中
67
Debug.logVerbose("[JdonFramework]save cache: " + cachKey + " hashCode:" + model.hashCode(), module);
68             cacheManager.putObect(cachKey, model);
69         }
70     }
71
72     /**
73      * 将数æ?®ID, Model类类型组å?ˆæˆ?Key, 将对应的Modelä¿?存到缓存
74      *
75      * @param dataKey
76      * @param modelClassName
77      * @param model
78      */

79     public void setCache2(Object JavaDoc dataKey, String JavaDoc modelClassName, ModelIF model) {
80         CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
81         if (cachKey != null)
82             saveToCache(cachKey, model);
83     }
84
85     /**
86      * 将数æ?®ID, Formå??称组å?ˆæˆ?Key的对应Model从缓存中å?–出
87      *
88      * 基于一个Formå??称对应一个Model类型
89      *
90      * @param dataKey
91      * @param formName
92      * @return Model
93      */

94     public ModelIF getCache(Object JavaDoc dataKey, String JavaDoc formName) {
95         CacheKey cachKey = getCacheKey(dataKey, formName);
96         if (cachKey != null)
97             return getModelFromCache(cachKey);
98         else
99             return null;
100     }
101
102     private ModelIF getModelFromCache(CacheKey cachKey) {
103         
104         ModelIF model = (ModelIF) cacheManager.fetchObject(cachKey);
105         if (model != null){
106             Debug.logVerbose("[JdonFramework]found cache: " + cachKey + " hashCode:" + model.hashCode(), module);
107             if ( model.isModified()){
108                 Debug.logVerbose("[JdonFramework]<-cache-> model is isModified():" + cachKey, module);
109                 model = null;
110             }
111         }
112         return model;
113     }
114
115     /**
116      * 将数æ?®ID, Model类类型组å?ˆæˆ?Key的对应Model从缓存中å?–出
117      *
118      * @param dataKey
119      * @param modelClassName
120      * @return Model
121      */

122     public ModelIF getCache2(Object JavaDoc dataKey, String JavaDoc modelClassName) {
123         CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
124         if (cachKey != null)
125             return getModelFromCache(cachKey);
126         else
127             return null;
128     }
129
130     /**
131      * 清除缓存中该dataKey的相关所有缓存数æ?® dataKeyå?³æ•°æ?®çš„主键ID数值
132      *
133      * @param dataKey
134      * @param formName
135      */

136     public void removeCache(Object JavaDoc dataKey) {
137         cacheManager.removeCache(dataKey, cacheKeyFactory);
138     }
139
140     /**
141      * 将数æ?®ID, Model类类型组å?ˆæˆ?Key的对应Model从缓存中å?–出
142      *
143      * @param dataKey
144      * @param modelClassName
145      * @return
146      */

147     public void removeCache2(Object JavaDoc dataKey, String JavaDoc modelClassName) {
148         CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
149         if (cachKey != null)
150             cacheManager.removeObect(cachKey);
151     }
152
153     /**
154      * 生æˆ?缓存中ä¿?å­˜Modelçš„Key,根æ?®ActionFormå??称
155      *
156      * @param dataKey
157      * @param formName
158      * @return String
159      */

160     public CacheKey getCacheKey(Object JavaDoc dataKey, String JavaDoc formName) {
161         ModelMapping modelMapping = this.modelFactory.getModelMapping(formName);
162         return cacheKeyFactory.createCacheKey(dataKey, modelMapping.getClassName());
163     }
164
165     public CacheKey getCacheKey2(Object JavaDoc dataKey, String JavaDoc modelClassName) {
166         return cacheKeyFactory.createCacheKey(dataKey, modelClassName);
167     }
168
169     public void clearCache() {
170         cacheManager.clear();
171     }
172
173 }
174
Popular Tags