1 18 19 package org.apache.roller.ui.rendering.util; 20 21 import java.util.Date ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 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 38 public class PlanetCache { 39 40 private static Log log = LogFactory.getLog(PlanetCache.class); 41 42 public static final String CACHE_ID = "cache.planet"; 45 46 private boolean cacheEnabled = true; 48 private Cache contentCache = null; 49 50 private ExpiringCacheEntry lastUpdateTime = null; 52 private long timeout = 15 * 60 * 1000; 53 54 private static PlanetCache singletonInstance = new PlanetCache(); 56 57 58 private PlanetCache() { 59 60 cacheEnabled = RollerConfig.getBooleanProperty(CACHE_ID+".enabled"); 61 62 Map cacheProps = new HashMap (); 63 cacheProps.put("id", CACHE_ID); 64 Enumeration allProps = RollerConfig.keys(); 65 String prop = null; 66 while(allProps.hasMoreElements()) { 67 prop = (String ) allProps.nextElement(); 68 69 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 String timeoutString = RollerConfig.getProperty("cache.planet.timeout"); 86 try { 87 long timeoutSecs = Long.parseLong(timeoutString); 88 this.timeout = timeoutSecs * 1000; 89 } catch(Exception e) { 90 } 92 } 93 94 95 public static PlanetCache getInstance() { 96 return singletonInstance; 97 } 98 99 100 public Object get(String key) { 101 102 if(!cacheEnabled) 103 return null; 104 105 Object 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 key, Object 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 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 getLastModified() { 149 150 Date lastModified = null; 151 152 if(this.lastUpdateTime != null) { 154 lastModified = (Date ) this.lastUpdateTime.getValue(); 155 } 156 157 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 (); 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 193 public String generateKey(PlanetRequest planetRequest) { 194 195 StringBuffer key = new StringBuffer (); 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 key.append("/").append(planetRequest.getLanguage()); 208 209 if(planetRequest.getFlavor() != null) { 210 if(planetRequest.isExcerpts()) { 212 key.append("/excerpts"); 213 } 214 } else { 215 if(planetRequest.getAuthenticUser() != null) { 217 key.append("/user=").append(planetRequest.getAuthenticUser()); 218 } 219 } 220 221 return key.toString(); 222 } 223 224 } 225 | Popular Tags |