View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.store;
18  
19  import net.sf.ehcache.CacheException;
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.Element;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import java.io.Serializable;
26  import java.util.Iterator;
27  import java.util.LinkedHashMap;
28  import java.util.Set;
29  
30  /**
31   * First-In-First-Out (FIFO) implementation of MemoryStore.
32   *
33   * @author <a href="mailto:ssuravarapu@users.sourceforge.net">Surya Suravarapu</a>
34   * @version $Id: FifoMemoryStore.java 512 2007-07-10 09:18:45Z gregluck $
35   */
36  public class FifoMemoryStore extends MemoryStore {
37      private static final Log LOG = LogFactory.getLog(FifoMemoryStore.class.getName());
38  
39      /**
40       * Constructor for the FifoMemoryStore object.
41       * <p/>
42       * First tries to use {@link java.util.LinkedHashMap}. If not found uses
43       * Jakarta Commons collections.
44       */
45      public FifoMemoryStore(Ehcache cache, Store diskStore) {
46          super(cache, diskStore);
47          map = new LinkedHashMap();
48      }
49  
50      /**
51       * Allow specialised actions over adding the element to the map
52       *
53       * @param element
54       */
55      protected final void doPut(Element element) throws CacheException {
56          if (isFull()) {
57              removeFirstElement();
58          }
59      }
60  
61  
62      /**
63       * Returns the first eligible element that can be taken out of the cache
64       * based on the FIFO policy
65       */
66      Element getFirstElement() {
67          if (map.size() == 0) {
68              return null;
69          }
70  
71          Element element = null;
72          Serializable key;
73  
74          Set keySet = map.keySet();
75          Iterator itr = keySet.iterator();
76          // The first element is the candidate to remove
77          if (itr.hasNext()) {
78              key = (Serializable) itr.next();
79              element = (Element) map.get(key);
80          }
81  
82          return element;
83      }
84  
85      /**
86       * Remove the first element that is eligible to removed from the store
87       * based on the FIFO policy
88       */
89      private void removeFirstElement() throws CacheException {
90          Element element = getFirstElement();
91  
92          if (element.isExpired()) {
93              remove(element.getObjectKey());
94              notifyExpiry(element);
95              return;
96          }
97          remove(element.getObjectKey());
98          evict(element);
99      }
100 }