Source Code Cross Referenced for SmartCache.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.utils;
007:
008:        import java.util.HashMap;
009:        import java.util.Iterator;
010:
011:        /**
012:         * The SmartCache class is used to store objects in memory for
013:         * a specified amount of time.  The time should be specified in seconds.
014:         * If the time is specified as a negative value, it will be cahced indefinitely.
015:         * @author Ken Weiner, kweiner@unicon.net
016:         * @version $Revision: 34797 $
017:         */
018:        public class SmartCache extends HashMap {
019:            protected int iExpirationTimeout = 3600000; // default to 1 hour
020:
021:            /**
022:             * Instantiate a new SmartCache.  Usually instances of SmartCache are
023:             * declared as static. When retrieving a value from SmartCache, it will
024:             * be null if the value has expired.  It is up to the client to then
025:             * retrieve the value and put it in the cache again.
026:             * Example:
027:             * <code>
028:             * import org.jasig.portal.utils.SmartCache;
029:             *
030:             * public class CacheClient {
031:             *   private static SmartCache cache = new SmartCache(3600); // This cache's values will expire in one hour
032:             *
033:             *   public static void main (String[] args) {
034:             *     // Try to get a value from the cache
035:             *     String aKey = "exampleKey";
036:             *     String aValue = (String)cache.get(aKey);
037:             *
038:             *     if (aValue == null) {
039:             *       // If we are here, the value has either expired or not in the cache
040:             *       // so we will get the value and stuff it in the cache
041:             *       String freshValue = someMethodWhichReturnsAString();
042:             *
043:             *       // Make sure it isn't null before putting it into the cache
044:             *       if (freshValue != null) {
045:             *         cache.put(aKey, freshValue);
046:             *         aValue = freshValue;
047:             *       }
048:             *     }
049:             *
050:             *     System.out.println ("Got the value: " + aValue);
051:             *   }
052:             * }
053:             * </code>
054:             * @param iExpirationTimeout specified in seconds
055:             */
056:            public SmartCache(int iExpirationTimeout) {
057:                super ();
058:                this .iExpirationTimeout = iExpirationTimeout * 1000;
059:            }
060:
061:            /**
062:             * Instantiate SmartCache with a default expiration timeout of one hour.
063:             */
064:            public SmartCache() {
065:                super ();
066:            }
067:
068:            /**
069:             * Add a new value to the cache.  The value will expire in accordance with the
070:             * cache's expiration timeout value which was set when the cache was created.
071:             * @param key the key, typically a String
072:             * @param value the value
073:             * @return the previous value of the specified key in this hashtable, or null if it did not have one.
074:             */
075:            public synchronized Object put(Object key, Object value) {
076:                ValueWrapper valueWrapper = new ValueWrapper(value);
077:                return super .put(key, valueWrapper);
078:            }
079:
080:            /**
081:             * Add a new value to the cache
082:             * @param key the key, typically a String
083:             * @param value the value
084:             * @param lCacheInterval an expiration timeout value, in seconds, which will  
085:             * override the default cache value just for this item. If a negative timeout
086:             * value is specified, the value will be cached indefinitely.                     
087:             * @return the cached object
088:             */
089:            public synchronized Object put(Object key, Object value,
090:                    long lCacheInterval) {
091:                ValueWrapper valueWrapper = new ValueWrapper(value,
092:                        lCacheInterval);
093:                return super .put(key, valueWrapper);
094:            }
095:
096:            /**
097:             * Get an object from the cache.
098:             * @param key the key, typically a String
099:             * @return the value to which the key is mapped in this cache; null if the key is not mapped to any value in this cache.
100:             */
101:            public synchronized Object get(Object key) {
102:                ValueWrapper valueWrapper = (ValueWrapper) super .get(key);
103:                if (valueWrapper != null) {
104:                    // Check if value has expired
105:                    long creationTime = valueWrapper.getCreationTime();
106:                    long cacheInterval = valueWrapper.getCacheInterval();
107:                    long currentTime = System.currentTimeMillis();
108:                    if (cacheInterval >= 0
109:                            && creationTime + cacheInterval < currentTime) {
110:                        remove(key);
111:                        return null;
112:                    }
113:                    return valueWrapper.getValue();
114:                } else
115:                    return null;
116:            }
117:
118:            /**
119:             * Removes from the cache values which have expired.
120:             */
121:            protected void sweepCache() {
122:                for (Iterator keyIterator = keySet().iterator(); keyIterator
123:                        .hasNext();) {
124:                    Object key = keyIterator.next();
125:                    ValueWrapper valueWrapper = (ValueWrapper) super .get(key);
126:                    long creationTime = valueWrapper.getCreationTime();
127:                    long cacheInterval = valueWrapper.getCacheInterval();
128:                    long currentTime = System.currentTimeMillis();
129:                    if (cacheInterval >= 0
130:                            && creationTime + cacheInterval < currentTime) {
131:                        remove(key);
132:                    }
133:                }
134:            }
135:
136:            private class ValueWrapper {
137:                private long lCreationTime = System.currentTimeMillis();
138:                private long lCacheInterval = iExpirationTimeout;
139:                private Object oValue;
140:
141:                protected ValueWrapper(Object oValue) {
142:                    this .oValue = oValue;
143:                }
144:
145:                protected ValueWrapper(Object oValue, long lCacheInterval) {
146:                    this .oValue = oValue;
147:                    this .lCacheInterval = lCacheInterval * 1000;
148:                }
149:
150:                protected Object getValue() {
151:                    return oValue;
152:                }
153:
154:                protected void setValue(Object oValue) {
155:                    this .oValue = oValue;
156:                }
157:
158:                protected long getCreationTime() {
159:                    return lCreationTime;
160:                }
161:
162:                protected void setCreationTime(long lCreationTime) {
163:                    this .lCreationTime = lCreationTime;
164:                }
165:
166:                protected long getCacheInterval() {
167:                    return lCacheInterval;
168:                }
169:
170:                protected void setCacheInterval(long lCacheInterval) {
171:                    this.lCacheInterval = lCacheInterval;
172:                }
173:            }
174:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.