KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > thread > TimedItem


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.server.thread;
30
31 import com.caucho.util.Alarm;
32 import com.caucho.util.CacheListener;
33
34 /**
35  * A single item timed cache. The item will remain valid until it expires.
36  * TimedItem can simplify database caching.
37  *
38  * <pre><code>
39  * TimedItem currentStories = new TimedItem(60000);
40  *
41  * public ArrayList getCurrentStories()
42  * {
43  * ArrayList storyList = (ArrayList) currentStories.get();
44  *
45  * if (storyList == null) {
46  * storyList = DB.queryStoryDatabase();
47  * currentStories.put(storyList);
48  * }
49  *
50  * return storyList;
51  * }
52  * </code></pre>
53  */

54 public class TimedItem {
55   private long expireInterval;
56
57   private long createTime;
58   private Object JavaDoc value;
59
60   /**
61    * Create a new timed item with a specified update time
62    *
63    * @param expireInterval the time in milliseconds the item remains valid.
64    */

65   public TimedItem(long expireInterval)
66   {
67     this.expireInterval = expireInterval;
68   }
69
70   /**
71    * Returns the expire time for this TimedItem.
72    */

73   public long getExpireInterval()
74   {
75     return expireInterval;
76   }
77
78   /**
79    * Sets the expire time for this timedItem.
80    */

81   public void setExpireInterval(long expireInterval)
82   {
83     this.expireInterval = expireInterval;
84   }
85
86   /**
87    * Sets the value.
88    */

89   public void put(Object JavaDoc value)
90   {
91     createTime = Alarm.getCurrentTime();
92     this.value = value;
93   }
94
95   /**
96    * Gets the cached value, returning null if expires.
97    */

98   public Object JavaDoc get()
99   {
100     if (Alarm.getCurrentTime() < createTime + expireInterval)
101       return value;
102     else {
103       Object JavaDoc v = value;
104       value = null;
105       
106       if (v instanceof CacheListener)
107         ((CacheListener) v).removeEvent();
108       
109       return null;
110     }
111   }
112 }
113
114
115
Popular Tags