001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.cache;
024:
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import biz.hammurapi.config.ConfigurationException;
032: import biz.hammurapi.util.Acceptor;
033:
034: /**
035: * Simple cache. Uses map internally.
036: * @author Pavel Vlasov
037: * @version $Revision: 1.3 $
038: */
039: public class SimpleMemoryCache extends AbstractProducer implements
040: Cache {
041: private Map cache = new HashMap();
042: private Producer producer;
043:
044: /**
045: *
046: */
047: public SimpleMemoryCache(Producer producer) {
048: super ();
049:
050: this .producer = producer;
051: if (producer != null) {
052: producer.addCache(this );
053: }
054:
055: }
056:
057: public void put(Object key, final Object value, final long time,
058: final long expirationTime) {
059: synchronized (cache) {
060: cache.put(key, new Entry() {
061:
062: public long getExpirationTime() {
063: return expirationTime;
064: }
065:
066: public long getTime() {
067: return time;
068: }
069:
070: public Object get() {
071: return value;
072: }
073:
074: });
075: }
076: }
077:
078: public Entry get(Object key) {
079:
080: synchronized (cache) {
081: Entry entry = (Entry) cache.get(key);
082: long now = System.currentTimeMillis();
083: if (entry != null) {
084: if (entry.getExpirationTime() > 0
085: && entry.getExpirationTime() < now) {
086: cache.remove(key);
087: } else {
088: return entry;
089: }
090: }
091:
092: if (producer != null) {
093: entry = producer.get(key);
094: cache.put(key, entry);
095: return entry;
096: }
097:
098: return null;
099: }
100: }
101:
102: public void clear() {
103: synchronized (cache) {
104: cache.clear();
105: }
106: }
107:
108: public void remove(Object key) {
109: synchronized (cache) {
110: cache.remove(key);
111: }
112: onRemove(key);
113: }
114:
115: public void remove(Acceptor acceptor) {
116: synchronized (cache) {
117: Iterator it = cache.keySet().iterator();
118: while (it.hasNext()) {
119: Object key = it.next();
120: if (acceptor.accept(key)) {
121: it.remove();
122: onRemove(key);
123: }
124: }
125: }
126: }
127:
128: public void stop() {
129: cache.clear();
130: }
131:
132: public Set keySet() {
133: HashSet ret = new HashSet();
134: synchronized (cache) {
135: ret.addAll(cache.keySet());
136: }
137:
138: if (producer != null) {
139: Set pkeys = producer.keySet();
140: if (pkeys != null) {
141: ret.addAll(pkeys);
142: }
143: }
144:
145: return ret;
146: }
147:
148: public boolean isActive() {
149: return true;
150: }
151:
152: public void start() throws ConfigurationException {
153: // Nothing
154: }
155:
156: public void setOwner(Object owner) {
157: // Nothing
158: }
159: }
|