KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > query > cache > BlockCacheManager


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.model.query.cache;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 import com.jdon.controller.cache.CacheKey;
25 import com.jdon.controller.cache.CacheKeyFactory;
26 import com.jdon.controller.cache.CacheManager;
27
28 /**
29  * query block manager.
30  * batch query will get a collection result that it is a block,
31  * this block will be cached, next time, we lookup the result
32  * in the block that existed in cache, maybe in the block there
33  * are those promary keys collection, so reducing visiting database.
34  *
35  * the block is made of the primary keys of all models.
36  *
37  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
38  *
39  */

40 public class BlockCacheManager extends CacheKeyFactory {
41     private final static Logger logger = Logger.getLogger(BlockCacheManager.class);
42
43     public final static String JavaDoc CACHE_TYPE_BLOCK = "BLOCK";
44
45     private CacheManager cacheManager;
46
47     private List JavaDoc<CacheKey> cacheKeys;
48
49     public BlockCacheManager(CacheManager cacheManager) {
50         super(cacheManager);
51
52         this.cacheKeys = new CopyOnWriteArrayList JavaDoc<CacheKey>();
53         this.cacheManager = cacheManager;
54
55     }
56
57     public List JavaDoc getBlockKeysFromCache(QueryConditonDatakey qckey) {
58         CacheKey cacheKey = getCacheKey(qckey);
59         return (List JavaDoc) cacheManager.fetchObject(cacheKey);
60
61     }
62
63     public CacheKey getCacheKey(QueryConditonDatakey qckey) {
64         return createCacheKey(qckey, Integer.toString(qckey.getBlockStart()));
65     }
66
67     public void saveBlockKeys(QueryConditonDatakey qckey, List JavaDoc keys) {
68         CacheKey cacheKey = getCacheKey(qckey);
69         cacheManager.putObect(cacheKey, keys);
70         cacheKeys.add(cacheKey);
71     }
72
73     public Integer JavaDoc getAllCountsFromCache(QueryConditonDatakey qckey) {
74         CacheKey cacheKey = getCacheKey(qckey);
75         return (Integer JavaDoc) cacheManager.fetchObject(cacheKey);
76     }
77
78     public void saveAllCounts(QueryConditonDatakey qckey, Integer JavaDoc allCount) {
79         CacheKey cacheKey = getCacheKey(qckey);
80         cacheManager.putObect(cacheKey, allCount);
81         cacheKeys.add(cacheKey);
82     }
83
84     public CacheKey createCacheKeyImp(Object JavaDoc dataKey, String JavaDoc typeName) {
85         return new CacheKey(CACHE_TYPE_BLOCK, dataKey, typeName);
86     }
87
88     public synchronized void clearCache() {
89         try {
90             Iterator JavaDoc iter = cacheKeys.iterator();
91             while (iter.hasNext()) {
92                 CacheKey cacheKey = (CacheKey) iter.next();
93                 cacheManager.removeObect(cacheKey);
94             }
95         } catch (Exception JavaDoc e) {
96             logger.error(e);
97         } finally {
98             cacheKeys.clear();
99         }
100     }
101 }
102
Popular Tags