001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.thread;
030:
031: import com.caucho.util.Alarm;
032: import com.caucho.util.CacheListener;
033:
034: /**
035: * A single item timed cache. The item will remain valid until it expires.
036: * TimedItem can simplify database caching.
037: *
038: * <pre><code>
039: * TimedItem currentStories = new TimedItem(60000);
040: *
041: * public ArrayList getCurrentStories()
042: * {
043: * ArrayList storyList = (ArrayList) currentStories.get();
044: *
045: * if (storyList == null) {
046: * storyList = DB.queryStoryDatabase();
047: * currentStories.put(storyList);
048: * }
049: *
050: * return storyList;
051: * }
052: * </code></pre>
053: */
054: public class TimedItem {
055: private long expireInterval;
056:
057: private long createTime;
058: private Object value;
059:
060: /**
061: * Create a new timed item with a specified update time
062: *
063: * @param expireInterval the time in milliseconds the item remains valid.
064: */
065: public TimedItem(long expireInterval) {
066: this .expireInterval = expireInterval;
067: }
068:
069: /**
070: * Returns the expire time for this TimedItem.
071: */
072: public long getExpireInterval() {
073: return expireInterval;
074: }
075:
076: /**
077: * Sets the expire time for this timedItem.
078: */
079: public void setExpireInterval(long expireInterval) {
080: this .expireInterval = expireInterval;
081: }
082:
083: /**
084: * Sets the value.
085: */
086: public void put(Object value) {
087: createTime = Alarm.getCurrentTime();
088: this .value = value;
089: }
090:
091: /**
092: * Gets the cached value, returning null if expires.
093: */
094: public Object get() {
095: if (Alarm.getCurrentTime() < createTime + expireInterval)
096: return value;
097: else {
098: Object v = value;
099: value = null;
100:
101: if (v instanceof CacheListener)
102: ((CacheListener) v).removeEvent();
103:
104: return null;
105: }
106: }
107: }
|