Files
alfresco-community-repo/source/java/org/alfresco/repo/cache/HibernateSimpleCacheAdapter.java
Raluca Munteanu 8674e2bfc8 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 12:48:49 +00:00

137 lines
2.8 KiB
Java

package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.Map;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.Timestamper;
/**
* Adapts a {@link SimpleCache} instance for use as a Hibernate {@link Cache}.
*
* @author Matt Ward
*/
public class HibernateSimpleCacheAdapter implements Cache
{
private final SimpleCache<Serializable, Object> cache;
private final String regionName;
/**
* Adapt a
* @param cache SimpleCache<Serializable, Object>
* @param regionName String
*/
public HibernateSimpleCacheAdapter(SimpleCache<Serializable, Object> cache, String regionName)
{
this.cache = cache;
this.regionName = regionName;
}
@Override
public Object read(Object key) throws CacheException
{
return cache.get(serializable(key));
}
@Override
public Object get(Object key) throws CacheException
{
return cache.get(serializable(key));
}
@Override
public void put(Object key, Object value) throws CacheException
{
cache.put(serializable(key), value);
}
@Override
public void update(Object key, Object value) throws CacheException
{
cache.put(serializable(key), value);
}
@Override
public void remove(Object key) throws CacheException
{
cache.remove(serializable(key));
}
@Override
public void clear() throws CacheException
{
cache.clear();
}
@Override
public void destroy() throws CacheException
{
// NoOp
}
@Override
public void lock(Object key) throws CacheException
{
// NoOp
}
@Override
public void unlock(Object key) throws CacheException
{
// NoOp
}
@Override
public long nextTimestamp()
{
return Timestamper.next();
}
@Override
public int getTimeout()
{
return Timestamper.ONE_MS * 60000; // 1 minute
}
@Override
public String getRegionName()
{
return regionName;
}
@Override
public long getSizeInMemory()
{
return -1;
}
@Override
public long getElementCountInMemory()
{
return -1;
}
@Override
public long getElementCountOnDisk()
{
return 0;
}
@Override
public Map toMap()
{
throw new UnsupportedOperationException();
}
private Serializable serializable(Object obj)
{
if (!(obj instanceof Serializable))
{
throw new IllegalArgumentException("Object is not Serializable, class=" + obj.getClass().getName());
}
return (Serializable) obj;
}
}