KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > TimedCache


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * A timed LRU cache. Items remain valid until they expire.
33  * TimedCache can simplify database caching.
34  *
35  * <pre><code>
36  * TimedCache storyCache = new TimedCache(30, 60000);
37  *
38  * public Story getCurrentStory(String id)
39  * {
40  * Story story = (Story) storyCache.get(id);
41  *
42  * if (story == null) {
43  * story = DB.queryStoryDatabase(id);
44  * storyCache.put(id, story);
45  * }
46  *
47  * return story;
48  * }
49  * </code></pre>
50  */

51 public class TimedCache<K,V> {
52   private LruCache<K,TimedCache.Entry<V>> _cache;
53   private long _expireInterval;
54
55   /**
56    * Creates a new timed LRU cache.
57    *
58    * @param capacity the maximum size of the LRU cache
59    * @param expireInterval the time an entry remains valid
60    */

61   public TimedCache(int capacity, long expireInterval)
62   {
63     _cache = new LruCache<K,Entry<V>>(capacity);
64
65     _expireInterval = expireInterval;
66   }
67
68   /**
69    * Put a new item in the cache.
70    */

71   public V put(K key, V value)
72   {
73     Entry<V> oldValue = _cache.put(key, new Entry<V>(_expireInterval, value));
74
75     if (oldValue != null)
76       return oldValue.getValue();
77     else
78       return null;
79   }
80
81   /**
82    * Gets an item from the cache, returning null if expired.
83    */

84   public V get(K key)
85   {
86     Entry<V> entry = _cache.get(key);
87
88     if (entry == null)
89       return null;
90
91     if (entry.isValid())
92       return entry.getValue();
93     else {
94       _cache.remove(key);
95
96       return null;
97     }
98   }
99
100   /**
101    * Class representing a cached entry.
102    */

103   static class Entry<V> implements CacheListener {
104     private long _expireInterval;
105     private long _checkTime;
106     private V _value;
107
108     Entry(long expireInterval, V value)
109     {
110       _expireInterval = expireInterval;
111       _value = value;
112
113       _checkTime = Alarm.getCurrentTime();
114     }
115
116     boolean isValid()
117     {
118       return Alarm.getCurrentTime() < _checkTime + _expireInterval;
119     }
120
121     V getValue()
122     {
123       return _value;
124     }
125
126     public void removeEvent()
127     {
128       if (_value instanceof CacheListener)
129         ((CacheListener) _value).removeEvent();
130     }
131   }
132 }
133
134
Popular Tags