KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > PlanetCache


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

18
19 package org.apache.roller.ui.rendering.util;
20
21 import java.util.Date JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.config.RollerConfig;
29 import org.apache.roller.model.RollerFactory;
30 import org.apache.roller.util.cache.Cache;
31 import org.apache.roller.util.cache.CacheManager;
32 import org.apache.roller.util.cache.ExpiringCacheEntry;
33
34
35 /**
36  * Cache for planet content.
37  */

38 public class PlanetCache {
39     
40     private static Log log = LogFactory.getLog(PlanetCache.class);
41     
42     // a unique identifier for this cache, this is used as the prefix for
43
// roller config properties that apply to this cache
44
public static final String JavaDoc CACHE_ID = "cache.planet";
45     
46     // keep cached content
47
private boolean cacheEnabled = true;
48     private Cache contentCache = null;
49     
50     // keep a cached version of last expired time
51
private ExpiringCacheEntry lastUpdateTime = null;
52     private long timeout = 15 * 60 * 1000;
53     
54     // reference to our singleton instance
55
private static PlanetCache singletonInstance = new PlanetCache();
56     
57     
58     private PlanetCache() {
59         
60         cacheEnabled = RollerConfig.getBooleanProperty(CACHE_ID+".enabled");
61         
62         Map JavaDoc cacheProps = new HashMap JavaDoc();
63         cacheProps.put("id", CACHE_ID);
64         Enumeration JavaDoc allProps = RollerConfig.keys();
65         String JavaDoc prop = null;
66         while(allProps.hasMoreElements()) {
67             prop = (String JavaDoc) allProps.nextElement();
68             
69             // we are only interested in props for this cache
70
if(prop.startsWith(CACHE_ID+".")) {
71                 cacheProps.put(prop.substring(CACHE_ID.length()+1),
72                         RollerConfig.getProperty(prop));
73             }
74         }
75         
76         log.info("Planet cache = "+cacheProps);
77         
78         if(cacheEnabled) {
79             contentCache = CacheManager.constructCache(null, cacheProps);
80         } else {
81             log.warn("Caching has been DISABLED");
82         }
83         
84         // lookup our timeout value
85
String JavaDoc timeoutString = RollerConfig.getProperty("cache.planet.timeout");
86         try {
87             long timeoutSecs = Long.parseLong(timeoutString);
88             this.timeout = timeoutSecs * 1000;
89         } catch(Exception JavaDoc e) {
90             // ignored ... illegal value
91
}
92     }
93     
94     
95     public static PlanetCache getInstance() {
96         return singletonInstance;
97     }
98     
99     
100     public Object JavaDoc get(String JavaDoc key) {
101         
102         if(!cacheEnabled)
103             return null;
104         
105         Object JavaDoc entry = contentCache.get(key);
106         
107         if(entry == null) {
108             log.debug("MISS "+key);
109         } else {
110             log.debug("HIT "+key);
111         }
112         
113         return entry;
114     }
115     
116     
117     public void put(String JavaDoc key, Object JavaDoc value) {
118         
119         if(!cacheEnabled)
120             return;
121         
122         contentCache.put(key, value);
123         log.debug("PUT "+key);
124     }
125     
126     
127     public void remove(String JavaDoc key) {
128         
129         if(!cacheEnabled)
130             return;
131         
132         contentCache.remove(key);
133         log.debug("REMOVE "+key);
134     }
135     
136     
137     public void clear() {
138         
139         if(!cacheEnabled)
140             return;
141         
142         contentCache.clear();
143         this.lastUpdateTime = null;
144         log.debug("CLEAR");
145     }
146     
147     
148     public Date JavaDoc getLastModified() {
149         
150         Date JavaDoc lastModified = null;
151         
152         // first try our cached version
153
if(this.lastUpdateTime != null) {
154             lastModified = (Date JavaDoc) this.lastUpdateTime.getValue();
155         }
156         
157         // still null, we need to get a fresh value
158
if(lastModified == null) {
159             
160             try {
161                 lastModified = RollerFactory.getRoller().getPlanetManager().getLastUpdated();
162             } catch (RollerException ex) {
163                 log.error("Error getting planet manager", ex);
164             }
165             
166             if (lastModified == null) {
167                 lastModified = new Date JavaDoc();
168                 log.warn("Can't get lastUpdate time, using current time instead");
169             }
170             
171             this.lastUpdateTime = new ExpiringCacheEntry(lastModified, this.timeout);
172         }
173         
174         return lastModified;
175     }
176     
177     
178     /**
179      * Generate a cache key from a parsed planet request.
180      * This generates a key of the form ...
181      *
182      * <context>/<type>/<language>[/user]
183      * or
184      * <context>/<type>[/flavor]/<language>[/excerpts]
185      *
186      *
187      * examples ...
188      *
189      * planet/page/en
190      * planet/feed/rss/en/excerpts
191      *
192      */

193     public String JavaDoc generateKey(PlanetRequest planetRequest) {
194         
195         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
196         
197         key.append(this.CACHE_ID).append(":");
198         key.append(planetRequest.getContext());
199         key.append("/");
200         key.append(planetRequest.getType());
201         
202         if(planetRequest.getFlavor() != null) {
203             key.append("/").append(planetRequest.getFlavor());
204         }
205         
206         // add language
207
key.append("/").append(planetRequest.getLanguage());
208         
209         if(planetRequest.getFlavor() != null) {
210             // add excerpts
211
if(planetRequest.isExcerpts()) {
212                 key.append("/excerpts");
213             }
214         } else {
215             // add login state
216
if(planetRequest.getAuthenticUser() != null) {
217                 key.append("/user=").append(planetRequest.getAuthenticUser());
218             }
219         }
220         
221         return key.toString();
222     }
223     
224 }
225
Popular Tags