📄 cachemanager.java
字号:
} }); } catch (PrivilegedActionException ex) { Exception e = ex.getException(); if (e instanceof IOException) { throw (IOException)e; } ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } debugln("done>"); } public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof Entry) { // hash == anObject.hashCode()) { // byte v1[] = version; // byte v2[] = ((Entry)anObject).version; long v2 = ((Entry)anObject).version; return name.equals(((Entry)anObject).name) && (version == 0 || v2 == 0 || version == v2); // byte v1[] = version; // byte v2[] = ((Entry)anObject).version; // return name.equals( ((Entry)anObject).name ) && // (v1 == null || // v2 == null || // MessageDigest.isEqual(v1, v2)); } return false; } void remove() { if (filename == null) { return; } final java.io.File f = new File(filename); try { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { f.delete(); return null; } }); } catch (Exception ex) { System.out.println("CacheManager: couldn't delete a file " + filename); } } int hash; public int hashCode() { return hash; } public String toString() { if (data != null) { return "Entry:[" + name + '.' + version + '.' + data.length + ']'; } else { return "Entry:[" + name + '.' + version + '.' + null + ']'; } } } /* * ================================================== * public static void main(String a[]) { * CacheManager cache = new CacheManager(a[0], * Long.parseLong(a[1]), * Long.parseLong(a[2])); * byte val_a[] = {'a'}; * byte val_b[] = {'b'}; * byte val_c[] = {'c'}; * byte val_d[] = {'d'}; * byte val_e[] = {'e'}; * byte val_f[] = {'f'}; * byte val_g[] = {'g'}; * byte val[]; * long v1 = 1; * long v2 = 2; * long v3 = 3; * long v4 = 4; * long v5 = 5; * // if (a.length == 2) { * for (int i = 0; i < 5; i++) { * System.out.println("---------------------"); * cache.putData("a", v1, val_a); * cache.putData("b", v1, val_b); * cache.putData("c", v1, val_c); * cache.putData("d", v2, val_d); * cache.putData("e", v2, val_e); * cache.putData("f", v2, val_f); * cache.putData("g", v3, val_g); * // cache.close(); * // } else { * val = cache.getData("a", v1); * if (val == null) { * cache.putData("a", v1, val_a); * } * val = cache.getData("a", v1); * if (val != null) System.out.println("*************found 'a'"); * val = cache.getData("b", v1); * if (val != null) System.out.println("*************found 'b'"); * val = cache.getData("c", v1); * if (val != null) System.out.println("*************found 'c'"); * val = cache.getData("d", v2); * if (val != null) System.out.println("*************found 'd'"); * val = cache.getData("e", v2); * if (val != null) System.out.println("*************found 'e'"); * val = cache.getData("f", v2); * if (val != null) System.out.println("*************found 'f'"); * val = cache.getData("g", v3); * if (val != null) System.out.println("*************found 'g'"); * val = cache.getData("d", v2); * if (val != null) System.out.println("*************found 'd'"); * val = cache.getData("e", v2); * if (val != null) System.out.println("*************found 'e'"); * // } * } * System.exit(0); * } * ================================================== */ public CacheManager() throws IOException { Resource res = Resource.getResourceFor("aglets"); try { String server = AgletRuntime.getAgletRuntime().getServerAddress(); URL u = new URL(server); _pool_dir = res.getString("aglets.cache") + File.separator + u.getHost() + '@' + u.getPort(); } catch (MalformedURLException ex) { throw new IOException("Could not get host info."); } debugln("pool_dir = " + _pool_dir); final File dir = new File(_pool_dir); boolean exists = false; boolean isDir = false; try { Boolean[] bb = (Boolean[])AccessController .doPrivileged(new PrivilegedAction() { public Object run() { Boolean[] result = new Boolean[2]; result[0] = new Boolean(dir.exists()); result[1] = new Boolean(dir.isDirectory()); return result; } }); exists = bb[0].booleanValue(); isDir = bb[1].booleanValue(); } catch (Exception ex) { ex.printStackTrace(); } if (exists && (isDir == false)) { throw new IOException(_pool_dir + " is not a directory."); } if (exists == false) { debugln("Creating pool dir " + _pool_dir); if (FileUtils.ensureDirectory(_pool_dir + File.separator) == false) { throw new IOException("Failed to create new cache directory : " + _pool_dir); } } _cache_size = res.getInteger("aglets.cache.size", 1024 * 512); _cache = new LinkedList(_cache_size); _pool = new Hashtable(); try { readPool(); } catch (Exception ex) { ex.printStackTrace(); } } public CacheManager(String dir, long size) { _pool_dir = dir; _cache_size = size; _cache = new LinkedList(size); _pool = new Hashtable(); readPool(); } void debug(String a) { // System.out.print(a); } void debugln(String a) { // System.out.println(a); } private void decRefCount(Entry e) { synchronized (e) { debugln("refCount--[" + (e.refCount - 1) + "]: " + e.toString()); if (--e.refCount <= 0) { _cache.remove(e); _pool.remove(e); e.remove(); debugln("refCount--: removed " + e.toString()); } } } public static CacheManager getCacheManager() { if (_singleton == null) { try { _singleton = new CacheManager(); } catch (IOException ex) { ex.printStackTrace(); _singleton = null; } } return _singleton; } synchronized public byte[] getData(String name) { return getData(name, 0); } synchronized public byte[] getData(String name, long version) { Entry key = new Entry(name, version, null); Entry e = getFromCache(key); return ((e == null) ? null : e.data); } synchronized Entry getFromCache(Entry key) { Entry e = _cache.get(key); if (e == null) { e = (Entry)_pool.get(key); if (e == null) { return null; } } putIntoCache(e); Entry result = _cache.getFirst(); return result; } private void incRefCount(Entry e) { synchronized (e) { e.refCount++; debugln("refCount++[" + e.refCount + "]: " + e.toString()); } } synchronized public void putData(String name, long version, byte[] data) { Entry e = new Entry(name, version, data); putIntoCache(e); } synchronized public void putData(String name, long version, byte[] data, boolean refCount) { Entry e = new Entry(name, version, data); putIntoCache(e); if (refCount) { incRefCount(_cache.getFirst()); } } synchronized void putIntoCache(Entry e) { Entry purged[] = _cache.putAtFirst(e); if (purged != null) { for (int i = purged.length - 1; i >= 0; i--) { debugln("memcache purge: " + purged[i].name); if (!_pool.contains(purged[i])) { try { purged[i].save(); _pool.put(purged[i], purged[i]); } catch (IOException ex) { System.out .println("Failed to write a bytecode cache entry to disk: " + purged[i].toString()); } } } } } private void readPool() { debugln("<readPool: " + _pool_dir); final File f = new File(_pool_dir); String[] lists = null; try { lists = (String[])AccessController .doPrivileged(new PrivilegedAction() { public Object run() { return f.list(); } }); } catch (Exception ex) { ex.printStackTrace(); } if (lists != null) { debugln("<Reading Pool..." + lists.length); } for (int i = 0; lists != null && i < lists.length; i++) { try { int index = Integer.parseInt(lists[i].substring(2)); if (_pool_index < index) { _pool_index = index; } } catch (Exception ex) { // just skip the illegal file continue; } String filename = _pool_dir + File.separator + lists[i]; debugln(" Reading Cache file: " + filename); DataInputStream oi = null; try { oi = new DataInputStream(new FileInputStream(filename)); String name = oi.readUTF(); long version = oi.readLong(); int available = oi.readInt(); if (oi.available() != available) { System.out.println("Cache size mismatch.!"); try { // remove illegal cache.! final String fFileName = filename; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { new File(fFileName).delete(); return null; } }); } catch (Exception ex) { ex.printStackTrace(); } continue; } Entry e = new Entry(name, version, null); e.filename = filename; e.fillInData(); putIntoCache(e); } catch (IOException ex) { ex.printStackTrace(); } finally { try { oi.close(); } catch (Exception ex) {} ; } } debugln("done>"); } synchronized public void releaseData(String name, long version) { Entry key = new Entry(name, version, null); Entry e = getFromCache(key); decRefCount(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -