1 7 package org.jboss.test; 8 9 import java.io.FilePermission ; 10 import java.net.URL ; 11 import java.security.CodeSource ; 12 import java.security.Permission ; 13 import java.security.PermissionCollection ; 14 import java.security.Policy ; 15 16 import org.jboss.util.TimedCachePolicy; 17 18 25 public class TstTimedCache 26 { 27 static class Refreshable implements TimedCachePolicy.TimedEntry 28 { 29 int refreshes; 30 long expirationTime; 31 Object value; 32 Refreshable(long lifetime, Object value, int refreshes) 33 { 34 this.expirationTime = 1000 * lifetime; 35 this.value = value; 36 this.refreshes = refreshes; 37 } 38 public void init(long now) 39 { 40 expirationTime += now; 41 System.out.println(value+".init("+now+"), expirationTime="+expirationTime); 42 } 43 public boolean isCurrent(long now) 44 { 45 System.out.println(value+".isCurrent("+now+") = "+(expirationTime > now)); 46 return expirationTime > now; 47 } 48 public boolean refresh() 49 { 50 refreshes --; 51 System.out.println(value+".refresh() = "+(refreshes > 0)); 52 return refreshes > 0; 53 } 54 public void destroy() 55 { 56 System.out.println(value+".destroy()"); 57 } 58 public Object getValue() 59 { 60 return value; 61 } 62 } 63 64 67 public static void main(String args[]) 68 { 69 TimedCachePolicy cache = new TimedCachePolicy(20, false, 1); 70 cache.create(); 71 cache.start(); 72 cache.insert("1", new Refreshable(5, "value1", 4)); 73 cache.insert("2", new Refreshable(3, "value2", 10)); 74 cache.insert("3", "value3"); 75 long start = System.currentTimeMillis(); 76 while( cache.peek("2") != null ) 78 { 79 long now = System.currentTimeMillis(); 80 System.out.println("Elapsed: "+(now - start) / 1000); 81 System.out.println("get(1) -> "+cache.get("1")); 82 System.out.println("get(2) -> "+cache.get("2")); 83 System.out.println("get(3) -> "+cache.get("3")); 84 try 85 { 86 Thread.currentThread().sleep(3*1000); 87 } 88 catch(InterruptedException e) 89 { 90 } 91 } 92 long now = System.currentTimeMillis(); 93 System.out.println("End, elapsed: "+(now - start) / 1000); 94 System.out.println("get(1) -> "+cache.get("1")); 95 System.out.println("get(2) -> "+cache.get("2")); 96 System.out.println("get(3) -> "+cache.get("3")); 97 } 98 99 } 100 | Popular Tags |