KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > cache > SimpleExoCache


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.cache;
6
7 import java.io.Serializable JavaDoc;
8 import java.util.* ;
9 import java.lang.ref.SoftReference JavaDoc;
10 /**
11  * Created by The eXo Platform SARL
12  * Author : Tuan Nguyen
13  * tuan08@users.sourceforge.net
14  * Sat, Sep 13, 2003 @
15  * Time: 1:12:22 PM
16  */

17 public class SimpleExoCache extends LinkedHashMap implements ExoCache {
18     private static int DEFAULT_MAX_SIZE = 100 ;
19   
20   private String JavaDoc 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 JavaDoc name, int maxSize) {
34     maxSize_ = maxSize ;
35     name_ = name ;
36   }
37   
38   public String JavaDoc getName() { return name_ ; }
39   public void setName(String JavaDoc 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 JavaDoc get(Serializable JavaDoc name) {
47     SoftReference JavaDoc ref = (SoftReference JavaDoc) 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 JavaDoc remove(Serializable JavaDoc name) {
57     SoftReference JavaDoc ref = (SoftReference JavaDoc) super.remove(name) ;
58     if(ref != null) return ref.get() ;
59     return null ;
60   }
61
62   synchronized public void put(Serializable JavaDoc name, Object JavaDoc obj) {
63     SoftReference JavaDoc ref = new SoftReference JavaDoc(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