KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > cache > EhCacheAdapter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.cache;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 import net.sf.ehcache.Cache;
23 import net.sf.ehcache.CacheException;
24 import net.sf.ehcache.Element;
25
26 import org.alfresco.error.AlfrescoRuntimeException;
27
28 /**
29  * A thin adapter for <b>Ehcache</b> support.
30  * <p>
31  * Thread-safety is taken care of by the underlying <b>Ehcache</b>
32  * instance.
33  *
34  * @see org.springframework.cache.ehcache.EhCacheFactoryBean
35  * @see org.springframework.cache.ehcache.EhCacheManagerFactoryBean
36  *
37  * @author Derek Hulley
38  */

39 public class EhCacheAdapter<K extends Serializable JavaDoc, V extends Serializable JavaDoc>
40         implements SimpleCache<K, V>
41 {
42     private net.sf.ehcache.Cache cache;
43     
44     public EhCacheAdapter()
45     {
46     }
47
48     /**
49      * @param cache the backing Ehcache instance
50      */

51     public void setCache(Cache cache)
52     {
53         this.cache = cache;
54     }
55
56     public boolean contains(K key)
57     {
58         try
59         {
60             return (cache.get(key) != null);
61         }
62         catch (CacheException e)
63         {
64             throw new AlfrescoRuntimeException("contains failed", e);
65         }
66     }
67
68     @SuppressWarnings JavaDoc("unchecked")
69     public V get(K key)
70     {
71         try
72         {
73             Element element = cache.get(key);
74             if (element != null)
75             {
76                 return (V) element.getValue();
77             }
78             else
79             {
80                 return null;
81             }
82         }
83         catch (CacheException e)
84         {
85             throw new AlfrescoRuntimeException("Failed to get from EhCache: \n" +
86                     " key: " + key);
87         }
88     }
89
90     public void put(K key, V value)
91     {
92         Element element = new Element(key, value);
93         cache.put(element);
94     }
95
96     public void remove(K key)
97     {
98         cache.remove(key);
99     }
100
101     public void clear()
102     {
103         try
104         {
105             cache.removeAll();
106         }
107         catch (IOException JavaDoc e)
108         {
109             throw new AlfrescoRuntimeException("Failed to clear cache", e);
110         }
111     }
112 }
113
Popular Tags