| 1 package com.protomatter.util; 2 3 52 53 57 public class ExpiringSoftReferenceCache 58 extends SoftReferenceCache 59 { 60 private long timeout = 0; 61 62 65 public ExpiringSoftReferenceCache(long timeout) 66 { 67 super(); 68 this.timeout = timeout; 69 } 70 71 75 public void put(Object key, Object val) 76 { 77 super.put(key, new CacheValue(val)); 78 } 79 80 83 public Object get(Object key) 84 { 85 CacheValue value = (CacheValue)super.get(key); 86 if (value != null) 87 { 88 long now = System.currentTimeMillis(); 89 if ((value.getCreateTime() + this.timeout) >= now) 90 { 91 return value.getValue(); 92 } 93 else 94 { 95 clear(key); 96 } 97 } 98 return null; 99 } 100 101 private class CacheValue 102 { 103 private long createTime = 0; 104 private Object value = null; 105 106 public CacheValue(Object value) 107 { 108 this.value = value; 109 this.createTime = System.currentTimeMillis(); 110 } 111 112 public Object getValue() 113 { 114 return this.value; 115 } 116 117 public long getCreateTime() 118 { 119 return this.createTime; 120 } 121 } 122 } 123 | Popular Tags |