KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.config.RollerConfig;
29 import org.apache.roller.util.Utilities;
30 import org.apache.roller.util.cache.Cache;
31 import org.apache.roller.util.cache.CacheManager;
32 import org.apache.roller.util.cache.LazyExpiringCacheEntry;
33
34
35 /**
36  * Cache for weblog feed content.
37  */

38 public class WeblogFeedCache {
39     
40     private static Log log = LogFactory.getLog(WeblogFeedCache.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.weblogfeed";
45     
46     // keep cached content
47
private boolean cacheEnabled = true;
48     private Cache contentCache = null;
49     
50     // reference to our singleton instance
51
private static WeblogFeedCache singletonInstance = new WeblogFeedCache();
52     
53     
54     private WeblogFeedCache() {
55         
56         cacheEnabled = RollerConfig.getBooleanProperty(CACHE_ID+".enabled");
57         
58         Map JavaDoc cacheProps = new HashMap JavaDoc();
59         cacheProps.put("id", CACHE_ID);
60         Enumeration JavaDoc allProps = RollerConfig.keys();
61         String JavaDoc prop = null;
62         while(allProps.hasMoreElements()) {
63             prop = (String JavaDoc) allProps.nextElement();
64             
65             // we are only interested in props for this cache
66
if(prop.startsWith(CACHE_ID+".")) {
67                 cacheProps.put(prop.substring(CACHE_ID.length()+1),
68                         RollerConfig.getProperty(prop));
69             }
70         }
71         
72         log.info(cacheProps);
73         
74         if(cacheEnabled) {
75             contentCache = CacheManager.constructCache(null, cacheProps);
76         } else {
77             log.warn("Caching has been DISABLED");
78         }
79     }
80     
81     
82     public static WeblogFeedCache getInstance() {
83         return singletonInstance;
84     }
85     
86     
87     public Object JavaDoc get(String JavaDoc key, long lastModified) {
88         
89         if(!cacheEnabled)
90             return null;
91         
92         Object JavaDoc entry = null;
93         
94         LazyExpiringCacheEntry lazyEntry =
95                 (LazyExpiringCacheEntry) this.contentCache.get(key);
96         if(lazyEntry != null) {
97             entry = lazyEntry.getValue(lastModified);
98             
99             if(entry != null) {
100                 log.debug("HIT "+key);
101             } else {
102                 log.debug("HIT-EXPIRED "+key);
103             }
104             
105         } else {
106             log.debug("MISS "+key);
107         }
108         
109         return entry;
110     }
111     
112     
113     public void put(String JavaDoc key, Object JavaDoc value) {
114         
115         if(!cacheEnabled)
116             return;
117         
118         contentCache.put(key, new LazyExpiringCacheEntry(value));
119         log.debug("PUT "+key);
120     }
121     
122     
123     public void remove(String JavaDoc key) {
124         
125         if(!cacheEnabled)
126             return;
127         
128         contentCache.remove(key);
129         log.debug("REMOVE "+key);
130     }
131     
132     
133     public void clear() {
134         
135         if(!cacheEnabled)
136             return;
137         
138         contentCache.clear();
139         log.debug("CLEAR");
140     }
141     
142     
143     /**
144      * Generate a cache key from a parsed weblog feed request.
145      * This generates a key of the form ...
146      *
147      * <handle>/<type>/<format>/[/category][/language][/excerpts]
148      *
149      * examples ...
150      *
151      * foo/entries/rss/en
152      * foo/comments/rss/MyCategory/en
153      * foo/entries/atom/en/excerpts
154      *
155      */

156     public String JavaDoc generateKey(WeblogFeedRequest feedRequest) {
157         
158         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
159         
160         key.append(this.CACHE_ID).append(":");
161         key.append(feedRequest.getWeblogHandle());
162         
163         key.append("/").append(feedRequest.getType());
164         key.append("/").append(feedRequest.getFormat());
165         
166         if(feedRequest.getWeblogCategoryName() != null) {
167             String JavaDoc cat = feedRequest.getWeblogCategoryName();
168             try {
169                 cat = URLEncoder.encode(cat, "UTF-8");
170             } catch (UnsupportedEncodingException JavaDoc ex) {
171                 // should never happen, utf-8 is always supported
172
}
173             
174             key.append("/").append(cat);
175         }
176         
177         if(feedRequest.getLocale() != null) {
178             key.append("/").append(feedRequest.getLocale());
179         }
180         
181         if(feedRequest.isExcerpts()) {
182             key.append("/excerpts");
183         }
184         
185         return key.toString();
186     }
187     
188 }
189
Popular Tags