File Source: WeblogFeedCache.java
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.weblogger.ui.rendering.util.cache;
20
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.weblogger.config.WebloggerConfig;
31 import org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest;
32 import org.apache.roller.weblogger.util.Utilities;
33 import org.apache.roller.weblogger.util.cache.Cache;
34 import org.apache.roller.weblogger.util.cache.CacheManager;
35 import org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry;
36
37
38 /**
39 * Cache for weblog feed content.
40 */
41 public class WeblogFeedCache {
42
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.util.cache.WeblogFeedCache__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@43 != null
*
* Postconditions:
* (soft) log != null
* singletonInstance == &new WeblogFeedCache(WeblogFeedCache__static_init#1)
* new WeblogFeedCache(WeblogFeedCache__static_init#1) num objects == 1
* init'ed(singletonInstance.cacheEnabled)
* init'ed(singletonInstance.contentCache)
*/
43 private static Log log = LogFactory.getLog(WeblogFeedCache.class);
44
45 // a unique identifier for this cache, this is used as the prefix for
46 // roller config properties that apply to this cache
47 public static final String CACHE_ID = "cache.weblogfeed";
48
49 // keep cached content
50 private boolean cacheEnabled = true;
51 private Cache contentCache = null;
52
53 // reference to our singleton instance
54 private static WeblogFeedCache singletonInstance = new WeblogFeedCache();
55
56
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.cache.WeblogFeedCache()
*
* Preconditions:
* log != null
*
* Presumptions:
* java.lang.String:length(...)@70 <= 232-2
* java.util.Enumeration:nextElement(...)@66 != null
* org.apache.roller.weblogger.config.WebloggerConfig:keys(...)@63 != null
*
* Postconditions:
* init'ed(this.cacheEnabled)
* init'ed(this.contentCache)
*
* Test Vectors:
* java.lang.String:startsWith(...)@69: {0}, {1}
* java.util.Enumeration:hasMoreElements(...)@65: {0}, {1}
* org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@59: {0}, {1}
*/
57 private WeblogFeedCache() {
58
59 cacheEnabled = WebloggerConfig.getBooleanProperty(CACHE_ID+".enabled");
60
61 Map cacheProps = new HashMap();
62 cacheProps.put("id", CACHE_ID);
63 Enumeration allProps = WebloggerConfig.keys();
64 String prop = null;
65 while(allProps.hasMoreElements()) {
66 prop = (String) allProps.nextElement();
67
68 // we are only interested in props for this cache
69 if(prop.startsWith(CACHE_ID+".")) {
70 cacheProps.put(prop.substring(CACHE_ID.length()+1),
71 WebloggerConfig.getProperty(prop));
72 }
73 }
74
75 log.info(cacheProps);
76
77 if(cacheEnabled) {
78 contentCache = CacheManager.constructCache(null, cacheProps);
79 } else {
80 log.warn("Caching has been DISABLED");
81 }
82 }
83
84
85 public static WeblogFeedCache getInstance() {
/*
P/P * Method: WeblogFeedCache getInstance()
*
* Preconditions:
* init'ed(singletonInstance)
*
* Postconditions:
* return_value == singletonInstance
* init'ed(return_value)
*/
86 return singletonInstance;
87 }
88
89
90 public Object get(String key, long lastModified) {
91
/*
P/P * Method: Object get(String, long)
*
* Preconditions:
* init'ed(this.cacheEnabled)
* (soft) log != null
* (soft) this.contentCache != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.cacheEnabled: {1}, {0}
* org.apache.roller.weblogger.util.cache.Cache:get(...)@97: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry:getValue(...)@100: Addr_Set{null}, Inverse{null}
*/
92 if(!cacheEnabled)
93 return null;
94
95 Object entry = null;
96
97 LazyExpiringCacheEntry lazyEntry =
98 (LazyExpiringCacheEntry) this.contentCache.get(key);
99 if(lazyEntry != null) {
100 entry = lazyEntry.getValue(lastModified);
101
102 if(entry != null) {
103 log.debug("HIT "+key);
104 } else {
105 log.debug("HIT-EXPIRED "+key);
106 }
107
108 } else {
109 log.debug("MISS "+key);
110 }
111
112 return entry;
113 }
114
115
116 public void put(String key, Object value) {
117
/*
P/P * Method: void put(String, Object)
*
* Preconditions:
* init'ed(this.cacheEnabled)
* (soft) log != null
* (soft) this.contentCache != null
*
* Test Vectors:
* this.cacheEnabled: {1}, {0}
*/
118 if(!cacheEnabled)
119 return;
120
121 contentCache.put(key, new LazyExpiringCacheEntry(value));
122 log.debug("PUT "+key);
123 }
124
125
126 public void remove(String key) {
127
/*
P/P * Method: void remove(String)
*
* Preconditions:
* init'ed(this.cacheEnabled)
* (soft) log != null
* (soft) this.contentCache != null
*
* Test Vectors:
* this.cacheEnabled: {1}, {0}
*/
128 if(!cacheEnabled)
129 return;
130
131 contentCache.remove(key);
132 log.debug("REMOVE "+key);
133 }
134
135
136 public void clear() {
137
/*
P/P * Method: void clear()
*
* Preconditions:
* init'ed(this.cacheEnabled)
* (soft) log != null
* (soft) this.contentCache != null
*
* Test Vectors:
* this.cacheEnabled: {1}, {0}
*/
138 if(!cacheEnabled)
139 return;
140
141 contentCache.clear();
142 log.debug("CLEAR");
143 }
144
145
146 /**
147 * Generate a cache key from a parsed weblog feed request.
148 * This generates a key of the form ...
149 *
150 * <handle>/<type>/<format>/[/category][/language][/excerpts]
151 *
152 * examples ...
153 *
154 * foo/entries/rss/en
155 * foo/comments/rss/MyCategory/en
156 * foo/entries/atom/en/excerpts
157 *
158 */
159 public String generateKey(WeblogFeedRequest feedRequest) {
160
/*
P/P * Method: String generateKey(WeblogFeedRequest)
*
* Preconditions:
* feedRequest != null
* init'ed(feedRequest.excerpts)
* init'ed(feedRequest.format)
* init'ed(feedRequest.locale)
* init'ed(feedRequest.tags)
* init'ed(feedRequest.type)
* init'ed(feedRequest.weblogCategoryName)
* init'ed(feedRequest.weblogHandle)
*
* Presumptions:
* java.util.Set:size(...)@182 >= 0
*
* Postconditions:
* init'ed(java.lang.StringBuffer:toString(...)._tainted)
* return_value == &java.lang.StringBuffer:toString(...)
*
* Test Vectors:
* feedRequest.excerpts: {0}, {1}
* feedRequest.locale: Addr_Set{null}, Inverse{null}
* feedRequest.tags: Addr_Set{null}, Inverse{null}
* feedRequest.weblogCategoryName: Addr_Set{null}, Inverse{null}
* java.util.List:size(...)@180: {-231..0}, {1..232-1}
*/
161 StringBuffer key = new StringBuffer();
162
163 key.append(this.CACHE_ID).append(":");
164 key.append(feedRequest.getWeblogHandle());
165
166 key.append("/").append(feedRequest.getType());
167 key.append("/").append(feedRequest.getFormat());
168
169 if(feedRequest.getWeblogCategoryName() != null) {
170 String cat = feedRequest.getWeblogCategoryName();
171 try {
172 cat = URLEncoder.encode(cat, "UTF-8");
173 } catch (UnsupportedEncodingException ex) {
174 // should never happen, utf-8 is always supported
175 }
176
177 key.append("/").append(cat);
178 }
179
180 if(feedRequest.getTags() != null && feedRequest.getTags().size() > 0) {
181 Set ordered = new TreeSet(feedRequest.getTags());
182 String[] tags = (String[]) ordered.toArray(new String[ordered.size()]);
183 key.append("/tags/").append(Utilities.stringArrayToString(tags,"+"));
184 }
185
186 if(feedRequest.getLocale() != null) {
187 key.append("/").append(feedRequest.getLocale());
188 }
189
190 if(feedRequest.isExcerpts()) {
191 key.append("/excerpts");
192 }
193
194 return key.toString();
195 }
196
197 }
SofCheck Inspector Build Version : 2.18479
| WeblogFeedCache.java |
2009-Jan-02 14:25:34 |
| WeblogFeedCache.class |
2009-Sep-04 03:12:45 |