/** * Remove all mappings without regard to transactional safety * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evictAll() * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#evictAll() */ public final void evictAll() throws CacheException { region.clear(); } }
/** * Soft-lock a cache item. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockItem(java.lang.Object, java.lang.Object) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#lockItem(java.lang.Object, java.lang.Object) */ public final SoftLock lockItem(Object key, Object version) throws CacheException { region.writeLock(key); try { Lockable item = (Lockable) region.get(key); long timeout = region.nextTimestamp() + region.getTimeout(); final Lock lock = (item == null) ? new Lock(timeout, uuid, nextLockId(), version) : item.lock(timeout, uuid, nextLockId()); putLock(key, lock); return lock; } finally { region.writeUnlock(key); } }
/** * Creates a read/write cache access strategy around the given cache region. */ public AbstractReadWriteEhcacheAccessStrategy(T region, Settings settings) { super(region, settings); this.versionComparator = region.getCacheDataDescription().getVersionComparator(); }
/** * Read unlock the entry for the given key if internal cache locks will not provide correct exclusion. */ private void readUnlockIfNeeded(Object key) { if (region.locksAreIndependentOfCache()) { region.readUnlock(key); } }
/** * Read lock the entry for the given key if internal cache locks will not provide correct exclusion. */ private void readLockIfNeeded(Object key) { if (region.locksAreIndependentOfCache()) { region.readLock(key); } }
private void putLock(final Object key, final Lock newLock) { if (newLock.isLocked()) { region.putEternal(key, newLock); } else { region.put(key, newLock); } }
/** * Returns <code>false</code> and fails to put the value if there is an existing un-writeable item mapped to this * key. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean) */ @Override public final boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException { region.writeLock(key); try { Lockable item = (Lockable) region.get(key); boolean writeable = item == null || item.isWriteable(txTimestamp, version, versionComparator); if (writeable) { region.put(key, new Item(value, version, region.nextTimestamp())); return true; } else { return false; } } finally { region.writeUnlock(key); } }
/** * Handle the timeout of a previous lock mapped to this key */ protected void handleMissingLock(Object key, Lockable lock) { LOG.error("Cache " + region.getName() + " Key " + key + " Lockable : " + lock + "\n" + "A soft-locked cache entry was removed already. Out of balance lock/unlock sequences ?"); long ts = region.nextTimestamp() + region.getTimeout(); // create new lock that times out immediately Lock newLock = new Lock(ts, uuid, nextLockId.getAndIncrement(), null); newLock.unlock(ts); // This isn't really necessary, could be a simple put, but for completeness, though given pinning, this also should never happen putLock(key, newLock); }
/** * Soft-unlock a cache item. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock) */ public final void unlockItem(Object key, SoftLock lock) throws CacheException { region.writeLock(key); try { Lockable item = (Lockable) region.get(key); if ((item != null) && item.isUnlockable(lock)) { decrementLock(key, (Lock) item); } else { handleMissingLock(key, item); } } finally { region.writeUnlock(key); } }
/** * Map the given value to the given key, replacing any existing mapping for this key, * pinning the key in the cache */ public final void putEternal(Object key, Object value) throws CacheException { put(key, value, true); }
/** * Unlock and re-put the given key, lock combination. */ protected void decrementLock(Object key, Lock lock) { lock.unlock(region.nextTimestamp()); putLock(key, lock); }
/** * Returns <code>null</code> if the item is not readable. Locked items are not readable, nor are items created * after the start of this transaction. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#get(java.lang.Object, long) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#get(java.lang.Object, long) */ public final Object get(Object key, long txTimestamp) throws CacheException { readLockIfNeeded(key); try { Lockable item = (Lockable) region.get(key); boolean readable = item != null && item.isReadable(txTimestamp); if (readable) { return item.getValue(); } else { return null; } } finally { readUnlockIfNeeded(key); } }
/** * Returns <code>false</code> and fails to put the value if there is an existing un-writeable item mapped to this * key. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean) */ @Override public final boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException { region.writeLock(key); try { Lockable item = (Lockable) region.get(key); boolean writeable = item == null || item.isWriteable(txTimestamp, version, versionComparator); if (writeable) { region.put(key, new Item(value, version, region.nextTimestamp())); return true; } else { return false; } } finally { region.writeUnlock(key); } }
/** * Handle the timeout of a previous lock mapped to this key */ protected void handleMissingLock(Object key, Lockable lock) { LOG.error("Cache " + region.getName() + " Key " + key + " Lockable : " + lock + "\n" + "A soft-locked cache entry was removed already. Out of balance lock/unlock sequences ?"); long ts = region.nextTimestamp() + region.getTimeout(); // create new lock that times out immediately Lock newLock = new Lock(ts, uuid, nextLockId.getAndIncrement(), null); newLock.unlock(ts); // This isn't really necessary, could be a simple put, but for completeness, though given pinning, this also should never happen putLock(key, newLock); }
/** * Soft-unlock a cache item. * * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock) * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock) */ public final void unlockItem(Object key, SoftLock lock) throws CacheException { region.writeLock(key); try { Lockable item = (Lockable) region.get(key); if ((item != null) && item.isUnlockable(lock)) { decrementLock(key, (Lock) item); } else { handleMissingLock(key, item); } } finally { region.writeUnlock(key); } }
/** * Read unlock the entry for the given key if internal cache locks will not provide correct exclusion. */ private void readUnlockIfNeeded(Object key) { if (region.locksAreIndependentOfCache()) { region.readUnlock(key); } }
/** * Read lock the entry for the given key if internal cache locks will not provide correct exclusion. */ private void readLockIfNeeded(Object key) { if (region.locksAreIndependentOfCache()) { region.readLock(key); } }
private void putLock(final Object key, final Lock newLock) { if (newLock.isLocked()) { region.putEternal(key, newLock); } else { region.put(key, newLock); } }
/** * Map the given value to the given key, replacing any existing mapping for this key * this unpins the key in the cache should it be currently pinned */ public final void put(Object key, Object value) throws CacheException { put(key, value, false); }