~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/constructs/blocking/BlockingCache.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
61
61
 * Further improvements to hashing suggested by Joe Bowbeer.
62
62
 *
63
63
 * @author Greg Luck
64
 
 * @version $Id: BlockingCache.java 3188 2010-11-10 15:28:24Z cdennis $
 
64
 * @version $Id: BlockingCache.java 5881 2012-06-29 18:12:01Z alexsnaps $
65
65
 */
66
66
public class BlockingCache extends EhcacheDecoratorAdapter {
67
67
 
202
202
     * Adds an entry and unlocks it
203
203
     */
204
204
    @Override
205
 
    public void put(Element element) {
206
 
 
207
 
        if (element == null) {
208
 
            return;
 
205
    public void put(final Element element) {
 
206
 
 
207
        doAndReleaseWriteLock(new PutAction<Void>(element) {
 
208
            @Override
 
209
            public Void put() {
 
210
                if (element.getObjectValue() != null) {
 
211
                    underlyingCache.put(element);
 
212
                } else {
 
213
                    underlyingCache.remove(element.getObjectKey());
 
214
                }
 
215
                return null;
 
216
            }
 
217
        });
 
218
    }
 
219
 
 
220
    @Override
 
221
    public void put(final Element element, final boolean doNotNotifyCacheReplicators)
 
222
        throws IllegalArgumentException, IllegalStateException, CacheException {
 
223
        doAndReleaseWriteLock(new PutAction<Void>(element) {
 
224
            @Override
 
225
            public Void put() {
 
226
                underlyingCache.put(element, doNotNotifyCacheReplicators);
 
227
                return null;
 
228
            }
 
229
        });
 
230
    }
 
231
 
 
232
    @Override
 
233
    public void putQuiet(final Element element) throws IllegalArgumentException, IllegalStateException, CacheException {
 
234
        doAndReleaseWriteLock(new PutAction<Void>(element) {
 
235
            @Override
 
236
            public Void put() {
 
237
                underlyingCache.putQuiet(element);
 
238
                return null;
 
239
            }
 
240
        });
 
241
    }
 
242
 
 
243
    @Override
 
244
    public void putWithWriter(final Element element) throws IllegalArgumentException, IllegalStateException, CacheException {
 
245
        doAndReleaseWriteLock(new PutAction<Void>(element) {
 
246
            @Override
 
247
            public Void put() {
 
248
                underlyingCache.putWithWriter(element);
 
249
                return null;
 
250
            }
 
251
        });
 
252
    }
 
253
 
 
254
    @Override
 
255
    public Element putIfAbsent(final Element element) throws NullPointerException {
 
256
        return doAndReleaseWriteLock(new PutAction<Element>(element) {
 
257
            @Override
 
258
            public Element put() {
 
259
                return underlyingCache.putIfAbsent(element);
 
260
            }
 
261
        });
 
262
    }
 
263
 
 
264
    @Override
 
265
    public Element putIfAbsent(final Element element, final boolean doNotNotifyCacheReplicators) throws NullPointerException {
 
266
        return doAndReleaseWriteLock(new PutAction<Element>(element) {
 
267
            @Override
 
268
            public Element put() {
 
269
                return underlyingCache.putIfAbsent(element, doNotNotifyCacheReplicators);
 
270
            }
 
271
        });
 
272
    }
 
273
 
 
274
    private <V> V doAndReleaseWriteLock(PutAction<V> putAction) {
 
275
 
 
276
        if (putAction.element == null) {
 
277
            return null;
209
278
        }
210
 
        Object key = element.getObjectKey();
211
 
        Object value = element.getObjectValue();
 
279
 
 
280
        Object key = putAction.element.getObjectKey();
212
281
 
213
282
        Sync lock = getLockForKey(key);
214
 
        
 
283
 
215
284
        if (!lock.isHeldByCurrentThread(LockType.WRITE)) {
216
285
            lock.lock(LockType.WRITE);
217
286
        }
218
287
        try {
219
 
            if (value != null) {
220
 
                underlyingCache.put(element);
221
 
            } else {
222
 
                underlyingCache.remove(key);
223
 
            }
 
288
            return putAction.put();
224
289
        } finally {
225
 
            //Release the readlock here. This will have been acquired in the get, where the element was null
 
290
            //Release the writelock here. This will have been acquired in the get, where the element was null
226
291
            lock.unlock(LockType.WRITE);
227
292
        }
228
293
    }
355
420
    public void loadAll(Collection keys, Object argument) throws CacheException {
356
421
        throw new CacheException("This method is not appropriate for a Blocking Cache");
357
422
    }
 
423
 
 
424
    /**
 
425
     * Callable like class to actually execute one of the many Ehcache.put* methods in the context of a BlockingCache
 
426
     * @param <V>
 
427
     */
 
428
    private abstract static class PutAction<V> {
 
429
 
 
430
        private final Element element;
 
431
 
 
432
        private PutAction(Element element) {
 
433
            this.element = element;
 
434
        }
 
435
 
 
436
        /**
 
437
         * implement method with the put*
 
438
         * @return the return value of the put*
 
439
         */
 
440
        abstract V put();
 
441
    }
358
442
}
359
443
 
360
444