KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > collection > SimpleCache


1  /*
2  -- GeiNuke --
3 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
4  http://www.hostingjava.it/-geinuke/
5
6 This file is part of GeiNuke.
7
8     GeiNuke is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     GeiNuke is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with GeiNuke; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */

22 package com.geinuke.util.collection;
23
24
25 import java.util.*;
26
27
28 public class SimpleCache extends Hashtable implements Runnable JavaDoc{
29     protected Hashtable timers=null;
30     protected ArrayList parents=null;
31     protected String JavaDoc name=null;
32     public SimpleCache(String JavaDoc name){
33         super();
34         this.name=name;
35         this.timers=new Hashtable();
36         parents=new ArrayList();
37         Thread JavaDoc t=new Thread JavaDoc(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 JavaDoc kk=null;
51                 Long JavaDoc t=null;
52                 while(e.hasMoreElements()){
53                     kk=e.nextElement();
54                     t=(Long JavaDoc)this.timers.get(kk);
55                     if(t.longValue()< (System.currentTimeMillis() / 1000 ) ){
56                         this.remove(kk);
57                     }
58                 }
59             }catch(Exception JavaDoc 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 JavaDoc key,Object JavaDoc value,long durata){
75         Long JavaDoc lo=new Long JavaDoc(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 JavaDoc(secs));
81             
82     }
83     
84     public synchronized Object JavaDoc remove(Object JavaDoc k){
85         this.timers.remove(k);
86         return super.remove(k);
87     }
88     
89 }
90
Popular Tags