1 5 package org.exoplatform.services.cache; 6 7 import java.io.Serializable ; 8 import java.util.* ; 9 import java.lang.ref.SoftReference ; 10 17 public class SimpleExoCache extends LinkedHashMap implements ExoCache { 18 private static int DEFAULT_MAX_SIZE = 100 ; 19 20 private String name_ ; 21 private int maxSize_ ; 22 private int cacheHit_ ; 23 private int cacheMiss_ ; 24 25 public SimpleExoCache() { 26 maxSize_ = DEFAULT_MAX_SIZE ; 27 } 28 29 public SimpleExoCache(int maxSize) { 30 maxSize_ = maxSize ; 31 } 32 33 public SimpleExoCache(String name, int maxSize) { 34 maxSize_ = maxSize ; 35 name_ = name ; 36 } 37 38 public String getName() { return name_ ; } 39 public void setName(String s) { name_ = s ; } 40 41 public int getCacheSize() { return size() ; } 42 43 public int getMaxSize() { return maxSize_ ; } 44 public void setMaxSize(int max) { maxSize_ = max ; } 45 46 synchronized public Object get(Serializable name) { 47 SoftReference ref = (SoftReference ) super.get(name) ; 48 if(ref != null) { 49 cacheHit_++ ; 50 return ref.get() ; 51 } 52 cacheMiss_++ ; 53 return null ; 54 } 55 56 synchronized public Object remove(Serializable name) { 57 SoftReference ref = (SoftReference ) super.remove(name) ; 58 if(ref != null) return ref.get() ; 59 return null ; 60 } 61 62 synchronized public void put(Serializable name, Object obj) { 63 SoftReference ref = new SoftReference (obj) ; 64 super.put(name, ref) ; 65 } 66 67 public int getCacheHit() { return cacheHit_ ;} 68 69 public int getCacheMiss() { return cacheMiss_ ; } 70 71 protected boolean removeEldestEntry(Map.Entry eldest) { 72 return size() > maxSize_ ; 73 } 74 } | Popular Tags |