Skip to content

Commit a872885

Browse files
alexsnapssebersole
authored andcommitted
HHH-10770 JCache 2nd-level cache
* Provider and CacheManager creation * Caches creation * Region class hierarchy * Different access strategies
1 parent 030f442 commit a872885

31 files changed

+2045
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
dependencies {
2+
compile project( ':hibernate-core' )
3+
compile( libraries.jcache )
4+
5+
testCompile project( ':hibernate-testing' )
6+
testCompile( libraries.mockito )
7+
testRuntime( libraries.ehcache3 )
8+
}
9+
10+
def pomName() {
11+
return 'Hibernate/JCache Integration'
12+
}
13+
14+
def pomDescription() {
15+
return 'Integration for javax.cache into Hibernate as a second-level caching service'
16+
}
17+
18+
def osgiDescription() {
19+
return pomDescription()
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import javax.cache.Cache;
10+
import org.hibernate.boot.spi.SessionFactoryOptions;
11+
12+
import org.hibernate.cache.CacheException;
13+
import org.hibernate.cache.jcache.access.NonStrictCollectionRegionAccessStrategy;
14+
import org.hibernate.cache.jcache.access.ReadOnlyCollectionRegionAccessStrategy;
15+
import org.hibernate.cache.jcache.access.ReadWriteCollectionRegionAccessStrategy;
16+
import org.hibernate.cache.spi.CacheDataDescription;
17+
import org.hibernate.cache.spi.CollectionRegion;
18+
import org.hibernate.cache.spi.access.AccessType;
19+
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
20+
21+
/**
22+
* @author Alex Snaps
23+
*/
24+
public class JCacheCollectionRegion extends JCacheTransactionalDataRegion implements CollectionRegion {
25+
26+
public JCacheCollectionRegion(Cache<Object, Object> cache, CacheDataDescription metadata, SessionFactoryOptions options) {
27+
super( cache, metadata, options );
28+
}
29+
30+
@Override
31+
public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
32+
switch ( accessType ) {
33+
case READ_ONLY:
34+
return new ReadOnlyCollectionRegionAccessStrategy( this );
35+
case NONSTRICT_READ_WRITE:
36+
return new NonStrictCollectionRegionAccessStrategy( this );
37+
case READ_WRITE:
38+
return new ReadWriteCollectionRegionAccessStrategy( this );
39+
case TRANSACTIONAL:
40+
throw new UnsupportedOperationException( "Implement me!" );
41+
default:
42+
throw new UnsupportedOperationException( "Unknown AccessType: " + accessType.name() );
43+
}
44+
}
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import javax.cache.Cache;
10+
11+
import org.hibernate.boot.spi.SessionFactoryOptions;
12+
import org.hibernate.cache.CacheException;
13+
import org.hibernate.cache.jcache.access.NonStrictEntityRegionAccessStrategy;
14+
import org.hibernate.cache.jcache.access.ReadOnlyEntityRegionAccessStrategy;
15+
import org.hibernate.cache.jcache.access.ReadWriteEntityRegionAccessStrategy;
16+
import org.hibernate.cache.spi.CacheDataDescription;
17+
import org.hibernate.cache.spi.EntityRegion;
18+
import org.hibernate.cache.spi.access.AccessType;
19+
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
20+
21+
/**
22+
* @author Alex Snaps
23+
*/
24+
public class JCacheEntityRegion extends JCacheTransactionalDataRegion implements EntityRegion {
25+
26+
public JCacheEntityRegion(Cache<Object, Object> cache, CacheDataDescription metadata, SessionFactoryOptions options) {
27+
super( cache, metadata, options );
28+
}
29+
30+
@Override
31+
public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
32+
throwIfAccessTypeUnsupported( accessType );
33+
switch ( accessType ) {
34+
case READ_ONLY:
35+
return new ReadOnlyEntityRegionAccessStrategy( this );
36+
case NONSTRICT_READ_WRITE:
37+
return new NonStrictEntityRegionAccessStrategy( this );
38+
case READ_WRITE:
39+
return new ReadWriteEntityRegionAccessStrategy( this );
40+
case TRANSACTIONAL:
41+
return createTransactionalEntityRegionAccessStrategy();
42+
default:
43+
throw new IllegalArgumentException( "Unknown AccessType: " + accessType );
44+
}
45+
}
46+
47+
protected EntityRegionAccessStrategy createTransactionalEntityRegionAccessStrategy() {
48+
throw new UnsupportedOperationException("No org.hibernate.cache.spi.access.AccessType.TRANSACTIONAL support");
49+
}
50+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import javax.cache.Cache;
10+
11+
import org.hibernate.cache.CacheException;
12+
import org.hibernate.cache.spi.GeneralDataRegion;
13+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
14+
15+
/**
16+
* @author Alex Snaps
17+
*/
18+
public class JCacheGeneralDataRegion extends JCacheRegion implements GeneralDataRegion {
19+
20+
public JCacheGeneralDataRegion(Cache<Object, Object> cache) {
21+
super( cache );
22+
}
23+
24+
@Override
25+
public Object get(SharedSessionContractImplementor session, Object key) throws CacheException {
26+
return cache.get( key );
27+
}
28+
29+
@Override
30+
public void put(SharedSessionContractImplementor session, Object key, Object value) throws CacheException {
31+
cache.put( key, value );
32+
}
33+
34+
@Override
35+
public void evict(Object key) throws CacheException {
36+
cache.remove( key );
37+
}
38+
39+
@Override
40+
public void evictAll() throws CacheException {
41+
cache.removeAll();
42+
}
43+
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import org.jboss.logging.annotations.LogMessage;
10+
import org.jboss.logging.annotations.Message;
11+
import org.jboss.logging.annotations.MessageLogger;
12+
13+
import org.hibernate.internal.CoreMessageLogger;
14+
15+
import static org.jboss.logging.Logger.Level.WARN;
16+
17+
/**
18+
* @author Alex Snaps
19+
*/
20+
@MessageLogger(projectCode = "HHH")
21+
public interface JCacheMessageLogger extends CoreMessageLogger {
22+
23+
static final int NAMESPACE = 40000;
24+
25+
@LogMessage(level = WARN)
26+
@Message(
27+
value = "Attempt to restart an already started JCacheRegionFactory. Use sessionFactory.close() between " +
28+
"repeated calls to buildSessionFactory. Using previously created JCacheRegionFactory.",
29+
id = NAMESPACE + 1
30+
)
31+
void attemptToRestartAlreadyStartedJCacheProvider();
32+
33+
@LogMessage(level = WARN)
34+
@Message(
35+
value = "Attempt to restop an already stopped JCacheRegionFactory.",
36+
id = NAMESPACE + 2
37+
)
38+
void attemptToRestopAlreadyStoppedJCacheProvider();
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import javax.cache.Cache;
10+
11+
import org.hibernate.boot.spi.SessionFactoryOptions;
12+
import org.hibernate.cache.CacheException;
13+
import org.hibernate.cache.jcache.access.NonStrictNaturalIdRegionAccessStrategy;
14+
import org.hibernate.cache.jcache.access.ReadOnlyNaturalIdRegionAccessStrategy;
15+
import org.hibernate.cache.jcache.access.ReadWriteNaturalIdRegionAccessStrategy;
16+
import org.hibernate.cache.spi.CacheDataDescription;
17+
import org.hibernate.cache.spi.NaturalIdRegion;
18+
import org.hibernate.cache.spi.access.AccessType;
19+
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
20+
21+
/**
22+
* @author Alex Snaps
23+
*/
24+
public class JCacheNaturalIdRegion extends JCacheTransactionalDataRegion implements NaturalIdRegion {
25+
26+
public JCacheNaturalIdRegion(Cache<Object, Object> cache, CacheDataDescription metadata, SessionFactoryOptions options) {
27+
super( cache, metadata, options );
28+
}
29+
30+
@Override
31+
public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
32+
switch ( accessType ) {
33+
case READ_ONLY:
34+
return new ReadOnlyNaturalIdRegionAccessStrategy( this );
35+
case NONSTRICT_READ_WRITE:
36+
return new NonStrictNaturalIdRegionAccessStrategy( this );
37+
case READ_WRITE:
38+
return new ReadWriteNaturalIdRegionAccessStrategy( this );
39+
case TRANSACTIONAL:
40+
throw new UnsupportedOperationException( "Implement me!" );
41+
default:
42+
throw new UnsupportedOperationException( "Unknown AccessType: " + accessType.name() );
43+
}
44+
}
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import javax.cache.Cache;
10+
11+
import org.hibernate.cache.spi.QueryResultsRegion;
12+
13+
/**
14+
* @author Alex Snaps
15+
*/
16+
public class JCacheQueryResultsRegion extends JCacheGeneralDataRegion implements QueryResultsRegion {
17+
18+
public JCacheQueryResultsRegion(Cache<Object, Object> cache) {
19+
super( cache );
20+
}
21+
22+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.jcache;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import javax.cache.Cache;
12+
13+
import org.hibernate.cache.CacheException;
14+
import org.hibernate.cache.spi.Region;
15+
16+
/**
17+
* @author Alex Snaps
18+
*/
19+
public class JCacheRegion implements Region {
20+
21+
protected final Cache<Object, Object> cache;
22+
23+
public JCacheRegion(Cache<Object, Object> cache) {
24+
if(cache == null) {
25+
throw new NullPointerException("JCacheRegion requires a Cache!");
26+
}
27+
this.cache = cache;
28+
}
29+
30+
public String getName() {
31+
return cache.getName();
32+
}
33+
34+
public void destroy() throws CacheException {
35+
cache.getCacheManager().destroyCache( cache.getName() );
36+
}
37+
38+
public boolean contains(Object key) {
39+
return cache.containsKey( key );
40+
}
41+
42+
public long getSizeInMemory() {
43+
return -1;
44+
}
45+
46+
public long getElementCountInMemory() {
47+
return -1;
48+
}
49+
50+
public long getElementCountOnDisk() {
51+
return -1;
52+
}
53+
54+
public Map toMap() {
55+
final Map<Object, Object> map = new HashMap<Object, Object>();
56+
for ( Cache.Entry<Object, Object> entry : cache ) {
57+
map.put( entry.getKey(), entry.getValue() );
58+
}
59+
return map;
60+
}
61+
62+
public long nextTimestamp() {
63+
return JCacheRegionFactory.nextTS();
64+
}
65+
66+
public int getTimeout() {
67+
return JCacheRegionFactory.timeOut();
68+
}
69+
70+
Cache<Object, Object> getCache() {
71+
return cache;
72+
}
73+
}

0 commit comments

Comments
 (0)