KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > cache > CacheUtil


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.cache;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20
21 /**
22  * <p>Contains static methods that can be used to save and retrieve
23  * information used to retrieve appropriate caches and to
24  * configure the runtime behavior of the Cache Taglib.</p>
25  *
26  * @author Shawn Bayern
27  */

28
29 public class CacheUtil {
30
31     //*********************************************************************
32
// Constants
33

34     private static final String JavaDoc CACHE_PAGE =
35       "org.apache.taglibs.cache.page";
36     private static final String JavaDoc CACHE_REQUEST =
37       "org.apache.taglibs.cache.request";
38     private static final String JavaDoc CACHE_SESSION =
39       "org.apache.taglibs.cache.session";
40     private static final String JavaDoc CACHE_APPLICATION =
41       "org.apache.taglibs.cache.application";
42
43     private static final String JavaDoc CACHES_PREFIX = "caches.";
44     private static final String JavaDoc LIFETIME = "lifetime";
45     private static final String JavaDoc SIZE = "size";
46
47     private static final int DEFAULT_SIZE = 65536;
48     private static final int DEFAULT_LIFETIME = 300;
49
50     //*********************************************************************
51
// Public methods
52

53     /**
54      * Retrieves the size of the cache, given a particular scope and
55      * a context.
56      */

57     public static int getCacheSize(int scope, PageContext JavaDoc pageContext) {
58       String JavaDoc attribute = getAttributeName(scope, SIZE);
59       Number JavaDoc n = (Number JavaDoc) pageContext.getAttribute(attribute, scope);
60       if (n == null)
61         return DEFAULT_SIZE;
62       else
63         return n.intValue();
64     }
65
66     /**
67      * Retrieves the lifetime of items in the cache, given a particular
68      * scope and a context.
69      */

70     public static int getCacheLifetime(int scope, PageContext JavaDoc pageContext) {
71       String JavaDoc attribute = getAttributeName(scope, LIFETIME);
72       Number JavaDoc n = (Number JavaDoc) pageContext.getAttribute(attribute, scope);
73       if (n == null)
74         return DEFAULT_LIFETIME;
75       else
76         return n.intValue();
77     }
78
79     /**
80      * Sets a particular cache size to use for new caches in the
81      * given scope.
82      */

83     public static void setCacheSize(int size, int scope, PageContext JavaDoc ctx) {
84      String JavaDoc attribute = getAttributeName(scope, SIZE);
85      ctx.setAttribute(attribute, new Integer JavaDoc(size), scope);
86     }
87
88     /**
89      * Sets a particular cache lifetime to use for new caches in the
90      * given scope.
91      */

92     public static void setCacheLifetime(
93         int lifetime, int scope, PageContext JavaDoc ctx) {
94       String JavaDoc attribute = getAttributeName(scope, LIFETIME);
95       ctx.setAttribute(attribute, new Integer JavaDoc(lifetime), scope);
96     }
97
98     /**
99      * Retrieves (and creates if necessary) an appropriate LRUCache
100      * given a scope, name, and context.
101      */

102     public static LRUCache getCache(int scope, String JavaDoc name, PageContext JavaDoc ctx) {
103       String JavaDoc 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     /**
114      * Invalidates an entire cache
115      */

116     public static void invalidateCache(
117         int scope, String JavaDoc name, PageContext JavaDoc pageContext) {
118       String JavaDoc att = getAttributePrefixForScope(scope) + CACHES_PREFIX + name;
119       pageContext.removeAttribute(att, scope);
120     }
121
122     /**
123      * Invalidates an individual cache entry (key)
124      */

125     public static void invalidateCachedItem(
126         int scope, String JavaDoc name, String JavaDoc key, PageContext JavaDoc pageContext) {
127       LRUCache l = getCache(scope, name, pageContext);
128       if (l == null)
129         return; // nothing to do
130
l.remove(key);
131     }
132
133
134     //*********************************************************************
135
// Private utility methods
136

137     private static String JavaDoc getAttributeName(int scope, String JavaDoc variable) {
138       return getAttributePrefixForScope(scope) + variable;
139     }
140
141     private static String JavaDoc 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 JavaDoc("unknown scope");
153       }
154     }
155 }
156
Popular Tags