View Javadoc

1   /*$Id: Cache.java,v 1.3 2005/07/27 15:35:08 clq2 Exp $
2    * Created on 12-Apr-2005
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.jes.util;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import org.astrogrid.workflow.beans.v1.execution.JobURN;
17  
18  import org.apache.commons.collections.map.LRUMap;
19  
20  import java.lang.ref.Reference;
21  import java.lang.ref.SoftReference;
22  import java.util.Map;
23  /***
24   * A caching map - combination of a LeastRecentlyUsed map, with soft references.
25   * As the implementation of soft references suggests that the JVM frees the least recently used reference too, then
26   * we should get a nicely behaved cache - will have the fixed size until memory starts to get tight, where the least recently used items get freed.
27   * @author Noel Winstanley nw@jb.man.ac.uk 12-Apr-2005
28   *
29   */
30  
31  public class Cache {
32      /***
33       * Commons Logger for this class
34       */
35      private static final Log logger = LogFactory.getLog(Cache.class);
36  
37      public Cache(int size) {
38          m = new LRUMap(size);
39      }
40  
41      /*** use a combination of a LRU map and a reference map. */
42     protected final Map m;
43      public void stuff(JobURN key, Object value) {
44          Reference ref = new SoftReference(value);
45          m.put(key.getContent(),ref);
46      }
47      
48      public Object check(JobURN urn) {
49          Reference ref = (Reference)m.get(urn.getContent());
50         if (logger.isDebugEnabled()) {
51             logger.debug("Cache " + (ref != null && ref.get() != null ? " HIT" : "MISS"));
52         }
53          return ref == null ? null : ref.get();
54      }
55      
56      public void delete(JobURN key) {
57          m.remove(key.getContent());
58      }   
59      
60      public void clear() {
61          m.clear();
62      }
63  }
64  
65  /* 
66  $Log: Cache.java,v $
67  Revision 1.3  2005/07/27 15:35:08  clq2
68  jes_nww_review_unit_tests
69  
70  Revision 1.2.22.1  2005/07/19 15:38:06  nw
71  fixed unit tests -100% pass rate now.
72  
73  Revision 1.2  2005/04/25 12:13:54  clq2
74  jes-nww-776-again
75  
76  Revision 1.1.2.1  2005/04/12 17:07:35  nw
77  optimization class
78   
79  */