public static void main(String[] args) throws Exception { CacheTable t = new CacheTable().setDefaultKeepTime(1000).setMaxRow(100); for (int i = 0; i < 100; i++) { // if (i % 5 == 0) { // t.put(i, i, 10000); // } else { t.put(i, i); // } } Enumeration e = t.keys(); System.out.println(t.get(0)); System.out.println(t.get(10)); System.out.println(t.get(99)); Thread.sleep(2000); // t.clearExpiredItems(); System.out.println("----->" + t.size()); System.out.println(t.getFirstValue()); }
public V putFirst(K key, V value, long keepTime) { return _put(key, value, keepTime, MODE.FORCE_FIRST); }
public synchronized int getRemindTime(K key) { ENTRY entry = getEntry(key); if (entry != null) { if (entry.timeOfExpiration == 0) return Integer.MAX_VALUE; return (int) (entry.timeOfExpiration - System.currentTimeMillis()); } else { return 0; } }
private synchronized V _put(K key, V value, long keepTime, MODE m) { ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index]; e != null; e = e.next) { if (CompareUtil.equals(e.key, key)) { case FORCE_FIRST: if (header.link_next != e) { unchain(e); chain(header, header.link_next, e); unchain(e); chain(header.link_prev, header, e); case FIRST: while (count >= max) { removeLast(); case LAST: while (count >= max) { removeFirst(); rehash(); tab = table; index = hash(key) % tab.length; case FORCE_FIRST: case FIRST: chain(header, header.link_next, e); break;
public void clearExpiredItems() { try { ArrayList<K> delete = new ArrayList<K>(); Enumeration<CacheTable.ENTRY<K, V>> en = this.entries(); while (en.hasMoreElements()) { ENTRY e = en.nextElement(); if (e.isExpired()) { delete.add((K) e.getKey()); } } for (int i = 0; i < delete.size(); i++) { remove(delete.get(i)); } } catch (Throwable t) { t.printStackTrace(); } }
private ENTRY<K, V> getEntry(K key) { if (key == null) return null; ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index]; e != null; e = e.next) { if (CompareUtil.equals(e.key, key)) { if (e.isExpired()) { remove(e.getKey()); return null; } else { return e; } } } return null; }
public Block getReadBlock(String date, int blockNum) { Block b = readCache.get(new BKey(date, blockNum)); if (b != null) return b; File f = getFile(date, blockNum); if (f.exists() == false) return null; try { byte[] gz = FileUtil.readAll(f); gz = CompressUtil.unZip(gz); Block bk = new Block(date, gz, 0, gz.length, GZipCtr.BLOCK_MAX_SIZE); bk.blockNum = blockNum; readCache.put(new BKey(date, blockNum), bk, conf._compress_read_cache_expired_ms); return bk; } catch (Throwable e) { e.printStackTrace(); } return null; } public void shutdown() {
public synchronized V remove(Object key) { if (key == null) return null; ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) { if (CompareUtil.equals(e.key, key)) { if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; // unchain(e); return oldValue; } } return null; }
public String toFormatString() { StringBuffer buf = new StringBuffer(); Enumeration it = entries(); buf.append("{\n"); while (it.hasMoreElements()) { ENTRY e = (ENTRY) it.nextElement(); buf.append("\t").append(e.getKey() + "=" + e.getValue()).append("\n"); } buf.append("}"); return buf.toString(); }
protected void rehash() { int oldCapacity = table.length; ENTRY oldMap[] = table; int newCapacity = oldCapacity * 2 + 1; ENTRY newMap[] = new ENTRY[newCapacity]; threshold = (int) (newCapacity * loadFactor); table = newMap; for (int i = oldCapacity; i-- > 0;) { for (ENTRY<K, V> old = oldMap[i]; old != null;) { ENTRY<K, V> e = old; old = old.next; K key = e.key; int index = hash(key) % newCapacity; e.next = newMap[index]; newMap[index] = e; } } }
private synchronized V _put(K key, V value, long keepTime, MODE m) { ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index]; e != null; e = e.next) { if (CompareUtil.equals(e.key, key)) { case FORCE_FIRST: if (header.link_next != e) { unchain(e); chain(header, header.link_next, e); unchain(e); chain(header.link_prev, header, e); case FIRST: while (count >= max) { removeLast(); case LAST: while (count >= max) { removeFirst(); rehash(); tab = table; index = hash(key) % tab.length; case FORCE_FIRST: case FIRST: chain(header, header.link_next, e); break;
public void clearExpiredItems() { try { ArrayList<K> delete = new ArrayList<K>(); Enumeration<CacheTable.ENTRY<K, V>> en = this.entries(); while (en.hasMoreElements()) { ENTRY e = en.nextElement(); if (e.isExpired()) { delete.add((K) e.getKey()); } } for (int i = 0; i < delete.size(); i++) { remove(delete.get(i)); } } catch (Throwable t) { t.printStackTrace(); } }
private ENTRY<K, V> getEntry(K key) { if (key == null) return null; ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index]; e != null; e = e.next) { if (CompareUtil.equals(e.key, key)) { if (e.isExpired()) { remove(e.getKey()); return null; } else { return e; } } } return null; }
private void addCounter0(AddCounterParam param) { ObjectType objectType = param.objectType; Counter counter = param.counter; Logger.println("[counter+]Trying to add new counter : " + objectType.getFamily().getName() + " - " + counter.getName()); //TODO login check if (counter.semanticEquals(prevAddedCounter.get(counter.getName()))) { Logger.println("[counter+] ignored by equals"); return; } Family family = objectType.getFamily(); family.addCounter(counter); boolean success = counterManager.safelyAddFamily(family); if (success) { prevAddedCounter.put(counter.getName(), counter); changeNotifyQueue.put(1); } } }
public synchronized V remove(Object key) { if (key == null) return null; ENTRY<K, V> tab[] = table; int index = hash(key) % tab.length; for (ENTRY<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) { if (CompareUtil.equals(e.key, key)) { if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; // unchain(e); return oldValue; } } return null; }
public String toFormatString() { StringBuffer buf = new StringBuffer(); Enumeration it = entries(); buf.append("{\n"); while (it.hasMoreElements()) { ENTRY e = (ENTRY) it.nextElement(); buf.append("\t").append(e.getKey() + "=" + e.getValue()).append("\n"); } buf.append("}"); return buf.toString(); }