#1222 - Extend L2 ServerCache API with putAll(), getAll() and removeAll() methods

This commit is contained in:
Rob Bygrave
2017-12-13 19:58:28 +13:00
parent c7f4b56dd1
commit 4d0b29624e
7 changed files with 100 additions and 33 deletions
@@ -33,6 +33,8 @@ import io.ebean.plugin.BeanDocType;
import io.ebean.plugin.BeanType;
import io.ebean.plugin.ExpressionPath;
import io.ebean.plugin.Property;
import io.ebean.util.SplitName;
import io.ebeaninternal.api.BeanCacheResult;
import io.ebeaninternal.api.CQueryPlanKey;
import io.ebeaninternal.api.ConcurrencyMode;
import io.ebeaninternal.api.LoadContext;
@@ -62,8 +64,6 @@ import io.ebeaninternal.server.el.ElPropertyValue;
import io.ebeaninternal.server.persist.DmlUtil;
import io.ebeaninternal.server.query.CQueryPlan;
import io.ebeaninternal.server.query.CQueryPlanStats.Snapshot;
import io.ebean.util.SplitName;
import io.ebeaninternal.api.BeanCacheResult;
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
import io.ebeaninternal.server.rawsql.SpiRawSql;
import io.ebeaninternal.server.text.json.ReadJson;
@@ -1348,6 +1348,17 @@ public class BeanDescriptor<T> implements MetaBeanInfo, BeanType<T> {
cacheHelp.beanCachePut(bean);
}
@SuppressWarnings("unchecked")
public void cacheBeanPutAll(Collection<?> beans) {
if (!beans.isEmpty()) {
cacheHelp.beanPutAll((Collection<EntityBean>)beans);
}
}
void cacheBeanPutAllDirect(Collection<EntityBean> beans) {
cacheHelp.beanCachePutAllDirect(beans);
}
/**
* Put a bean into the cache as the correct type.
*/
@@ -418,6 +418,22 @@ final class BeanDescriptorCacheHelp<T> {
return CachedBeanDataFromBean.extract(targetDesc, bean);
}
void beanPutAll(Collection<EntityBean> beans) {
if (desc.inheritInfo != null) {
Class<?> aClass = theClassOf(beans);
desc.descOf(aClass).cacheBeanPutAllDirect(beans);
} else {
beanCachePutAllDirect(beans);
}
}
private Class<?> theClassOf(Collection<EntityBean> beans) {
if (beans instanceof List) {
return ((List)beans).get(0).getClass();
}
return beans.iterator().next().getClass();
}
/**
* Put a bean into the bean cache.
*/
@@ -430,9 +446,41 @@ final class BeanDescriptorCacheHelp<T> {
}
}
void beanCachePutAllDirect(Collection<EntityBean> beans) {
Map<Object,Object> natKeys = null;
if (naturalKey != null) {
natKeys = new LinkedHashMap<>();
}
Map<Object,Object> map = new LinkedHashMap<>();
for (EntityBean bean : beans) {
CachedBeanData beanData = beanExtractData(desc, bean);
Object id = desc.getId(bean);
map.put(id, beanData);
if (naturalKey != null) {
Object naturalKey = calculateNaturalKey(beanData);
if (naturalKey != null) {
natKeys.put(naturalKey, id);
}
}
}
if (beanLog.isDebugEnabled()) {
beanLog.debug(" PUT ALL {}({})", cacheName, map.keySet());
}
getBeanCache().putAll(map);
if (natKeys != null && !natKeys.isEmpty()) {
if (natLog.isDebugEnabled()) {
natLog.debug(" PUT ALL {}({}, {})", cacheName, naturalKey, natKeys.keySet());
}
naturalKeyCache.putAll(natKeys);
}
}
/**
* Put the bean into the bean cache.
*/
* Put the bean into the bean cache.
*/
void beanCachePutDirect(EntityBean bean) {
CachedBeanData beanData = beanExtractData(desc, bean);
@@ -494,6 +542,9 @@ final class BeanDescriptorCacheHelp<T> {
}
return null;
}
if (beanLog.isTraceEnabled()) {
beanLog.trace(" GET {}({}) - hit", cacheName, id);
}
return convertToBean(id, readOnly, context, data);
}
@@ -558,9 +609,6 @@ final class BeanDescriptorCacheHelp<T> {
ebi.setPersistenceContext(context);
desc.contextPut(context, id, bean);
if (beanLog.isTraceEnabled()) {
beanLog.trace(" GET {}({}) - hit", cacheName, id);
}
if (desc.isReadAuditing()) {
desc.readAuditBean("l2", "", bean);
}