Source Code Cross Referenced for MultiCache.java in  » Ajax » zk » org » zkoss » util » 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 » Ajax » zk » org.zkoss.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* MultiCache.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Wed Aug 29 17:29:45     2007, Created by tomyeh
010:        }}IS_NOTE
011:
012:        Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        	This program is distributed under GPL Version 2.0 in the hope that
016:        	it will be useful, but WITHOUT ANY WARRANTY.
017:        }}IS_RIGHT
018:         */
019:        package org.zkoss.util;
020:
021:        import org.zkoss.lang.Objects;
022:
023:        /**
024:         * A {@link CacheMap}-based cache.
025:         * It creates multiple instances of {@link CacheMap}, called
026:         * the internal caches, and then distributes the access across them.
027:         * Thus, the performance is porportional to the number of internal caches.
028:         *
029:         * <p>Thread safe.
030:         *
031:         * @author tomyeh
032:         * @since 3.0.0
033:         */
034:        public class MultiCache implements  Cache, java.io.Serializable,
035:                Cloneable {
036:            private final CacheMap[] _caches;
037:            private int _maxsize;
038:
039:            /** Constructs a multi cache with 16 inital caches.
040:             */
041:            public MultiCache() {
042:                this (16);
043:            }
044:
045:            /** Constucts a multi cache with the specified number of internal caches,
046:             * the max size and the lifetime.
047:             *
048:             * @param nCache the postive number of the internal caches.
049:             * The large the number the fast the performance.
050:             */
051:            public MultiCache(int nCache, int maxSize, int lifetime) {
052:                this (nCache);
053:                setMaxSize(maxSize);
054:                setLifetime(lifetime);
055:            }
056:
057:            /** Constructs a multi cache with the specified number of internal caches.
058:             *
059:             * @param nCache the postive number of the internal caches.
060:             * The large the number the fast the performance.
061:             */
062:            public MultiCache(int nCache) {
063:                if (nCache <= 0)
064:                    throw new IllegalArgumentException("Positive only");
065:
066:                _caches = new CacheMap[nCache];
067:                for (int j = 0; j < nCache; ++j) {
068:                    _caches[j] = new CacheMap(8);
069:                    _maxsize += _caches[j].getMaxSize();
070:                }
071:            }
072:
073:            /* Used by {@link #clone} only. */
074:            private MultiCache(CacheMap[] clone, int maxsize) {
075:                _maxsize = maxsize;
076:                _caches = new CacheMap[clone.length];
077:                for (int j = 0; j < clone.length; ++j)
078:                    _caches[j] = (CacheMap) clone[j].clone();
079:            }
080:
081:            /** Constructs a multi cache with the specified number of internal caches
082:             * and the initialize size.
083:             *
084:             * @param nCache the postive number of the internal caches.
085:             * The large the number the fast the performance.
086:             * @param initSize the initialize size
087:             */
088:            public MultiCache(int nCache, int initSize) {
089:                if (nCache <= 0 || initSize <= 0)
090:                    throw new IllegalArgumentException("Positive only");
091:
092:                initSize = (initSize - 1) / nCache + 1;
093:                _caches = new CacheMap[nCache];
094:                for (int j = 0; j < nCache; ++j) {
095:                    _caches[j] = new CacheMap(initSize);
096:                    _maxsize += _caches[j].getMaxSize();
097:                }
098:            }
099:
100:            //Cache//
101:            public boolean containsKey(Object key) {
102:                final CacheMap map = getCache(key);
103:                synchronized (map) {
104:                    return map.containsKey(key);
105:                }
106:            }
107:
108:            public Object get(Object key) {
109:                final CacheMap map = getCache(key);
110:                synchronized (map) {
111:                    return map.get(key);
112:                }
113:            }
114:
115:            public Object put(Object key, Object value) {
116:                final CacheMap map = getCache(key);
117:                synchronized (map) {
118:                    return map.put(key, value);
119:                }
120:            }
121:
122:            public Object remove(Object key) {
123:                final CacheMap map = getCache(key);
124:                synchronized (map) {
125:                    return map.remove(key);
126:                }
127:            }
128:
129:            public void clear() {
130:                for (int j = 0; j < _caches.length; ++j) {
131:                    synchronized (_caches[j]) {
132:                        _caches[j].clear();
133:                    }
134:                }
135:            }
136:
137:            private CacheMap getCache(Object key) {
138:                int hc = Objects.hashCode(key);
139:                hc = (hc >>> 13) ^ hc;
140:                if (hc < 0)
141:                    hc = -hc;
142:                return _caches[hc % _caches.length];
143:            }
144:
145:            public int getLifetime() {
146:                return _caches[0].getLifetime();
147:            }
148:
149:            public void setLifetime(int lifetime) {
150:                synchronized (this ) {
151:                    for (int j = 0; j < _caches.length; ++j)
152:                        _caches[j].setLifetime(lifetime);
153:                }
154:            }
155:
156:            public int getMaxSize() {
157:                return _maxsize;
158:            }
159:
160:            public void setMaxSize(int maxsize) {
161:                _maxsize = maxsize;
162:
163:                int v = maxsize / _caches.length;
164:                if (v == 0)
165:                    v = maxsize > 0 ? 1 : maxsize < 0 ? -1 : 0;
166:
167:                synchronized (this ) {
168:                    for (int j = 0; j < _caches.length; ++j)
169:                        _caches[j].setMaxSize(v);
170:                }
171:            }
172:
173:            //Cloneable//
174:            public Object clone() {
175:                return new MultiCache(_caches, _maxsize);
176:            }
177:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.