Return-Path: Delivered-To: apmail-jakarta-taglibs-dev-archive@apache.org Received: (qmail 8661 invoked from network); 19 Mar 2003 22:59:12 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 19 Mar 2003 22:59:12 -0000 Received: (qmail 26429 invoked by uid 97); 19 Mar 2003 23:01:01 -0000 Delivered-To: qmlist-jakarta-archive-taglibs-dev@nagoya.betaversion.org Received: (qmail 26422 invoked from network); 19 Mar 2003 23:01:01 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 19 Mar 2003 23:01:01 -0000 Received: (qmail 7931 invoked by uid 500); 19 Mar 2003 22:59:03 -0000 Mailing-List: contact taglibs-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tag Libraries Developers List" Reply-To: "Tag Libraries Developers List" Delivered-To: mailing list taglibs-dev@jakarta.apache.org Received: (qmail 7838 invoked from network); 19 Mar 2003 22:59:02 -0000 Received: from unknown (HELO sf-inet01.wagerworks.com) (205.158.224.67) by daedalus.apache.org with SMTP; 19 Mar 2003 22:59:02 -0000 Received: (qmail 24524 invoked from network); 19 Mar 2003 13:56:55 -0000 Received: from exchange.corp.wagerworks.com (192.168.1.25) by sf-inet01.wagerworks.com with SMTP; 19 Mar 2003 13:56:55 -0000 content-class: urn:content-classes:message Subject: [PATCH] Cache - Overloading CacheUtil methods MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Wed, 19 Mar 2003 14:59:06 -0800 Message-ID: X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PATCH] Cache - Overloading CacheUtil methods Thread-Index: AcLuayVGVLUdaR1gQHyFeY125YyObw== From: "Andy Bryant" To: X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Currently, all the cache management methods in CacheUtil take a = PageContext object as a parameter regardless of the scope. This = simplifies coding as all other scoped objects are available through the = PageContext object. There are also useful convenience methods for = settings and getting attributes in all scoped objects through the = PageContext. Although this makes the code simple, it unnecessarily complicates using = this class in Java code (as it is intended to be used). A class must = obtain a valid PageContext object somehow from the container. The only = reasonable way of doing this is to get one passed from a JSP page (eg = ). Since page scope caching is only really useful within a single JSP page, = there should be other means for a developer to get access to CacheUtil = methods using HttpSession, ServletRequest or ServletContext. Especially = since these objects can easily be obtained in the web tier from a Front = Controller. I propose overloading all the public methods to take these objects in = place of the PageContext. The caching will work identically to how it = does now, it's just that attributes will be accessed directly from their = scoped objects instead of through the PageContext. This allows developers to perform convenient cache setting = initialization in places like the Servlet init method. It also lets web = tier objects invalidate caches when they become aware that business tier = objects have changed (which usually happens before any JSPs are called). cheers andy PS If it's easier, I can provide the entire file instead of the = following large diff... =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /home/cvspublic/jakarta-taglibs/cache/src/org/apache/taglibs/cache/CacheU= til.java,v retrieving revision 1.1.1.1 diff -r1.1.1.1 CacheUtil.java 57a58,60 > import javax.servlet.ServletContext; > import javax.servlet.http.HttpSession; > import javax.servlet.ServletRequest; 93,94c96,101 < * Retrieves the size of the cache, given a particular scope and < * a context. --- > * Retrieves the size in bytes of the cache, given a particular = scope=20 > * and a context. > * > * @param scope must be one of four scope constants in PageContext > * @param pageContext has access to all scope objects. Obtained > * from a JSP page 99,102c106,133 < if (n =3D=3D null) < return DEFAULT_SIZE; < else < return n.intValue();=20 --- > return getNumber(n, DEFAULT_SIZE); > } >=20 > /** > * Retrieves the size in bytes of the given application scope = cache. > */=20 > public static int getCacheSize(ServletContext servletContext) { > String attribute =3D = getAttributeName(PageContext.APPLICATION_SCOPE, SIZE); > Number n =3D (Number) servletContext.getAttribute(attribute); > return getNumber(n, DEFAULT_SIZE); > } >=20 > /** > * Retrieves the size in bytes of the given session scope cache. > */=20 > public static int getCacheSize(HttpSession session) { > String attribute =3D getAttributeName(PageContext.SESSION_SCOPE, = SIZE); > Number n =3D (Number) session.getAttribute(attribute); > return getNumber(n, DEFAULT_SIZE); > } >=20 > /** > * Retrieves the size in bytes of the given request scope cache. > */=20 > public static int getCacheSize(ServletRequest request) { > String attribute =3D getAttributeName(PageContext.REQUEST_SCOPE, = SIZE); > Number n =3D (Number) request.getAttribute(attribute); > return getNumber(n, DEFAULT_SIZE); 106,107c137,152 < * Retrieves the lifetime of items in the cache, given a = particular < * scope and a context. --- > * Retrieves the size in bytes of the given page scope cache. > */=20 > public static int getCacheSize(PageContext pageContext) { > String attribute =3D getAttributeName(PageContext.PAGE_SCOPE, = SIZE); > Number n =3D (Number) pageContext.getAttribute(attribute); > return getNumber(n, DEFAULT_SIZE); > } > =20 > =20 > /** > * Retrieves the lifetime in seconds of items in the cache,=20 > * given a particular scope and a context. > * > * @param scope must be one of four scope constants in PageContext > * @param pageContext has access to all scope objects. Obtained > * from a JSP page 112,115c157 < if (n =3D=3D null) < return DEFAULT_LIFETIME; < else < return n.intValue(); --- > return getNumber(n, DEFAULT_LIFETIME); 119c161,202 < * Sets a particular cache size to use for new caches in the --- > * Retrieves the lifetime in seconds of items in the > * given application scope cache. > */=20 > public static int getCacheLifetime(ServletContext servletContext) = { > String attribute =3D = getAttributeName(PageContext.APPLICATION_SCOPE, LIFETIME); > Number n =3D (Number) servletContext.getAttribute(attribute); > return getNumber(n, DEFAULT_LIFETIME); > } > =20 > /** > * Retrieves the lifetime in seconds of items in the > * given session scope cache. > */=20 > public static int getCacheLifetime(HttpSession session) { > String attribute =3D getAttributeName(PageContext.SESSION_SCOPE, = LIFETIME); > Number n =3D (Number) session.getAttribute(attribute); > return getNumber(n, DEFAULT_LIFETIME); > } >=20 > /** > * Retrieves the lifetime in seconds of items in the > * given request scope cache. > */=20 > public static int getCacheLifetime(ServletRequest request) { > String attribute =3D getAttributeName(PageContext.REQUEST_SCOPE, = LIFETIME); > Number n =3D (Number) request.getAttribute(attribute); > return getNumber(n, DEFAULT_LIFETIME); > } >=20 > /** > * Retrieves the lifetime in seconds of items in the > * given page scope cache. > */=20 > public static int getCacheLifetime(PageContext pageContext) { > String attribute =3D getAttributeName(PageContext.PAGE_SCOPE, = LIFETIME); > Number n =3D (Number) pageContext.getAttribute(attribute); > return getNumber(n, DEFAULT_LIFETIME); > } >=20 >=20 > /** > * Sets a cache size in bytes to use for new caches in the 123,124c206,243 < String attribute =3D getAttributeName(scope, SIZE); < ctx.setAttribute(attribute, new Integer(size), scope); --- > String attribute =3D getAttributeName(scope, SIZE); > ctx.setAttribute(attribute, new Integer(size), scope); > } >=20 > /** > * Sets a cache size in bytes to use for new caches in the > * given application context. > */ > public static void setCacheSize(int size, ServletContext = servletContext) { > String attribute =3D = getAttributeName(PageContext.APPLICATION_SCOPE, SIZE); > servletContext.setAttribute(attribute, new Integer(size)); > } >=20 > /** > * Sets a cache size in bytes to use for new caches in the > * given session context. > */ > public static void setCacheSize(int size, HttpSession session) { > String attribute =3D getAttributeName(PageContext.SESSION_SCOPE, = SIZE); > session.setAttribute(attribute, new Integer(size)); > } >=20 > /** > * Sets a cache size in bytes to use for new caches in the > * given request context. > */ > public static void setCacheSize(int size, ServletRequest request) = { > String attribute =3D getAttributeName(PageContext.REQUEST_SCOPE, = SIZE); > request.setAttribute(attribute, new Integer(size)); > } >=20 > /** > * Sets a cache size in bytes to use for new caches in the > * given page context. > */ > public static void setCacheSize(int size, PageContext pageContext) = { > String attribute =3D getAttributeName(PageContext.PAGE_SCOPE, = SIZE); > pageContext.setAttribute(attribute, new Integer(size)); 126a246 >=20 135a256,299 > =20 > /** > * Sets a cache lifetime in seconds to use for new caches in the > * given application context. > */ > public static void setCacheLifetime( > int lifetime, ServletContext servletContext) { > String attribute =3D = getAttributeName(PageContext.APPLICATION_SCOPE, > LIFETIME); > servletContext.setAttribute(attribute, new Integer(lifetime)); > } > =20 > /** > * Sets a cache lifetime in seconds to use for new caches in the > * given session context. > */ > public static void setCacheLifetime( > int lifetime, HttpSession session) { > String attribute =3D getAttributeName(PageContext.SESSION_SCOPE, > LIFETIME); > session.setAttribute(attribute, new Integer(lifetime)); > } >=20 > /** > * Sets a cache lifetime in seconds to use for new caches in the > * given request context. > */ > public static void setCacheLifetime( > int lifetime, ServletRequest request) { > String attribute =3D getAttributeName(PageContext.REQUEST_SCOPE, > LIFETIME); > request.setAttribute(attribute, new Integer(lifetime)); > } >=20 > /** > * Sets a cache lifetime in seconds to use for new caches in the > * given page context. > */ > public static void setCacheLifetime( > int lifetime, PageContext pageContext) { > String attribute =3D getAttributeName(PageContext.PAGE_SCOPE, > LIFETIME); > pageContext.setAttribute(attribute, new Integer(lifetime)); > } 152a317,380 > * Retrieves (and creates if necessary) the named LRUCache > * from the given application context. > */ > public static LRUCache getCache(String name, ServletContext = servletContext) { > String att =3D = getAttributePrefixForScope(PageContext.APPLICATION_SCOPE) +=20 > CACHES_PREFIX + name; > LRUCache l =3D (LRUCache) servletContext.getAttribute(att); > if (l =3D=3D null) { > l =3D new LRUCache( > getCacheSize(servletContext), = getCacheLifetime(servletContext)); > servletContext.setAttribute(att, l); > } > return l; > } >=20 > /** > * Retrieves (and creates if necessary) the named LRUCache > * from the given session context. > */ > public static LRUCache getCache(String name, HttpSession session) = { > String att =3D = getAttributePrefixForScope(PageContext.SESSION_SCOPE) +=20 > CACHES_PREFIX + name; > LRUCache l =3D (LRUCache) session.getAttribute(att); > if (l =3D=3D null) { > l =3D new LRUCache( > getCacheSize(session), getCacheLifetime(session)); > session.setAttribute(att, l); > } > return l; > } >=20 > /** > * Retrieves (and creates if necessary) the named LRUCache > * from the given request context. > */ > public static LRUCache getCache(String name, ServletRequest = request) { > String att =3D = getAttributePrefixForScope(PageContext.REQUEST_SCOPE) +=20 > CACHES_PREFIX + name; > LRUCache l =3D (LRUCache) request.getAttribute(att); > if (l =3D=3D null) { > l =3D new LRUCache( > getCacheSize(request), getCacheLifetime(request)); > request.setAttribute(att, l); > } > return l; > } >=20 > /** > * Retrieves (and creates if necessary) the named LRUCache > * from the given page context. > */ > public static LRUCache getCache(String name, PageContext = pageContext) { > String att =3D = getAttributePrefixForScope(PageContext.PAGE_SCOPE) +=20 > CACHES_PREFIX + name; > LRUCache l =3D (LRUCache) pageContext.getAttribute(att); > if (l =3D=3D null) { > l =3D new LRUCache( > getCacheSize(pageContext), getCacheLifetime(pageContext)); > pageContext.setAttribute(att, l); > } > return l; > } >=20 > /** 161a390,429 > * Invalidates an entire cache > */ > public static void invalidateCache( > String name, ServletContext servletContext) { > String att =3D = getAttributePrefixForScope(PageContext.APPLICATION_SCOPE) +=20 > CACHES_PREFIX + name; > servletContext.removeAttribute(att); > } > =20 > /** > * Invalidates an entire cache > */ > public static void invalidateCache( > String name, HttpSession session) { > String att =3D = getAttributePrefixForScope(PageContext.SESSION_SCOPE) +=20 > CACHES_PREFIX + name; > session.removeAttribute(att); > } >=20 > /** > * Invalidates an entire cache > */ > public static void invalidateCache( > String name, ServletRequest request) { > String att =3D = getAttributePrefixForScope(PageContext.REQUEST_SCOPE) +=20 > CACHES_PREFIX + name; > request.removeAttribute(att); > } >=20 > /** > * Invalidates an entire cache > */ > public static void invalidateCache( > String name, PageContext pageContext) { > String att =3D = getAttributePrefixForScope(PageContext.PAGE_SCOPE) +=20 > CACHES_PREFIX + name; > pageContext.removeAttribute(att); > } > =20 > /** 167,169c435,459 < if (l =3D=3D null) < return; // nothing to do < l.remove(key); --- > if (l !=3D null) { > l.remove(key); > } > } >=20 > /** > * Invalidates an individual cache entry (key) > */ > public static void invalidateCachedItem( > String name, String key, ServletContext servletContext) { > LRUCache l =3D getCache(name, servletContext); > if (l !=3D null) { > l.remove(key); > } > } >=20 > /** > * Invalidates an individual cache entry (key) > */ > public static void invalidateCachedItem( > String name, String key, HttpSession session) { > LRUCache l =3D getCache(name, session); > if (l !=3D null) { > l.remove(key); > } 171a462,482 > /** > * Invalidates an individual cache entry (key) > */ > public static void invalidateCachedItem( > String name, String key, ServletRequest request) { > LRUCache l =3D getCache(name, request); > if (l !=3D null) { > l.remove(key); > } > } >=20 > /** > * Invalidates an individual cache entry (key) > */ > public static void invalidateCachedItem( > String name, String key, PageContext pageContext) { > LRUCache l =3D getCache(name, pageContext); > if (l !=3D null) { > l.remove(key); > } > } 175a487,498 > /**=20 > * Return the value of a Number object or defaultValue,=20 > * if it is null.=20 > */ > private static int getNumber(Number n, int defaultValue) { > if (n =3D=3D null) { > return defaultValue; > } else { > return n.intValue();=20 > } > } =20 >=20 --------------------------------------------------------------------- To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org