KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > cache > ExpiringLRUCacheImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util.cache;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25 /**
26  * An LRU cache where entries expire after a given timeout period.
27  */

28 public class ExpiringLRUCacheImpl extends LRUCacheImpl {
29     
30     private static Log log = LogFactory.getLog(ExpiringLRUCacheImpl.class);
31     
32     private long timeout = 0;
33     
34     
35     protected ExpiringLRUCacheImpl(String JavaDoc id) {
36         
37         super(id);
38         this.timeout = 60 * 60 * 1000;
39     }
40     
41     
42     protected ExpiringLRUCacheImpl(String JavaDoc id, int maxsize, long timeout) {
43         
44         super(id, maxsize);
45         
46         // timeout is specified in seconds; only positive values allowed
47
if(timeout > 0) {
48             this.timeout = timeout * 1000;
49         }
50     }
51     
52     
53     /**
54      * Store an entry in the cache.
55      *
56      * We wrap the cached object in our ExpiringCacheEntry object so that we
57      * can track when the entry has expired.
58      */

59     public synchronized void put(String JavaDoc key, Object JavaDoc value) {
60         
61         ExpiringCacheEntry entry = new ExpiringCacheEntry(value, this.timeout);
62         super.put(key, entry);
63     }
64     
65     
66     /**
67      * Retrieve an entry from the cache.
68      *
69      * This LRU cache supports timeouts, so if the cached object has expired
70      * then we return null, just as if the entry wasn't found.
71      */

72     public Object JavaDoc get(String JavaDoc key) {
73         
74         Object JavaDoc value = null;
75         ExpiringCacheEntry entry = null;
76         
77         synchronized(this) {
78             entry = (ExpiringCacheEntry) super.get(key);
79         }
80         
81         if (entry != null) {
82             
83             value = entry.getValue();
84             
85             // if the value is null then that means this entry expired
86
if (value == null) {
87                 log.debug("EXPIRED ["+key+"]");
88                 hits--;
89                 super.remove(key);
90             }
91         }
92         
93         return value;
94     }
95     
96 }
97
Popular Tags