KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > cache > MapQueryCache


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.cache;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.query.QueryMetadata;
28 import org.apache.commons.collections.map.LRUMap;
29
30 /**
31  * A default implementation of the {@link QueryCache} interface that stores data in a
32  * non-expiring LRUMap.
33  *
34  * @since 3.0
35  * @author Andrus Adamchik
36  */

37 public class MapQueryCache implements QueryCache, Serializable JavaDoc {
38
39     public static final int DEFAULT_CACHE_SIZE = 2000;
40
41     protected Map JavaDoc map;
42
43     public MapQueryCache() {
44         this(DEFAULT_CACHE_SIZE);
45     }
46
47     public MapQueryCache(int maxSize) {
48         this.map = new LRUMap(maxSize);
49     }
50
51     public List JavaDoc get(QueryMetadata metadata) {
52         String JavaDoc key = metadata.getCacheKey();
53         if (key == null) {
54             return null;
55         }
56
57         CacheEntry entry;
58         synchronized (this) {
59             entry = (CacheEntry) map.get(key);
60         }
61
62         return (entry != null) ? entry.list : null;
63     }
64
65     /**
66      * Returns a non-null cached value. If it is not present in the cache, it is obtained
67      * by calling {@link QueryCacheEntryFactory#createObject()} without blocking the cache. As
68      * a result there is a potential of multiple threads to be updating cache in parallel -
69      * this wouldn't lead to corruption of the cache, but can be suboptimal.
70      */

71     public List JavaDoc get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
72         List JavaDoc result = get(metadata);
73         if (result == null) {
74             Object JavaDoc newObject = factory.createObject();
75
76             if (!(newObject instanceof List JavaDoc)) {
77                 if (newObject == null) {
78                     throw new CayenneRuntimeException("Null on cache rebuilding: "
79                             + metadata.getCacheKey());
80                 }
81                 else {
82                     throw new CayenneRuntimeException(
83                             "Invalid query result, expected List, got "
84                                     + newObject.getClass().getName());
85                 }
86             }
87
88             result = (List JavaDoc) newObject;
89             put(metadata, result);
90         }
91
92         return result;
93     }
94
95     public void put(QueryMetadata metadata, List JavaDoc results) {
96         String JavaDoc key = metadata.getCacheKey();
97         if (key != null) {
98
99             CacheEntry entry = new CacheEntry();
100             entry.list = results;
101             entry.cacheGroups = metadata.getCacheGroups();
102
103             synchronized (this) {
104                 map.put(key, entry);
105             }
106         }
107     }
108
109     public void remove(String JavaDoc key) {
110         if (key != null) {
111             synchronized (this) {
112                 map.remove(key);
113             }
114         }
115     }
116
117     public void removeGroup(String JavaDoc groupKey) {
118         if (groupKey != null) {
119             synchronized (this) {
120                 Iterator JavaDoc it = map.values().iterator();
121                 while (it.hasNext()) {
122                     CacheEntry entry = (CacheEntry) it.next();
123                     if (entry.cacheGroups != null) {
124                         for (int i = 0; i < entry.cacheGroups.length; i++) {
125
126                             if (groupKey.equals(entry.cacheGroups[i])) {
127                                 it.remove();
128                                 break;
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135     }
136
137     public void clear() {
138         synchronized (this) {
139             map.clear();
140         }
141     }
142
143     public int size() {
144         return map.size();
145     }
146
147     final class CacheEntry {
148
149         List JavaDoc list;
150         String JavaDoc[] cacheGroups;
151     }
152 }
153
Popular Tags