1 16 17 package org.apache.taglibs.cache; 18 19 import javax.servlet.jsp.PageContext ; 20 21 28 29 public class CacheUtil { 30 31 34 private static final String CACHE_PAGE = 35 "org.apache.taglibs.cache.page"; 36 private static final String CACHE_REQUEST = 37 "org.apache.taglibs.cache.request"; 38 private static final String CACHE_SESSION = 39 "org.apache.taglibs.cache.session"; 40 private static final String CACHE_APPLICATION = 41 "org.apache.taglibs.cache.application"; 42 43 private static final String CACHES_PREFIX = "caches."; 44 private static final String LIFETIME = "lifetime"; 45 private static final String SIZE = "size"; 46 47 private static final int DEFAULT_SIZE = 65536; 48 private static final int DEFAULT_LIFETIME = 300; 49 50 53 57 public static int getCacheSize(int scope, PageContext pageContext) { 58 String attribute = getAttributeName(scope, SIZE); 59 Number n = (Number ) pageContext.getAttribute(attribute, scope); 60 if (n == null) 61 return DEFAULT_SIZE; 62 else 63 return n.intValue(); 64 } 65 66 70 public static int getCacheLifetime(int scope, PageContext pageContext) { 71 String attribute = getAttributeName(scope, LIFETIME); 72 Number n = (Number ) pageContext.getAttribute(attribute, scope); 73 if (n == null) 74 return DEFAULT_LIFETIME; 75 else 76 return n.intValue(); 77 } 78 79 83 public static void setCacheSize(int size, int scope, PageContext ctx) { 84 String attribute = getAttributeName(scope, SIZE); 85 ctx.setAttribute(attribute, new Integer (size), scope); 86 } 87 88 92 public static void setCacheLifetime( 93 int lifetime, int scope, PageContext ctx) { 94 String attribute = getAttributeName(scope, LIFETIME); 95 ctx.setAttribute(attribute, new Integer (lifetime), scope); 96 } 97 98 102 public static LRUCache getCache(int scope, String name, PageContext ctx) { 103 String att = getAttributePrefixForScope(scope) + CACHES_PREFIX + name; 104 LRUCache l = (LRUCache) ctx.getAttribute(att, scope); 105 if (l == null) { 106 l = new LRUCache( 107 getCacheSize(scope, ctx), getCacheLifetime(scope, ctx)); 108 ctx.setAttribute(att, l, scope); 109 } 110 return l; 111 } 112 113 116 public static void invalidateCache( 117 int scope, String name, PageContext pageContext) { 118 String att = getAttributePrefixForScope(scope) + CACHES_PREFIX + name; 119 pageContext.removeAttribute(att, scope); 120 } 121 122 125 public static void invalidateCachedItem( 126 int scope, String name, String key, PageContext pageContext) { 127 LRUCache l = getCache(scope, name, pageContext); 128 if (l == null) 129 return; l.remove(key); 131 } 132 133 134 137 private static String getAttributeName(int scope, String variable) { 138 return getAttributePrefixForScope(scope) + variable; 139 } 140 141 private static String getAttributePrefixForScope(int scope) { 142 switch(scope) { 143 case PageContext.PAGE_SCOPE: 144 return CACHE_PAGE + "."; 145 case PageContext.REQUEST_SCOPE: 146 return CACHE_REQUEST + "."; 147 case PageContext.SESSION_SCOPE: 148 return CACHE_SESSION + "."; 149 case PageContext.APPLICATION_SCOPE: 150 return CACHE_APPLICATION + "."; 151 default: 152 throw new IllegalArgumentException ("unknown scope"); 153 } 154 } 155 } 156 | Popular Tags |