~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/cache/EhCache.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: EhCache.java 10716 2006-11-03 19:05:11Z max.andersen@jboss.com $
 
2
/**
 
3
 *  Copyright 2003-2006 Greg Luck, Jboss Inc
 
4
 *
 
5
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *  you may not use this file except in compliance with the License.
 
7
 *  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 */
 
17
package org.hibernate.cache;
 
18
 
 
19
import java.io.IOException;
 
20
import java.util.HashMap;
 
21
import java.util.Iterator;
 
22
import java.util.Map;
 
23
 
 
24
import net.sf.ehcache.CacheManager;
 
25
import net.sf.ehcache.Element;
 
26
import org.apache.commons.logging.Log;
 
27
import org.apache.commons.logging.LogFactory;
 
28
 
 
29
/**
 
30
 * EHCache plugin for Hibernate
 
31
 * <p/>
 
32
 * EHCache uses a {@link net.sf.ehcache.store.MemoryStore} and a
 
33
 * {@link net.sf.ehcache.store.DiskStore}.
 
34
 * The {@link net.sf.ehcache.store.DiskStore} requires that both keys and values be {@link java.io.Serializable}.
 
35
 * However the MemoryStore does not and in ehcache-1.2 nonSerializable Objects are permitted. They are discarded
 
36
 * if an attempt it made to overflow them to Disk or to replicate them to remote cache peers.
 
37
 *
 
38
 * @author Greg Luck
 
39
 * @author Emmanuel Bernard
 
40
 */
 
41
public class EhCache implements Cache {
 
42
        private static final Log log = LogFactory.getLog( EhCache.class );
 
43
 
 
44
        private static final int SIXTY_THOUSAND_MS = 60000;
 
45
 
 
46
        private net.sf.ehcache.Cache cache;
 
47
 
 
48
        /**
 
49
         * Creates a new Hibernate pluggable cache based on a cache name.
 
50
         * <p/>
 
51
         *
 
52
         * @param cache The underlying EhCache instance to use.
 
53
         */
 
54
        public EhCache(net.sf.ehcache.Cache cache) {
 
55
                this.cache = cache;
 
56
        }
 
57
 
 
58
        /**
 
59
         * Gets a value of an element which matches the given key.
 
60
         *
 
61
         * @param key the key of the element to return.
 
62
         * @return The value placed into the cache with an earlier put, or null if not found or expired
 
63
         * @throws CacheException
 
64
         */
 
65
        public Object get(Object key) throws CacheException {
 
66
                try {
 
67
                        if ( log.isDebugEnabled() ) {
 
68
                                log.debug( "key: " + key );
 
69
                        }
 
70
                        if ( key == null ) {
 
71
                                return null;
 
72
                        }
 
73
                        else {
 
74
                                Element element = cache.get( key );
 
75
                                if ( element == null ) {
 
76
                                        if ( log.isDebugEnabled() ) {
 
77
                                                log.debug( "Element for " + key + " is null" );
 
78
                                        }
 
79
                                        return null;
 
80
                                }
 
81
                                else {
 
82
                                        return element.getObjectValue();
 
83
                                }
 
84
                        }
 
85
                }
 
86
                catch (net.sf.ehcache.CacheException e) {
 
87
                        throw new CacheException( e );
 
88
                }
 
89
        }
 
90
 
 
91
        public Object read(Object key) throws CacheException {
 
92
                return get( key );
 
93
        }
 
94
 
 
95
 
 
96
        /**
 
97
         * Puts an object into the cache.
 
98
         *
 
99
         * @param key   a key
 
100
         * @param value a value
 
101
         * @throws CacheException if the {@link CacheManager}
 
102
         *                        is shutdown or another {@link Exception} occurs.
 
103
         */
 
104
        public void update(Object key, Object value) throws CacheException {
 
105
                put( key, value );
 
106
        }
 
107
 
 
108
        /**
 
109
         * Puts an object into the cache.
 
110
         *
 
111
         * @param key   a key
 
112
         * @param value a value
 
113
         * @throws CacheException if the {@link CacheManager}
 
114
         *                        is shutdown or another {@link Exception} occurs.
 
115
         */
 
116
        public void put(Object key, Object value) throws CacheException {
 
117
                try {
 
118
                        Element element = new Element( key, value );
 
119
                        cache.put( element );
 
120
                }
 
121
                catch (IllegalArgumentException e) {
 
122
                        throw new CacheException( e );
 
123
                }
 
124
                catch (IllegalStateException e) {
 
125
                        throw new CacheException( e );
 
126
                }
 
127
                catch (net.sf.ehcache.CacheException e) {
 
128
                        throw new CacheException( e );
 
129
                }
 
130
 
 
131
        }
 
132
 
 
133
        /**
 
134
         * Removes the element which matches the key.
 
135
         * <p/>
 
136
         * If no element matches, nothing is removed and no Exception is thrown.
 
137
         *
 
138
         * @param key the key of the element to remove
 
139
         * @throws CacheException
 
140
         */
 
141
        public void remove(Object key) throws CacheException {
 
142
                try {
 
143
                        cache.remove( key );
 
144
                }
 
145
                catch (ClassCastException e) {
 
146
                        throw new CacheException( e );
 
147
                }
 
148
                catch (IllegalStateException e) {
 
149
                        throw new CacheException( e );
 
150
                }
 
151
                catch (net.sf.ehcache.CacheException e) {
 
152
                        throw new CacheException( e );
 
153
                }
 
154
        }
 
155
 
 
156
        /**
 
157
         * Remove all elements in the cache, but leave the cache
 
158
         * in a useable state.
 
159
         *
 
160
         * @throws CacheException
 
161
         */
 
162
        public void clear() throws CacheException {
 
163
                try {
 
164
                        cache.removeAll();
 
165
                }
 
166
                catch (IllegalStateException e) {
 
167
                        throw new CacheException( e );
 
168
                }
 
169
                catch (net.sf.ehcache.CacheException e) {
 
170
                        throw new CacheException( e );
 
171
                }
 
172
        }
 
173
 
 
174
        /**
 
175
         * Remove the cache and make it unuseable.
 
176
         *
 
177
         * @throws CacheException
 
178
         */
 
179
        public void destroy() throws CacheException {
 
180
                try {
 
181
                        cache.getCacheManager().removeCache( cache.getName() );
 
182
                }
 
183
                catch (IllegalStateException e) {
 
184
                        throw new CacheException( e );
 
185
                }
 
186
                catch (net.sf.ehcache.CacheException e) {
 
187
                        throw new CacheException( e );
 
188
                }
 
189
        }
 
190
 
 
191
        /**
 
192
         * Calls to this method should perform there own synchronization.
 
193
         * It is provided for distributed caches. Because EHCache is not distributed
 
194
         * this method does nothing.
 
195
         */
 
196
        public void lock(Object key) throws CacheException {
 
197
        }
 
198
 
 
199
        /**
 
200
         * Calls to this method should perform there own synchronization.
 
201
         * It is provided for distributed caches. Because EHCache is not distributed
 
202
         * this method does nothing.
 
203
         */
 
204
        public void unlock(Object key) throws CacheException {
 
205
        }
 
206
 
 
207
        /**
 
208
         * Gets the next timestamp;
 
209
         */
 
210
        public long nextTimestamp() {
 
211
                return Timestamper.next();
 
212
        }
 
213
 
 
214
        /**
 
215
         * Returns the lock timeout for this cache.
 
216
         */
 
217
        public int getTimeout() {
 
218
                // 60 second lock timeout
 
219
                return Timestamper.ONE_MS * SIXTY_THOUSAND_MS;
 
220
        }
 
221
 
 
222
        public String getRegionName() {
 
223
                return cache.getName();
 
224
        }
 
225
 
 
226
        /**
 
227
         * Warning: This method can be very expensive to run. Allow approximately 1 second
 
228
         * per 1MB of entries. Running this method could create liveness problems
 
229
         * because the object lock is held for a long period
 
230
         * <p/>
 
231
         *
 
232
         * @return the approximate size of memory ehcache is using for the MemoryStore for this cache
 
233
         */
 
234
        public long getSizeInMemory() {
 
235
                try {
 
236
                        return cache.calculateInMemorySize();
 
237
                }
 
238
                catch (Throwable t) {
 
239
                        return -1;
 
240
                }
 
241
        }
 
242
 
 
243
        public long getElementCountInMemory() {
 
244
                try {
 
245
                        return cache.getMemoryStoreSize();
 
246
                }
 
247
                catch (net.sf.ehcache.CacheException ce) {
 
248
                        throw new CacheException( ce );
 
249
                }
 
250
        }
 
251
 
 
252
        public long getElementCountOnDisk() {
 
253
                return cache.getDiskStoreSize();
 
254
        }
 
255
 
 
256
        public Map toMap() {
 
257
                try {
 
258
                        Map result = new HashMap();
 
259
                        Iterator iter = cache.getKeys().iterator();
 
260
                        while ( iter.hasNext() ) {
 
261
                                Object key = iter.next();
 
262
                                result.put( key, cache.get( key ).getObjectValue() );
 
263
                        }
 
264
                        return result;
 
265
                }
 
266
                catch (Exception e) {
 
267
                        throw new CacheException( e );
 
268
                }
 
269
        }
 
270
 
 
271
        public String toString() {
 
272
                return "EHCache(" + getRegionName() + ')';
 
273
        }
 
274
 
 
275
}
 
 
b'\\ No newline at end of file'