jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 18198] New: - [PATCH] Cache - Overloading CacheUtil methods
Date Thu, 20 Mar 2003 18:41:15 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18198>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18198

[PATCH] Cache - Overloading CacheUtil methods

           Summary: [PATCH] Cache - Overloading CacheUtil methods
           Product: Taglibs
           Version: 1.0
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Cache  Taglib
        AssignedTo: taglibs-dev@jakarta.apache.org
        ReportedBy: andy@wagerworks.com


Again, apologies if you've seen this before. I didn't go through Bugzilla the 
first time...

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 <c:set value="${pageContext}" 
target="${cacheHelper}" property="checkCaches"/>).

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...

===================================================================
RCS file: /home/cvspublic/jakarta-
taglibs/cache/src/org/apache/taglibs/cache/CacheUtil.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 characters of the cache, 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
99,102c106,133
<       if (n == null)
<         return DEFAULT_SIZE;
<       else
<         return n.intValue(); 
---
>       return getNumber(n, DEFAULT_SIZE);
>     }
> 
>     /**
>      * Retrieves the size in characters of the given application scope cache.
>      */ 
>     public static int getCacheSize(ServletContext servletContext) {
>       String attribute = getAttributeName(PageContext.APPLICATION_SCOPE, 
SIZE);
>       Number n = (Number) servletContext.getAttribute(attribute);
>       return getNumber(n, DEFAULT_SIZE);
>     }
> 
>     /**
>      * Retrieves the size in characters of the given session scope cache.
>      */ 
>     public static int getCacheSize(HttpSession session) {
>       String attribute = getAttributeName(PageContext.SESSION_SCOPE, SIZE);
>       Number n = (Number) session.getAttribute(attribute);
>       return getNumber(n, DEFAULT_SIZE);
>     }
> 
>     /**
>      * Retrieves the size in characters of the given request scope cache.
>      */ 
>     public static int getCacheSize(ServletRequest request) {
>       String attribute = getAttributeName(PageContext.REQUEST_SCOPE, SIZE);
>       Number n = (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 characters of the given page scope cache.
>      */ 
>     public static int getCacheSize(PageContext pageContext) {
>       String attribute = getAttributeName(PageContext.PAGE_SCOPE, SIZE);
>       Number n = (Number) pageContext.getAttribute(attribute);
>       return getNumber(n, DEFAULT_SIZE);
>     }
>     
>     
>     /**
>      * Retrieves the lifetime in seconds of items in the cache, 
>      * 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 == 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.
>      */ 
>     public static int getCacheLifetime(ServletContext servletContext) {
>       String attribute = getAttributeName(PageContext.APPLICATION_SCOPE, 
LIFETIME);
>       Number n = (Number) servletContext.getAttribute(attribute);
>       return getNumber(n, DEFAULT_LIFETIME);
>     }
>     
>     /**
>      * Retrieves the lifetime in seconds of items in the
>      * given session scope cache.
>      */ 
>     public static int getCacheLifetime(HttpSession session) {
>       String attribute = getAttributeName(PageContext.SESSION_SCOPE, 
LIFETIME);
>       Number n = (Number) session.getAttribute(attribute);
>       return getNumber(n, DEFAULT_LIFETIME);
>     }
> 
>     /**
>      * Retrieves the lifetime in seconds of items in the
>      * given request scope cache.
>      */ 
>     public static int getCacheLifetime(ServletRequest request) {
>       String attribute = getAttributeName(PageContext.REQUEST_SCOPE, 
LIFETIME);
>       Number n = (Number) request.getAttribute(attribute);
>       return getNumber(n, DEFAULT_LIFETIME);
>     }
> 
>     /**
>      * Retrieves the lifetime in seconds of items in the
>      * given page scope cache.
>      */ 
>     public static int getCacheLifetime(PageContext pageContext) {
>       String attribute = getAttributeName(PageContext.PAGE_SCOPE, LIFETIME);
>       Number n = (Number) pageContext.getAttribute(attribute);
>       return getNumber(n, DEFAULT_LIFETIME);
>     }
> 
> 
>     /**
>      * Sets a cache size in characters to use for new caches in the
123,124c206,243
<      String attribute = getAttributeName(scope, SIZE);
<      ctx.setAttribute(attribute, new Integer(size), scope);
---
>       String attribute = getAttributeName(scope, SIZE);
>       ctx.setAttribute(attribute, new Integer(size), scope);
>     }
> 
>     /**
>      * Sets a cache size in characters to use for new caches in the
>      * given application context.
>      */
>     public static void setCacheSize(int size, ServletContext servletContext) {
>       String attribute = getAttributeName(PageContext.APPLICATION_SCOPE, 
SIZE);
>       servletContext.setAttribute(attribute, new Integer(size));
>     }
> 
>     /**
>      * Sets a cache size in characters to use for new caches in the
>      * given session context.
>      */
>     public static void setCacheSize(int size, HttpSession session) {
>       String attribute = getAttributeName(PageContext.SESSION_SCOPE, SIZE);
>       session.setAttribute(attribute, new Integer(size));
>     }
> 
>     /**
>      * Sets a cache size in characters to use for new caches in the
>      * given request context.
>      */
>     public static void setCacheSize(int size, ServletRequest request) {
>       String attribute = getAttributeName(PageContext.REQUEST_SCOPE, SIZE);
>       request.setAttribute(attribute, new Integer(size));
>     }
> 
>     /**
>      * Sets a cache size in characters to use for new caches in the
>      * given page context.
>      */
>     public static void setCacheSize(int size, PageContext pageContext) {
>       String attribute = getAttributeName(PageContext.PAGE_SCOPE, SIZE);
>       pageContext.setAttribute(attribute, new Integer(size));
126a246
> 
135a256,299
>     
>     /**
>      * 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 = getAttributeName(PageContext.APPLICATION_SCOPE,
>                                           LIFETIME);
>       servletContext.setAttribute(attribute, new Integer(lifetime));
>     }
>     
>     /**
>      * 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 = getAttributeName(PageContext.SESSION_SCOPE,
>                                           LIFETIME);
>       session.setAttribute(attribute, new Integer(lifetime));
>     }
> 
>     /**
>      * 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 = getAttributeName(PageContext.REQUEST_SCOPE,
>                                           LIFETIME);
>       request.setAttribute(attribute, new Integer(lifetime));
>     }
> 
>     /**
>      * 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 = 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 = getAttributePrefixForScope(PageContext.APPLICATION_SCOPE) 
+ 
>         CACHES_PREFIX + name;
>       LRUCache l = (LRUCache) servletContext.getAttribute(att);
>       if (l == null) {
>         l = new LRUCache(
>           getCacheSize(servletContext), getCacheLifetime(servletContext));
>         servletContext.setAttribute(att, l);
>       }
>       return l;
>     }
> 
>     /**
>      * Retrieves (and creates if necessary) the named LRUCache
>      * from the given session context.
>      */
>     public static LRUCache getCache(String name, HttpSession session) {
>       String att = getAttributePrefixForScope(PageContext.SESSION_SCOPE) + 
>         CACHES_PREFIX + name;
>       LRUCache l = (LRUCache) session.getAttribute(att);
>       if (l == null) {
>         l = new LRUCache(
>           getCacheSize(session), getCacheLifetime(session));
>         session.setAttribute(att, l);
>       }
>       return l;
>     }
> 
>     /**
>      * Retrieves (and creates if necessary) the named LRUCache
>      * from the given request context.
>      */
>     public static LRUCache getCache(String name, ServletRequest request) {
>       String att = getAttributePrefixForScope(PageContext.REQUEST_SCOPE) + 
>         CACHES_PREFIX + name;
>       LRUCache l = (LRUCache) request.getAttribute(att);
>       if (l == null) {
>         l = new LRUCache(
>           getCacheSize(request), getCacheLifetime(request));
>         request.setAttribute(att, l);
>       }
>       return l;
>     }
> 
>     /**
>      * Retrieves (and creates if necessary) the named LRUCache
>      * from the given page context.
>      */
>     public static LRUCache getCache(String name, PageContext pageContext) {
>       String att = getAttributePrefixForScope(PageContext.PAGE_SCOPE) + 
>         CACHES_PREFIX + name;
>       LRUCache l = (LRUCache) pageContext.getAttribute(att);
>       if (l == null) {
>         l = new LRUCache(
>           getCacheSize(pageContext), getCacheLifetime(pageContext));
>         pageContext.setAttribute(att, l);
>       }
>       return l;
>     }
> 
>     /**
161a390,429
>      * Invalidates an entire cache
>      */
>     public static void invalidateCache(
>         String name, ServletContext servletContext) {
>       String att = getAttributePrefixForScope(PageContext.APPLICATION_SCOPE) 
+ 
>         CACHES_PREFIX + name;
>       servletContext.removeAttribute(att);
>     }
>     
>     /**
>      * Invalidates an entire cache
>      */
>     public static void invalidateCache(
>         String name, HttpSession session) {
>       String att = getAttributePrefixForScope(PageContext.SESSION_SCOPE) + 
>         CACHES_PREFIX + name;
>       session.removeAttribute(att);
>     }
> 
>     /**
>      * Invalidates an entire cache
>      */
>     public static void invalidateCache(
>         String name, ServletRequest request) {
>       String att = getAttributePrefixForScope(PageContext.REQUEST_SCOPE) + 
>         CACHES_PREFIX + name;
>       request.removeAttribute(att);
>     }
> 
>     /**
>      * Invalidates an entire cache
>      */
>     public static void invalidateCache(
>         String name, PageContext pageContext) {
>       String att = getAttributePrefixForScope(PageContext.PAGE_SCOPE) + 
>         CACHES_PREFIX + name;
>       pageContext.removeAttribute(att);
>     }
>     
>     /**
167,169c435,459
<       if (l == null)
<         return;						// nothing to do
<       l.remove(key);
---
>       if (l != null) {
>         l.remove(key);
>       }
>     }
> 
>     /**
>      * Invalidates an individual cache entry (key)
>      */
>     public static void invalidateCachedItem(
>         String name, String key, ServletContext servletContext) {
>       LRUCache l = getCache(name, servletContext);
>       if (l != null) {
>         l.remove(key);
>       }
>     }
> 
>     /**
>      * Invalidates an individual cache entry (key)
>      */
>     public static void invalidateCachedItem(
>         String name, String key, HttpSession session) {
>       LRUCache l = getCache(name, session);
>       if (l != null) {
>         l.remove(key);
>       }
171a462,482
>     /**
>      * Invalidates an individual cache entry (key)
>      */
>     public static void invalidateCachedItem(
>         String name, String key, ServletRequest request) {
>       LRUCache l = getCache(name, request);
>       if (l != null) {
>         l.remove(key);
>       }
>     }
> 
>     /**
>      * Invalidates an individual cache entry (key)
>      */
>     public static void invalidateCachedItem(
>         String name, String key, PageContext pageContext) {
>       LRUCache l = getCache(name, pageContext);
>       if (l != null) {
>         l.remove(key);
>       }
>     }
175a487,498
>     /** 
>      * Return the value of a Number object or defaultValue, 
>      * if it is null. 
>      */
>     private static int getNumber(Number n, int defaultValue) {
>       if (n == null) {
>         return defaultValue;
>       } else {
>         return n.intValue(); 
>       }
>     }    
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org


Mime
View raw message