1 18 19 package org.apache.roller.ui.rendering.util; 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 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 38 public class WeblogFeedCache { 39 40 private static Log log = LogFactory.getLog(WeblogFeedCache.class); 41 42 public static final String CACHE_ID = "cache.weblogfeed"; 45 46 private boolean cacheEnabled = true; 48 private Cache contentCache = null; 49 50 private static WeblogFeedCache singletonInstance = new WeblogFeedCache(); 52 53 54 private WeblogFeedCache() { 55 56 cacheEnabled = RollerConfig.getBooleanProperty(CACHE_ID+".enabled"); 57 58 Map cacheProps = new HashMap (); 59 cacheProps.put("id", CACHE_ID); 60 Enumeration allProps = RollerConfig.keys(); 61 String prop = null; 62 while(allProps.hasMoreElements()) { 63 prop = (String ) allProps.nextElement(); 64 65 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 get(String key, long lastModified) { 88 89 if(!cacheEnabled) 90 return null; 91 92 Object 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 key, Object 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 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 156 public String generateKey(WeblogFeedRequest feedRequest) { 157 158 StringBuffer key = new StringBuffer (); 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 cat = feedRequest.getWeblogCategoryName(); 168 try { 169 cat = URLEncoder.encode(cat, "UTF-8"); 170 } catch (UnsupportedEncodingException ex) { 171 } 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 |