1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.util; |
16 | |
17 | import org.apache.commons.logging.Log; |
18 | import org.apache.commons.logging.LogFactory; |
19 | |
20 | import net.sf.ehcache.Cache; |
21 | import net.sf.ehcache.CacheException; |
22 | import net.sf.ehcache.Element; |
23 | |
24 | public class ImageCache |
25 | { |
26 | static private final Log _logger = LogFactory.getLog(ImageCache.class); |
27 | |
28 | public ImageCache(Cache cache, int id) |
29 | { |
30 | _cache = cache; |
31 | _id = id; |
32 | } |
33 | |
34 | synchronized public void removeCache() |
35 | { |
36 | _cache.remove(_id); |
37 | } |
38 | |
39 | synchronized public void writeCache(ImageHolder image) |
40 | { |
41 | _cache.put(new Element(_id, image)); |
42 | } |
43 | |
44 | synchronized public ImageHolder readCache() |
45 | { |
46 | try |
47 | { |
48 | Element item = _cache.get(_id); |
49 | if (item == null) |
50 | { |
51 | return null; |
52 | } |
53 | return (ImageHolder) item.getValue(); |
54 | } |
55 | catch (CacheException e) |
56 | { |
57 | _logger.error("readCache() id " + _id, e); |
58 | return null; |
59 | } |
60 | } |
61 | |
62 | private final Cache _cache; |
63 | private final int _id; |
64 | } |