1 package org.shiftone.cache.policy.single; 2 3 4 5 import org.shiftone.cache.util.reaper.ReapableCache; 6 7 8 15 class SingleCache implements ReapableCache 16 { 17 18 private long timeoutMilliSeconds = 0; 19 private long expireTime = 0; 20 private Object userKey = null; 21 private Object cacheObject = null; 22 23 29 public SingleCache(long timeoutMilliSeconds) 30 { 31 this.timeoutMilliSeconds = timeoutMilliSeconds; 32 } 33 34 35 38 public synchronized void addObject(Object userKey, Object cacheObject) 39 { 40 41 this.userKey = userKey; 42 this.cacheObject = cacheObject; 43 expireTime = System.currentTimeMillis() + timeoutMilliSeconds; 44 } 45 46 47 50 public synchronized Object getObject(Object key) 51 { 52 53 Object value = null; 54 55 if ((this.userKey != null) && (key != null) && (key.equals(this.userKey))) 56 { 57 value = this.cacheObject; 58 } 59 60 return value; 61 } 62 63 64 67 public int size() 68 { 69 70 return (userKey == null) 71 ? 0 72 : 1; 73 } 74 75 76 79 public synchronized void remove(Object key) 80 { 81 82 if ((this.userKey != null) && (key != null) && (key.equals(this.userKey))) 83 { 84 userKey = null; 85 cacheObject = null; 86 } 87 } 88 89 90 93 public synchronized void clear() 94 { 95 userKey = null; 96 cacheObject = null; 97 } 98 99 100 103 public synchronized void removeExpiredElements() 104 { 105 106 if (System.currentTimeMillis() >= expireTime) 108 { 109 remove(this.userKey); 110 } 111 } 112 113 114 public final String toString() 115 { 116 return "SingleCache" + size(); 117 } 118 } 119 | Popular Tags |