1
//$Id: EhCache.java 10716 2006-11-03 19:05:11Z max.andersen@jboss.com $
3
* Copyright 2003-2006 Greg Luck, Jboss Inc
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.hibernate.cache;
19
import java.io.IOException;
20
import java.util.HashMap;
21
import java.util.Iterator;
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;
30
* EHCache plugin for Hibernate
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.
39
* @author Emmanuel Bernard
41
public class EhCache implements Cache {
42
private static final Log log = LogFactory.getLog( EhCache.class );
44
private static final int SIXTY_THOUSAND_MS = 60000;
46
private net.sf.ehcache.Cache cache;
49
* Creates a new Hibernate pluggable cache based on a cache name.
52
* @param cache The underlying EhCache instance to use.
54
public EhCache(net.sf.ehcache.Cache cache) {
59
* Gets a value of an element which matches the given key.
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
65
public Object get(Object key) throws CacheException {
67
if ( log.isDebugEnabled() ) {
68
log.debug( "key: " + key );
74
Element element = cache.get( key );
75
if ( element == null ) {
76
if ( log.isDebugEnabled() ) {
77
log.debug( "Element for " + key + " is null" );
82
return element.getObjectValue();
86
catch (net.sf.ehcache.CacheException e) {
87
throw new CacheException( e );
91
public Object read(Object key) throws CacheException {
97
* Puts an object into the cache.
100
* @param value a value
101
* @throws CacheException if the {@link CacheManager}
102
* is shutdown or another {@link Exception} occurs.
104
public void update(Object key, Object value) throws CacheException {
109
* Puts an object into the cache.
112
* @param value a value
113
* @throws CacheException if the {@link CacheManager}
114
* is shutdown or another {@link Exception} occurs.
116
public void put(Object key, Object value) throws CacheException {
118
Element element = new Element( key, value );
119
cache.put( element );
121
catch (IllegalArgumentException e) {
122
throw new CacheException( e );
124
catch (IllegalStateException e) {
125
throw new CacheException( e );
127
catch (net.sf.ehcache.CacheException e) {
128
throw new CacheException( e );
134
* Removes the element which matches the key.
136
* If no element matches, nothing is removed and no Exception is thrown.
138
* @param key the key of the element to remove
139
* @throws CacheException
141
public void remove(Object key) throws CacheException {
145
catch (ClassCastException e) {
146
throw new CacheException( e );
148
catch (IllegalStateException e) {
149
throw new CacheException( e );
151
catch (net.sf.ehcache.CacheException e) {
152
throw new CacheException( e );
157
* Remove all elements in the cache, but leave the cache
158
* in a useable state.
160
* @throws CacheException
162
public void clear() throws CacheException {
166
catch (IllegalStateException e) {
167
throw new CacheException( e );
169
catch (net.sf.ehcache.CacheException e) {
170
throw new CacheException( e );
175
* Remove the cache and make it unuseable.
177
* @throws CacheException
179
public void destroy() throws CacheException {
181
cache.getCacheManager().removeCache( cache.getName() );
183
catch (IllegalStateException e) {
184
throw new CacheException( e );
186
catch (net.sf.ehcache.CacheException e) {
187
throw new CacheException( e );
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.
196
public void lock(Object key) throws CacheException {
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.
204
public void unlock(Object key) throws CacheException {
208
* Gets the next timestamp;
210
public long nextTimestamp() {
211
return Timestamper.next();
215
* Returns the lock timeout for this cache.
217
public int getTimeout() {
218
// 60 second lock timeout
219
return Timestamper.ONE_MS * SIXTY_THOUSAND_MS;
222
public String getRegionName() {
223
return cache.getName();
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
232
* @return the approximate size of memory ehcache is using for the MemoryStore for this cache
234
public long getSizeInMemory() {
236
return cache.calculateInMemorySize();
238
catch (Throwable t) {
243
public long getElementCountInMemory() {
245
return cache.getMemoryStoreSize();
247
catch (net.sf.ehcache.CacheException ce) {
248
throw new CacheException( ce );
252
public long getElementCountOnDisk() {
253
return cache.getDiskStoreSize();
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() );
266
catch (Exception e) {
267
throw new CacheException( e );
271
public String toString() {
272
return "EHCache(" + getRegionName() + ')';
b'\\ No newline at end of file'