Source Code Cross Referenced for LRUCache.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » concurrency » caching » 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.concurrency.caching 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2002 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.concurrency.caching;
007:
008:        import java.util.HashMap;
009:
010:        /**
011:         * A rewrite of SmartCache that uses a moderate LRU algorithm:  entries
012:         * are purged from the cache via periodic sweeps rather than in response to
013:         * specific cache additions.  Note that sweeps have to be kicked off
014:         * externally, e.g.,
015:         * <p>
016:         * <code>
017:         *   int MAX_CACHE_SIZE = 1000;<br>
018:         *   int MAX_UNUSED_TIME_MILLIS = 30*60*1000;<br>
019:         *   LRUCache cache = new LRUCache(MAX_CACHE_SIZE, MAX_UNUSED_TIME_MILLIS);<br>
020:         *   // ... put stuff in ...<br>
021:         *   cache.sweepCache()<br>
022:         *   // ... put more stuff in ...<br>
023:         * </code>
024:         * <p>
025:         * At the end of the sweep, the cache will have no more (and possibly less)
026:         * than <code>maxSize</code> entries, though the sweep may have to reduce
027:         * <code>maxUnusedTimeMillis</code> in order to get there.
028:         * <p>
029:         * @author Ken Weiner
030:         * @author Dan Ellentuck
031:         * @version $Revision: 34996 $
032:         * @see org.jasig.portal.utils.SmartCache
033:         */
034:        public class LRUCache extends HashMap {
035:            // Maximum size of cache, after sweep.  Defaults to 1000.
036:            protected static int DEFAULT_MAX_SIZE = 1000;
037:            protected int maxSize;
038:
039:            // Maximum unused time for cache entries, used only when size
040:            // exceeds maxSize.  Defaults to 30 minutes.
041:            protected static int DEFAULT_MAX_UNUSED_TIME_MILLIS = 30 * 60 * 1000;
042:            protected int maxUnusedTimeMillis;
043:
044:            // Wrapper adds last used timestamp.
045:            private class ValueWrapper {
046:                private long lastReferenceTime = System.currentTimeMillis();
047:                private Object oValue;
048:
049:                protected ValueWrapper(Object oValue) {
050:                    this .oValue = oValue;
051:                }
052:
053:                protected Object getValue() {
054:                    return oValue;
055:                }
056:
057:                protected void setValue(Object oValue) {
058:                    this .oValue = oValue;
059:                }
060:
061:                protected long getLastReferenceTime() {
062:                    return lastReferenceTime;
063:                }
064:
065:                protected void resetLastReferenceTime() {
066:                    this .lastReferenceTime = System.currentTimeMillis();
067:                }
068:            }
069:
070:            /**
071:             */
072:            public LRUCache() {
073:                this (DEFAULT_MAX_SIZE, DEFAULT_MAX_UNUSED_TIME_MILLIS);
074:            }
075:
076:            /**
077:             */
078:            public LRUCache(int size) {
079:                this (size, DEFAULT_MAX_UNUSED_TIME_MILLIS);
080:            }
081:
082:            /**
083:             * @param size int
084:             * @param maxUnusedAge int
085:             */
086:            public LRUCache(int size, int maxUnusedAge) {
087:                super ();
088:                maxSize = size;
089:                maxUnusedTimeMillis = maxUnusedAge;
090:            }
091:
092:            /**
093:             * Synchronizes removal of ALL entries from the cache.
094:             */
095:            public synchronized void clear() {
096:                super .clear();
097:            }
098:
099:            /**
100:             * Get the object from the cache and reset the timestamp.
101:             * @param key the key, typically a String
102:             * @return the value to which the key is mapped in this cache;
103:             * null if the key is not mapped to any value in this cache.
104:             */
105:            public synchronized Object get(Object key) {
106:                ValueWrapper valueWrapper = (ValueWrapper) super .get(key);
107:                if (valueWrapper != null) {
108:                    // Update timestamp
109:                    valueWrapper.resetLastReferenceTime();
110:                    return valueWrapper.getValue();
111:                } else
112:                    return null;
113:            }
114:
115:            /**
116:             * Add a new value to the cache.
117:             * @param key the key, typically a String
118:             * @param value the value
119:             * @return the previous value of the specified key in this hashtable, or null if it did not have one.
120:             */
121:            public synchronized Object put(Object key, Object value) {
122:                ValueWrapper valueWrapper = new ValueWrapper(value);
123:                return super .put(key, valueWrapper);
124:            }
125:
126:            /**
127:             * Synchronizes removal of an entry from the cache.
128:             * @param key the key, typically a String
129:             * @return the previous value of the specified key in this hashtable,
130:             * or null if it did not have one.
131:             */
132:            public synchronized Object remove(Object key) {
133:                return super .remove(key);
134:            }
135:
136:            /**
137:             * Sweep the cache until it gets back under <code>maxSize</code>.
138:             */
139:            public void sweepCache() {
140:                long maxAge = maxUnusedTimeMillis;
141:                while (size() > maxSize) {
142:                    long cutOff = System.currentTimeMillis() - maxAge;
143:                    Object[] keys = getKeySetArray();
144:                    for (int i = 0; i < keys.length; i++) {
145:                        ValueWrapper valueWrapper = (ValueWrapper) super 
146:                                .get(keys[i]);
147:                        if (valueWrapper != null) {
148:                            if (valueWrapper.getLastReferenceTime() < cutOff) {
149:                                remove(keys[i]);
150:                            }
151:                        }
152:                    }
153:                    maxAge = maxAge * 3 / 4;
154:                }
155:            }
156:
157:            private synchronized Object[] getKeySetArray() {
158:                return keySet().toArray(new Object[size()]);
159:            }
160:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.