1 22 package com.geinuke.util.collection; 23 24 25 import java.util.*; 26 27 28 public class SimpleCache extends Hashtable implements Runnable { 29 protected Hashtable timers=null; 30 protected ArrayList parents=null; 31 protected String name=null; 32 public SimpleCache(String name){ 33 super(); 34 this.name=name; 35 this.timers=new Hashtable(); 36 parents=new ArrayList(); 37 Thread t=new Thread (this); 38 t.start(); 39 } 40 public void addParent(SimpleCache sc){ 41 this.parents.add(sc); 42 } 43 44 public void run(){ 45 while(true){ 46 47 try{ 48 Thread.sleep(100000); 49 Enumeration e=super.keys(); 50 Object kk=null; 51 Long t=null; 52 while(e.hasMoreElements()){ 53 kk=e.nextElement(); 54 t=(Long )this.timers.get(kk); 55 if(t.longValue()< (System.currentTimeMillis() / 1000 ) ){ 56 this.remove(kk); 57 } 58 } 59 }catch(Exception e){} 60 } 61 } 62 63 public synchronized void clear(){ 64 SimpleCache sc=null; 65 System.out.println("SimpleCache.clear(), clearing "+name); 66 for(int i=0;i<this.parents.size();i++){ 67 sc=(SimpleCache)this.parents.get(i); 68 sc.clear(); 69 } 70 super.clear(); 71 this.timers.clear(); 72 } 73 74 public synchronized void put(Object key,Object value,long durata){ 75 Long lo=new Long (durata); 76 this.put(key,value); 77 long start=System.currentTimeMillis()/1000; 78 long secs=0; 79 secs=start+ durata; 80 this.timers.put(key,new Long (secs)); 81 82 } 83 84 public synchronized Object remove(Object k){ 85 this.timers.remove(k); 86 return super.remove(k); 87 } 88 89 } 90 | Popular Tags |