private void writeLock(Object key) { if (cache.canLockEntries()) { cache.lock(key); } else { coarseWriteLock.lock(); } }
/** * {@inheritDoc} */ public final void clear() throws CacheException { cache.clear(); }
/** * {@inheritDoc} */ public final void destroy() { try { cache.destroy(); } catch (Exception e) { LOG.warn("could not destroy cache", e); } } }
private void decrementLock(Object key, Lock lock) { lock.unlock(cache.nextTimestamp()); cache.update(key, lock); }
/** * {@inheritDoc} */ public boolean afterInsert(Object key, Object value, Object version) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); if (item == null) { cache.update(key, new Item(value, version, cache.nextTimestamp())); return true; } else { return false; } } finally { writeUnlock(key); } }
/** * Soft-locks the associated mapping prior to updating/inserting a new value. */ public SoftLock lock(Object key, Object version) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); long timeout = cache.nextTimestamp() + cache.getTimeout(); final Lock lock = (item == null) ? new Lock(timeout, uuid, nextLockId(), version) : item.lock(timeout, uuid, nextLockId()); cache.update(key, lock); return lock; } finally { writeUnlock(key); } }
private void handleLockExpiry(Object key) { long ts = cache.nextTimestamp() + cache.getTimeout(); // create new lock that times out immediately Lock lock = new Lock(ts, uuid, nextLockId.getAndIncrement(), null); lock.unlock(ts); cache.update(key, lock); }
/** * {@inheritDoc} */ public boolean put(Object key, Object value, long txTimestamp, Object version, Comparator versionComparator, boolean minimalPut) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); boolean writeable = item == null || item.isWriteable(txTimestamp, version, versionComparator); if (writeable) { cache.put(key, new Item(value, version, cache.nextTimestamp())); return true; } else { return false; } } finally { writeUnlock(key); } }
/** * {@inheritDoc} */ public Object get(Object key, long txTimestamp) throws CacheException { return cache.get(key); }
private void writeUnlock(Object key) { if (cache.canLockEntries()) { cache.unlock(key); } else { coarseWriteLock.unlock(); } }
/** * Builds a Cache. * <p> * Even though this method provides properties, they are not used. * Properties for EHCache are specified in the ehcache.xml file. * Configuration will be read from ehcache.xml for a cache declaration * where the name attribute matches the name parameter in this builder. * * @param name the name of the cache. Must match a cache configured in ehcache.xml * @param properties not used * @return a newly built cache will be built and initialised * @throws org.hibernate.cache.CacheException * inter alia, if a cache of the same name already exists */ public final Cache buildCache(String name, Properties properties) throws CacheException { try { net.sf.ehcache.Ehcache cache = manager.getEhcache(name); if (cache == null) { LOG.warn("Could not find a specific ehcache configuration for cache named [" + name + "]; using defaults."); manager.addCache(name); cache = manager.getEhcache(name); LOG.debug("started EHCache region: " + name); } return new EhCache(cache); } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e); } }
/** * @return the region name, which is the cache name in ehcache */ @Override public final String toString() { return "EHCache(" + getRegionName() + ')'; }
private void readLockIfCoarse(Object key) { if (!cache.canLockEntries()) { coarseReadLock.lock(); } }
/** * Updates an object in the cache, or if it does not exist, inserts it. * * @param key an Object key * @param value an Object value * @throws CacheException if the {@link net.sf.ehcache.CacheManager} is shutdown or another {@link Exception} occurs. */ public final void update(Object key, Object value) throws CacheException { put(key, value); }
/** * {@inheritDoc} */ public boolean afterUpdate(Object key, Object value, Object version, SoftLock softlock) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); if (item != null && item.isUnlockable(softlock)) { Lock lock = (Lock) item; if (lock.wasLockedConcurrently()) { decrementLock(key, lock); return false; } else { cache.update(key, new Item(value, version, cache.nextTimestamp())); return true; } } else { handleLockExpiry(key); return false; } } finally { writeUnlock(key); } }
/** * Soft-locks the associated mapping prior to updating/inserting a new value. */ public SoftLock lock(Object key, Object version) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); long timeout = cache.nextTimestamp() + cache.getTimeout(); final Lock lock = (item == null) ? new Lock(timeout, uuid, nextLockId(), version) : item.lock(timeout, uuid, nextLockId()); cache.update(key, lock); return lock; } finally { writeUnlock(key); } }
private void handleLockExpiry(Object key) { long ts = cache.nextTimestamp() + cache.getTimeout(); // create new lock that times out immediately Lock lock = new Lock(ts, uuid, nextLockId.getAndIncrement(), null); lock.unlock(ts); cache.update(key, lock); }
/** * {@inheritDoc} */ public boolean put(Object key, Object value, long txTimestamp, Object version, Comparator versionComparator, boolean minimalPut) throws CacheException { writeLock(key); try { Lockable item = (Lockable) cache.get(key); boolean writeable = item == null || item.isWriteable(txTimestamp, version, versionComparator); if (writeable) { cache.put(key, new Item(value, version, cache.nextTimestamp())); return true; } else { return false; } } finally { writeUnlock(key); } }