mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-26 07:20:56 +00:00
#1679 - ENH: Support enabling/disabling L2 cache by "region" at runtime
This commit is contained in:
1 parent
410d1a9b37
commit
ae228d6b0e
17 files changed
+451
-42
No files matched your search
@@ -7,6 +7,8 @@ import io.ebean.bean.PersistenceContext;
|
||||
import io.ebean.cache.QueryCacheEntry;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebeaninternal.api.BeanCacheResult;
|
||||
import io.ebeaninternal.api.SpiCacheControl;
|
||||
import io.ebeaninternal.api.SpiCacheRegion;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.api.TransactionEventTable.TableIUD;
|
||||
import io.ebeaninternal.server.cache.CacheChangeSet;
|
||||
@@ -72,6 +74,9 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
|
||||
private final boolean noCaching;
|
||||
|
||||
private final SpiCacheControl cacheControl;
|
||||
private final SpiCacheRegion cacheRegion;
|
||||
|
||||
/**
|
||||
* Set to true if all persist changes need to notify the cache.
|
||||
*/
|
||||
@@ -113,6 +118,13 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
this.naturalKeyCache = null;
|
||||
}
|
||||
this.noCaching = (beanCache == null && queryCache == null);
|
||||
if (noCaching) {
|
||||
this.cacheControl = DCacheControlNone.INSTANCE;
|
||||
this.cacheRegion = (invalidateQueryCache) ? cacheManager.getRegion(cacheOptions.getRegion()) : DCacheRegionNone.INSTANCE;
|
||||
} else {
|
||||
this.cacheRegion = cacheManager.getRegion(cacheOptions.getRegion());
|
||||
this.cacheControl = new DCacheControl(cacheRegion, (beanCache != null), (naturalKeyCache != null), (queryCache != null));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,8 +135,8 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
cacheNotifyOnDelete = !cacheNotifyOnAll && isNotifyOnDeletes();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
if (isBeanCaching() || isQueryCaching() || cacheNotifyOnAll || cacheNotifyOnDelete) {
|
||||
String notifyMode = cacheNotifyOnAll ? "All" : (cacheNotifyOnDelete ? "Delete" : "None");
|
||||
if (cacheNotifyOnAll || cacheNotifyOnDelete) {
|
||||
String notifyMode = cacheNotifyOnAll ? "All" : "Delete";
|
||||
logger.debug("l2 caching on {} - beanCaching:{} queryCaching:{} notifyMode:{} ",
|
||||
desc.getFullName(), isBeanCaching(), isQueryCaching(), notifyMode);
|
||||
}
|
||||
@@ -148,36 +160,36 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
* Return true if the persist request needs to notify the cache.
|
||||
*/
|
||||
boolean isCacheNotify(PersistRequest.Type type) {
|
||||
return cacheNotifyOnAll
|
||||
|| cacheNotifyOnDelete && (type == PersistRequest.Type.DELETE || type == PersistRequest.Type.DELETE_PERMANENT);
|
||||
return cacheRegion.isEnabled()
|
||||
&& (cacheNotifyOnAll || cacheNotifyOnDelete && (type == PersistRequest.Type.DELETE || type == PersistRequest.Type.DELETE_PERMANENT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is currently query caching for this type of bean.
|
||||
*/
|
||||
boolean isQueryCaching() {
|
||||
return queryCache != null;
|
||||
return cacheControl.isQueryCaching();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is currently bean caching for this type of bean.
|
||||
*/
|
||||
boolean isBeanCaching() {
|
||||
return beanCache != null;
|
||||
return cacheControl.isBeanCaching();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is natural key caching for this type of bean.
|
||||
*/
|
||||
boolean isNaturalKeyCaching() {
|
||||
return naturalKeyCache != null;
|
||||
return cacheControl.isNaturalKeyCaching();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is bean or query caching on this type.
|
||||
*/
|
||||
boolean isCaching() {
|
||||
return beanCache != null || queryCache != null;
|
||||
return cacheControl.isCaching();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,7 +218,7 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
/**
|
||||
* Add query cache clear to the changeSet.
|
||||
*/
|
||||
void queryCacheClear(CacheChangeSet changeSet) {
|
||||
private void queryCacheClear(CacheChangeSet changeSet) {
|
||||
if (queryCache != null) {
|
||||
changeSet.addClearQuery(desc);
|
||||
}
|
||||
@@ -292,8 +304,8 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
Object ownerBean = bc.getOwnerBean();
|
||||
EntityBeanIntercept ebi = ((EntityBean) ownerBean)._ebean_getIntercept();
|
||||
EntityBean ownerBean = bc.getOwnerBean();
|
||||
EntityBeanIntercept ebi = ownerBean._ebean_getIntercept();
|
||||
PersistenceContext persistenceContext = ebi.getPersistenceContext();
|
||||
|
||||
BeanDescriptor<?> targetDescriptor = many.getTargetDescriptor();
|
||||
@@ -362,6 +374,28 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
return new CachedManyIds(idList);
|
||||
}
|
||||
|
||||
// BeanCacheResult<T> cacheIdLookup(PersistenceContext context, Collection<?> keys) {
|
||||
//
|
||||
// Set<Object> ids = new HashSet<>(keys);
|
||||
// Map<Object, Object> beanDataMap = beanCache.getAll(ids);
|
||||
// if (beanLog.isTraceEnabled()) {
|
||||
// beanLog.trace(" GET MANY {}({}) - hits:{}", cacheName, ids, beanDataMap.keySet());
|
||||
// }
|
||||
//
|
||||
// BeanCacheResult<T> result = new BeanCacheResult<>();
|
||||
// // process the hits into beans etc
|
||||
// for (Map.Entry<Object, Object> entry : beanDataMap.entrySet()) {
|
||||
//
|
||||
// Object id = entry.getKey();
|
||||
// CachedBeanData cachedBeanData = (CachedBeanData) entry.getValue();
|
||||
//
|
||||
// T bean = convertToBean(id, false, context, cachedBeanData);
|
||||
// result.add(bean, id);
|
||||
// }
|
||||
//
|
||||
// return result;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Use natural keys to hit the bean cache and return resulting hits.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeaninternal.api.SpiCacheControl;
|
||||
import io.ebeaninternal.api.SpiCacheRegion;
|
||||
|
||||
/**
|
||||
* Default implementation of SpiCacheControl.
|
||||
*/
|
||||
class DCacheControl implements SpiCacheControl {
|
||||
|
||||
private final SpiCacheRegion region;
|
||||
private final boolean bean;
|
||||
private final boolean naturalKey;
|
||||
private final boolean query;
|
||||
|
||||
DCacheControl(SpiCacheRegion region, boolean bean, boolean naturalKey, boolean query) {
|
||||
this.region = region;
|
||||
this.bean = bean;
|
||||
this.naturalKey = naturalKey;
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaching() {
|
||||
return (bean || query) && region.isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBeanCaching() {
|
||||
return bean && region.isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalKeyCaching() {
|
||||
return naturalKey && region.isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isQueryCaching() {
|
||||
return query && region.isEnabled();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeaninternal.api.SpiCacheControl;
|
||||
|
||||
/**
|
||||
* Cache control used when no caching is on the bean type.
|
||||
*/
|
||||
class DCacheControlNone implements SpiCacheControl {
|
||||
|
||||
static final SpiCacheControl INSTANCE = new DCacheControlNone();
|
||||
|
||||
@Override
|
||||
public boolean isCaching() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBeanCaching() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalKeyCaching() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isQueryCaching() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeaninternal.api.SpiCacheRegion;
|
||||
|
||||
public class DCacheRegion implements SpiCacheRegion {
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
private final String name;
|
||||
|
||||
public DCacheRegion(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeaninternal.api.SpiCacheRegion;
|
||||
|
||||
class DCacheRegionNone implements SpiCacheRegion {
|
||||
|
||||
static SpiCacheRegion INSTANCE = new DCacheRegionNone();
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "<none>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
throw new IllegalStateException("Not expected");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user