org.hibernate.cache.ehcache.internal.strategy.ReadOnlyEhcacheNaturalIdRegionAccessStrategy Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.cache.ehcache.internal.strategy;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.ehcache.internal.regions.EhcacheNaturalIdRegion;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
/**
* Ehcache specific read-only NaturalId region access strategy
*
* @author Chris Dennis
* @author Alex Snaps
*/
public class ReadOnlyEhcacheNaturalIdRegionAccessStrategy
extends AbstractEhcacheAccessStrategy
implements NaturalIdRegionAccessStrategy {
/**
* Create a read-only access strategy accessing the given NaturalId region.
*
* @param region THe wrapped region
* @param settings The Hibermate settings
*/
public ReadOnlyEhcacheNaturalIdRegionAccessStrategy(EhcacheNaturalIdRegion region, SessionFactoryOptions settings) {
super( region, settings );
}
@Override
public NaturalIdRegion getRegion() {
return region();
}
@Override
public Object get(Object key, long txTimestamp) throws CacheException {
return region().get( key );
}
@Override
public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
throws CacheException {
if ( minimalPutOverride && region().contains( key ) ) {
return false;
}
else {
region().put( key, value );
return true;
}
}
@Override
public SoftLock lockItem(Object key, Object version) throws UnsupportedOperationException {
return null;
}
/**
* {@inheritDoc}
*
* A no-op since this cache is read-only
*/
@Override
public void unlockItem(Object key, SoftLock lock) throws CacheException {
region().remove( key );
}
/**
* {@inheritDoc}
*
* This cache is asynchronous hence a no-op
*/
@Override
public boolean insert(Object key, Object value) throws CacheException {
return false;
}
@Override
public boolean afterInsert(Object key, Object value) throws CacheException {
region().put( key, value );
return true;
}
/**
* {@inheritDoc}
*
* Throws UnsupportedOperationException since this cache is read-only
*
* @throws UnsupportedOperationException always
*/
@Override
public boolean update(Object key, Object value) throws UnsupportedOperationException {
throw new UnsupportedOperationException( "Can't write to a readonly object" );
}
/**
* {@inheritDoc}
*
* Throws UnsupportedOperationException since this cache is read-only
*
* @throws UnsupportedOperationException always
*/
@Override
public boolean afterUpdate(Object key, Object value, SoftLock lock) throws UnsupportedOperationException {
throw new UnsupportedOperationException( "Can't write to a readonly object" );
}
}