01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19:
20: package de.schlund.pfixcore.webservice.utils;
21:
22: import java.util.LinkedHashMap;
23: import java.util.Map;
24:
25: import org.apache.log4j.Logger;
26:
27: /**
28: * @author mleidig@schlund.de
29: */
30: public class FileCache {
31:
32: final static Logger LOG = Logger.getLogger(FileCache.class);
33: private LinkedHashMap<String, FileCacheData> map;
34:
35: public FileCache(int size) {
36: final int maxSize = size;
37: map = new LinkedHashMap<String, FileCacheData>(maxSize, 0.75f,
38: true) {
39: protected boolean removeEldestEntry(
40: Map.Entry<String, FileCacheData> eldest) {
41: boolean exceeded = size() > maxSize;
42: if (exceeded)
43: LOG
44: .warn("Cache maximum size exceeded. Eldest entry to be removed.");
45: return exceeded;
46: }
47: };
48: }
49:
50: public synchronized void put(String name, FileCacheData data) {
51: map.put(name, data);
52: }
53:
54: public synchronized FileCacheData get(String name) {
55: FileCacheData data = map.get(name);
56: return data;
57: }
58:
59: public static void main(String[] args) {
60: FileCache cache = new FileCache(3);
61: cache.put("a", new FileCacheData("aaa".getBytes()));
62: cache.put("b", new FileCacheData("bbb".getBytes()));
63: cache.put("c", new FileCacheData("ccc".getBytes()));
64: cache.get("a");
65: cache.get("b");
66: cache.put("d", new FileCacheData("ddd".getBytes()));
67: FileCacheData data = cache.get("b");
68: String res = (data == null) ? "null" : new String(data.bytes)
69: + " " + data.md5;
70: System.out.println(res);
71: }
72:
73: }
|