下面列出了怎么用javax.persistence.CacheRetrieveMode的API类实例代码及写法,或者点击链接到github查看源代码。
private CacheMode determineAppropriateLocalCacheMode(Map<String, Object> localProperties) {
CacheRetrieveMode retrieveMode = null;
CacheStoreMode storeMode = null;
if ( localProperties != null ) {
retrieveMode = determineCacheRetrieveMode( localProperties );
storeMode = determineCacheStoreMode( localProperties );
}
if ( retrieveMode == null ) {
// use the EM setting
retrieveMode = determineCacheRetrieveMode( this.properties );
}
if ( storeMode == null ) {
// use the EM setting
storeMode = determineCacheStoreMode( this.properties );
}
return CacheModeHelper.interpretCacheMode( storeMode, retrieveMode );
}
/**
* Given a JPA {@link CacheStoreMode} and {@link CacheRetrieveMode}, determine the corresponding
* legacy Hibernate {@link CacheMode}.
*
* @param storeMode The JPA shared-cache store mode.
* @param retrieveMode The JPA shared-cache retrieve mode.
*
* @return Corresponding {@link CacheMode}.
*/
public static CacheMode interpretCacheMode(CacheStoreMode storeMode, CacheRetrieveMode retrieveMode) {
if ( storeMode == null ) {
storeMode = DEFAULT_STORE_MODE;
}
if ( retrieveMode == null ) {
retrieveMode = DEFAULT_RETRIEVE_MODE;
}
final boolean get = ( CacheRetrieveMode.USE == retrieveMode );
switch ( storeMode ) {
case USE: {
return get ? CacheMode.NORMAL : CacheMode.PUT;
}
case REFRESH: {
// really (get == true) here is a bit of an invalid combo...
return CacheMode.REFRESH;
}
case BYPASS: {
return get ? CacheMode.GET : CacheMode.IGNORE;
}
default: {
throw new IllegalStateException( "huh? :)" );
}
}
}
/**
* Given a JPA {@link CacheStoreMode} and {@link CacheRetrieveMode}, determine the corresponding
* legacy Hibernate {@link CacheMode}.
*
* @param storeMode The JPA shared-cache store mode.
* @param retrieveMode The JPA shared-cache retrieve mode.
*
* @return Corresponding {@link CacheMode}.
*/
public static CacheMode effectiveCacheMode(CacheStoreMode storeMode, CacheRetrieveMode retrieveMode) {
if ( storeMode == null && retrieveMode == null ) {
return null;
}
if ( storeMode == null ) {
storeMode = DEFAULT_STORE_MODE;
}
if ( retrieveMode == null ) {
retrieveMode = DEFAULT_RETRIEVE_MODE;
}
final boolean get = ( CacheRetrieveMode.USE == retrieveMode );
switch ( storeMode ) {
case USE: {
return get ? CacheMode.NORMAL : CacheMode.PUT;
}
case REFRESH: {
// really (get == true) here is a bit of an invalid combo...
return CacheMode.REFRESH;
}
case BYPASS: {
return get ? CacheMode.GET : CacheMode.IGNORE;
}
default: {
throw new IllegalStateException( "huh? :)" );
}
}
}
public static CacheRetrieveMode interpretCacheRetrieveMode(CacheMode cacheMode) {
if ( cacheMode == null ) {
cacheMode = DEFAULT_LEGACY_MODE;
}
return ( CacheMode.NORMAL == cacheMode || CacheMode.GET == cacheMode )
? CacheRetrieveMode.USE
: CacheRetrieveMode.BYPASS;
}
protected boolean applyJpaCacheRetrieveMode(CacheRetrieveMode mode) {
this.cacheRetrieveMode = mode;
return true;
}
private CacheRetrieveMode currentCacheRetrieveMode() {
return determineCacheRetrieveMode( properties );
}
private CacheRetrieveMode determineCacheRetrieveMode(Map<String, Object> settings) {
return ( CacheRetrieveMode ) settings.get( JPA_SHARED_CACHE_RETRIEVE_MODE );
}
@Before
public void configureEntityManager() {
// These are defaults but put here for documentation purposes
manager.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.USE);
manager.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
}
@Before
public void configureEntityManager() {
// These are defaults but put here for documentation purposes
manager.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.USE);
manager.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
}
@Before
public void configureEntityManager() {
// These are defaults but put here for documentation purposes
manager.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.USE);
manager.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
}
private CacheConfig(CacheRetrieveMode retrieveMode, CacheStoreMode storeMode) {
this.retrieveMode = retrieveMode;
this.storeMode = storeMode;
}
public CacheRetrieveMode getRetrieveMode() {
return retrieveMode;
}
/**
* get entities from cache
*/
public static CacheConfig get() {
return new CacheConfig(CacheRetrieveMode.USE, CacheStoreMode.USE);
}
/**
* get entities from db and refresh cache
*/
public static CacheConfig refresh() {
return new CacheConfig(CacheRetrieveMode.USE, CacheStoreMode.REFRESH);
}
/**
* bypass cache
*/
public static CacheConfig bypass() {
return new CacheConfig(CacheRetrieveMode.BYPASS, CacheStoreMode.BYPASS);
}
public static void cacheable(Query query) {
query.setHint(CACHEBLE, true);
query.setHint(RETRIEVE_MODE, CacheRetrieveMode.USE);
query.setHint(STORE_MODE, CacheStoreMode.USE);
}