001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.entity.cache;
008:
009: import java.util.Comparator;
010: import java.util.HashMap;
011: import java.util.Map;
012: import java.util.UUID;
013: import java.util.Collection;
014: import java.util.Collections;
015: import java.util.concurrent.Executors;
016: import java.util.concurrent.ScheduledExecutorService;
017: import java.util.concurrent.TimeUnit;
018:
019: /**
020: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
021: */
022: public class CacheConfig {
023:
024: private ScheduledExecutorService cleaner = Executors
025: .newSingleThreadScheduledExecutor();
026:
027: /**
028: * 支�的 Evicition 算法
029: */
030: public static enum Algorithm {
031: LRU, LFU, FIFO
032: }
033:
034: private String name;
035:
036: /**
037: * 最大生å˜æ—¶é—´ <=0 è¡¨ç¤ºæ— é™?
038: */
039: private long ttl = 0;
040:
041: /**
042: * å¯¹è±¡çš„æœ€å¤§çš„ä¼‘çœ æ—¶é—´ï¼Œ<=0è¡¨ç¤ºæ— é™?
043: */
044: private long maxIdleTime = 0;
045:
046: /**
047: * 能使用的最大内å˜æ•°, 默认 10M
048: */
049: private long maxMemorySize = 10 * 1024 * 1024;
050:
051: /**
052: * 最大缓å˜çš„对象数目,默认 1000
053: */
054: private int maxSize = 1000;
055:
056: /**
057: * 采用的Eviction算法
058: */
059: private Algorithm algorithm = Algorithm.LRU;
060:
061: private Map<String, Cache> caches = new HashMap<String, Cache>();
062:
063: public CacheConfig() {
064: this (UUID.randomUUID().toString(), Algorithm.LRU,
065: Integer.MAX_VALUE, Integer.MAX_VALUE,
066: Integer.MAX_VALUE, Integer.MAX_VALUE);
067: }
068:
069: public CacheConfig(String name) {
070: this (name, Algorithm.LRU, Integer.MAX_VALUE, Integer.MAX_VALUE,
071: Integer.MAX_VALUE, Integer.MAX_VALUE);
072: }
073:
074: public CacheConfig(String name, Algorithm algorithm, int maxSize,
075: long maxMemorySize, long maxIdleTime, long ttl) {
076: this .name = name;
077: this .algorithm = algorithm;
078: this .maxSize = maxSize;
079: this .maxMemorySize = maxMemorySize;
080: this .maxIdleTime = maxIdleTime;
081: this .ttl = ttl;
082: }
083:
084: public String getName() {
085: return name;
086: }
087:
088: public Algorithm getAlgorithm() {
089: return algorithm;
090: }
091:
092: public void setAlgorithm(Algorithm algorithm) {
093: this .algorithm = algorithm;
094: }
095:
096: public Comparator<CachedObject> getAlgorithmComparator() {
097: switch (getAlgorithm()) {
098: case LFU:
099: return new LFUComparator();
100: case LRU:
101: return new LRUComparator();
102: default:
103: return new LRUComparator();
104: }
105: }
106:
107: public void setMaxIdleTime(long maxIdleTime) {
108: this .maxIdleTime = maxIdleTime;
109: }
110:
111: public long getMaxIdleTime() {
112: return maxIdleTime;
113: }
114:
115: public void setMaxMemorySize(long maxMemorySize) {
116: this .maxMemorySize = maxMemorySize;
117: }
118:
119: public long getMaxMemorySize() {
120: return maxMemorySize;
121: }
122:
123: public void setMaxSize(int maxSize) {
124: this .maxSize = maxSize;
125: }
126:
127: public int getMaxSize() {
128: return maxSize;
129: }
130:
131: public void setTTL(long ttl) {
132: this .ttl = ttl;
133: }
134:
135: public long getTTL() {
136: return ttl;
137: }
138:
139: /**
140: * æ ¹æ?® Config 创建 Cache,如果已ç»?创建,则返回创建的
141: *
142: * @param cacheName cache name
143: */
144: public synchronized Cache buildCache(String cacheName) {
145: if (caches.isEmpty()) {
146: cleaner.scheduleWithFixedDelay(new Runnable() {
147: public void run() {
148: for (Cache cache : caches.values()) {
149: cache.clean();
150: }
151: }
152: }, getMaxIdleTime() / 10, getMaxIdleTime() / 2,
153: TimeUnit.MILLISECONDS);
154: }
155: if (!caches.containsKey(cacheName)) {
156: Cache cache = new SimpleCache(cacheName, this );
157: caches.put(cacheName, cache);
158: }
159: return caches.get(cacheName);
160: }
161:
162: public Collection<Cache> getAllCaches() {
163: return Collections.unmodifiableCollection(caches.values());
164: }
165:
166: public void clear() {
167: for (Cache cache : caches.values()) {
168: cache.clear();
169: }
170: }
171:
172: public void close() {
173: cleaner.shutdown();
174: clear();
175: caches.clear();
176: }
177:
178: public String toString() {
179: return "CacheConfig@" + getName() + "@" + hashCode();
180: }
181:
182: public static void main(String[] args) {
183: CacheConfig cacheConfig = new CacheConfig(UUID.randomUUID()
184: .toString(), CacheConfig.Algorithm.LRU, 2, 10 * 1024,
185: 60 * 1000, 0);
186: Cache cache = cacheConfig.buildCache("DEFAULT");
187: cache.put("a", "1");
188: cache.put("b", "2");
189: System.out.println(cache.get("a"));
190: System.out.println(cache.get("b"));
191: cache.remove("b");
192: System.out.println(cache.get("b"));
193: System.out.println(cache.getStatus().getSize());
194: System.out.println(cache.getStatus().getPuts());
195: System.out.println(cache.getStatus().getRemoves());
196: System.out.println(cache.getStatus().getSize());
197:
198: System.out.println("shutdown...");
199: cacheConfig.close();
200: }
201: }
|