1 32 33 package com.knowgate.dataobjs; 34 35 import java.lang.System ; 36 import java.util.TreeMap ; 37 import java.util.Iterator ; 38 import java.lang.Thread ; 39 40 final class DBSubsetCacheReaper extends Thread { 41 42 private DBSubsetCache oDBCache; 43 44 DBSubsetCacheReaper(DBSubsetCache objDBCache) { 45 oDBCache = objDBCache; 46 } 47 48 private void reapEntries() { 49 int iLRU = oDBCache.iTopIndex-oDBCache.iUsed; 51 int iDiscardCount = (oDBCache.capacity()*2)/10; 53 int iComma; String sEntry; String sTable; String sKey; 58 for (int i=iLRU; i<iLRU+iDiscardCount; i++) { 60 sEntry = oDBCache.getKey(iLRU); 61 if (null!=sEntry) { 62 iComma = sEntry.indexOf(','); 63 sTable = sEntry.substring(0, iComma); 64 sKey = sEntry.substring(iComma+1); 65 oDBCache.expire(sKey); 66 } } 69 oDBCache.iUsed-=iDiscardCount; } 72 public void run() { 73 reapEntries(); 74 } 75 } 77 79 84 85 public final class DBSubsetCache { 86 87 91 public DBSubsetCache() { 92 iUsed = 0; 93 iTopIndex = 0; 94 iCacheCapacity = 100; 95 LRUList = new String [iCacheCapacity]; 96 for (int s=0; s<iCacheCapacity; s++) LRUList[s] = null; 97 oCache = new TreeMap (); 98 } 99 100 104 105 public DBSubsetCache(int iCapacity) { 106 iUsed = 0; 107 iTopIndex = 0; 108 iCacheCapacity = iCapacity; 109 LRUList = new String [iCacheCapacity]; 110 for (int s=0; s<iCacheCapacity; s++) LRUList[s] = null; 111 oCache = new TreeMap (); 112 } 113 114 116 119 120 public int capacity() { 121 return iCacheCapacity; 122 } 123 124 126 132 public synchronized void put(String sTableName, String sKey, DBSubset oDBSS) { 133 int iIndex = iTopIndex%iCacheCapacity; iTopIndex++; iUsed++; 134 DBCacheEntry oEntry = new DBCacheEntry(oDBSS, sTableName, iIndex); 135 DBSubsetCacheReaper oReaper; 136 137 if (null==sTableName) sTableName="none"; 138 139 oCache.put(sKey, oEntry); 140 LRUList[iIndex] = sTableName + "," + sKey; 141 142 if (iUsed>=iCacheCapacity-1) { 143 oReaper = new DBSubsetCacheReaper(this); 144 oReaper.run(); 145 } } 148 150 155 156 public synchronized boolean expire(String sKey) { 157 Object objEntry = oCache.get(sKey); 158 159 if (null!=objEntry) { 160 setKey(null, ((DBCacheEntry) objEntry).iIndex); 161 oCache.remove(sKey); 162 } 163 return null!=objEntry ? true : false; 164 } 166 168 174 175 public void replace(String sTableName, String sKey, DBSubset oDBSS) { 176 177 expire(sKey); 178 put(sTableName, sKey, oDBSS); 179 } 181 183 186 187 public synchronized void clear() { 188 oCache.clear(); 189 for (int s=0; s<iCacheCapacity; s++) LRUList[s] = null; 190 iTopIndex=0; 191 iUsed=0; 192 } 194 196 200 201 public synchronized void clear(String sTable) { 202 Iterator oIter = oCache.keySet().iterator(); 203 String sKey; 204 DBCacheEntry oEntry; 205 206 if (sTable==null) sTable = "none"; 207 208 while (oIter.hasNext()) { 209 sKey = (String ) oIter.next(); 210 oEntry = (DBCacheEntry) oCache.get(sKey); 211 if (sTable.equals(oEntry.sTable)) 212 expire(sKey); 213 } } 216 218 223 224 public DBSubset get(String sKey) { 225 Object oObj = oCache.get(sKey); 226 DBCacheEntry oEntry; 227 228 if (oObj!=null) { 229 oEntry = (DBCacheEntry) oObj; 230 oEntry.iTimesUsed++; 231 oEntry.lastUsed = System.currentTimeMillis(); 232 return oEntry.oDBSubset; 233 } 234 else 235 return null; 236 } 238 240 245 246 public DBCacheEntry getEntry(String sKey) { 247 Object oObj = oCache.get(sKey); 248 DBCacheEntry oEntry; 249 250 if (oObj!=null) { 251 oEntry = (DBCacheEntry) oObj; 252 oEntry.iTimesUsed++; 253 oEntry.lastUsed = System.currentTimeMillis(); 254 return oEntry; 255 } 256 else 257 return null; 258 } 260 262 public String getKey(int iEntryIndex) { 263 return LRUList[iEntryIndex % iCacheCapacity]; 264 } 265 266 268 public void setKey(String sKey, int iEntryIndex) { 269 LRUList[iEntryIndex % iCacheCapacity] = sKey; 270 } 271 272 274 public class DBCacheEntry { 275 public long lastModified; 276 public long lastUsed; 277 public int iTimesUsed; 278 public int iIndex; 279 public String sTable; 280 public DBSubset oDBSubset; 281 282 DBCacheEntry (DBSubset oDBSS, String sTbl, int iIdx) { 283 sTable = sTbl; 284 iIndex = iIdx; 285 iTimesUsed = 0; 286 lastUsed = lastModified = System.currentTimeMillis(); 287 oDBSubset = oDBSS; 288 } 289 } 291 private int iCacheCapacity; private String LRUList[]; private TreeMap oCache; 295 public int iTopIndex; public int iUsed; }
| Popular Tags
|