1 package info.magnolia.cms.cache; 2 3 import info.magnolia.cms.beans.config.ConfigurationException; 4 import info.magnolia.cms.beans.config.MIMEMapping; 5 import info.magnolia.cms.cache.noop.NoOpCache; 6 import info.magnolia.cms.util.ClassUtil; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 import org.apache.commons.lang.StringUtils; 12 import org.slf4j.Logger; 13 import org.slf4j.LoggerFactory; 14 15 16 21 public class DefaultCacheManager extends BaseCacheManager { 22 23 private static final Logger log = LoggerFactory.getLogger(DefaultCacheManager.class); 24 25 private static final Cache NOOP_CACHE = new NoOpCache(); 26 27 private Cache cache = NOOP_CACHE; 28 29 32 public CacheKey getCacheKey(HttpServletRequest request) { 33 return new CacheKey(request); 34 } 35 36 39 public boolean isCacheable(HttpServletRequest request) { 40 41 if (StringUtils.equalsIgnoreCase(request.getMethod(), "POST")) { 42 return false; } 44 45 if (!request.getParameterMap().isEmpty()) { 46 return false; } 48 49 if (StringUtils.isEmpty(MIMEMapping.getMIMEType(request))) { 50 return false; } 52 53 return getConfig().isUriCacheable(request); 54 } 55 56 59 public boolean canCompress(HttpServletRequest request) { 60 61 String extension = StringUtils.substringAfter(request.getRequestURI(), "."); 62 return getConfig().canCompress(extension); 63 } 64 65 public boolean cacheRequest(CacheKey key, CacheableEntry entry, boolean canCompress) { 66 cache.cacheRequest(key, entry, canCompress); 67 return true; 68 } 69 70 protected void doFlushAll() { 71 this.cache.flush(); 72 } 73 74 protected long doGetCreationTime(CacheKey request) { 75 return this.cache.getCreationTime(request); 76 } 77 78 protected void doStart() { 79 try { 80 CacheConfig config = getConfig(); 81 82 Cache cache = newCache(config.getCacheImplementation()); 83 cache.start(config); 84 85 this.cache = cache; 86 } 87 catch (ConfigurationException e) { 88 } 90 } 91 92 protected void doStop() { 93 this.cache.stop(); 94 this.cache = NOOP_CACHE; 95 } 96 97 protected boolean doStreamFromCache(CacheKey key, HttpServletResponse response, boolean canCompress) { 98 if (!isRunning()) { 99 return false; 100 } 101 102 boolean didUseCache = this.cache.streamFromCache(key, response, canCompress); 103 104 if (log.isDebugEnabled()) { 105 if (didUseCache) { 106 log.debug("Used cache for: '{}'.", key); 107 } 108 else { 109 log.debug("Not found in cache: '{}'.", key); 110 } 111 } 112 113 return didUseCache; 114 } 115 116 protected Cache getCache() { 117 return this.cache; 118 } 119 120 private boolean isCached(CacheKey request) { 121 return this.cache.isCached(request); 122 } 123 124 private Cache newCache(String cacheImplementation) throws ConfigurationException { 125 try { 126 return (Cache) ClassUtil.newInstance(cacheImplementation); 127 } 128 catch (ClassNotFoundException e) { 129 throw new ConfigurationException("Cache class not found!", e); 130 } 131 catch (InstantiationException e) { 132 throw new ConfigurationException("Could not instantiate Cache class!", e); 133 } 134 catch (IllegalAccessException e) { 135 throw new ConfigurationException("Could not access Cache class!", e); 136 } 137 catch (ClassCastException e) { 138 throw new ConfigurationException("Class does not implement Cache!", e); 139 } 140 } 141 142 } 143 | Popular Tags |