#1427 - Map all deleteByIds into a single CacheChangeBeanRemove and use bulk removeAll api to remove from cache

This commit is contained in:
rob bygrave
2018-06-19 01:34:28 +12:00
parent 43e95d81f6
commit 671d082ad3
6 changed files with 62 additions and 31 deletions
@@ -2,6 +2,9 @@ package io.ebeaninternal.server.cache;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.ArrayList;
import java.util.Collection;
/**
* Change to remove bean from L2 cache.
*/
@@ -9,15 +12,35 @@ class CacheChangeBeanRemove implements CacheChange {
private final BeanDescriptor<?> descriptor;
private final Object id;
private final Collection<Object> ids;
CacheChangeBeanRemove(BeanDescriptor<?> descriptor, Object id) {
CacheChangeBeanRemove(Object id, BeanDescriptor<?> descriptor) {
this.descriptor = descriptor;
this.id = id;
this.ids = new ArrayList<>();
ids.add(id);
}
CacheChangeBeanRemove(BeanDescriptor<?> descriptor, Collection<Object> ids) {
this.descriptor = descriptor;
this.ids = ids;
}
@Override
public void apply() {
descriptor.cacheHandleDeleteById(id);
descriptor.cacheApplyInvalidate(ids);
}
/**
* Add more id values.
*/
public void addIds(Collection<Object> moreIds) {
ids.addAll(moreIds);
}
/**
* Add another id value.
*/
public void addId(Object id) {
ids.add(id);
}
}
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.cache;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
@@ -21,6 +22,8 @@ public class CacheChangeSet {
private final Set<BeanDescriptor<?>> queryCaches = new HashSet<>();
private final Map<BeanDescriptor<?>, CacheChangeBeanRemove> beanRemoveMap = new HashMap<>();
private final Map<ManyKey, ManyChange> manyChangeMap = new HashMap<>();
private final long modificationTimestamp;
@@ -54,6 +57,9 @@ public class CacheChangeSet {
for (CacheChange entry : manyChangeMap.values()) {
entry.apply();
}
for (CacheChange entry : beanRemoveMap.values()) {
entry.apply();
}
}
/**
@@ -102,8 +108,26 @@ public class CacheChangeSet {
* Remove a bean from the cache.
*/
public <T> void addBeanRemove(BeanDescriptor<T> desc, Object id) {
touchedTables.add(desc.getBaseTable());
entries.add(new CacheChangeBeanRemove(desc, id));
CacheChangeBeanRemove entry = beanRemoveMap.get(desc);
if (entry != null) {
entry.addId(id);
} else {
beanRemoveMap.put(desc, new CacheChangeBeanRemove(id, desc));
touchedTables.add(desc.getBaseTable());
}
}
/**
* Remove a bean from the cache.
*/
public <T> void addBeanRemoveMany(BeanDescriptor<T> desc, Collection<Object> ids) {
CacheChangeBeanRemove entry = beanRemoveMap.get(desc);
if (entry != null) {
entry.addIds(ids);
} else {
beanRemoveMap.put(desc, new CacheChangeBeanRemove(desc, ids));
touchedTables.add(desc.getBaseTable());
}
}
/**
@@ -1472,15 +1472,15 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
/**
* Remove a bean from the cache given its Id.
*/
public void cacheHandleDeleteById(Object id) {
cacheHelp.beanCacheRemove(id);
public void cacheHandleDeleteByIds(Collection<Object> ids, CacheChangeSet changeSet) {
cacheHelp.handleDeleteIds(ids, changeSet);
}
/**
* Remove a collection of beans from the cache given the ids.
*/
public void cacheHandleInvalidate(Collection<Object> ids) {
cacheHelp.beanCacheInvalidate(ids);
public void cacheApplyInvalidate(Collection<Object> ids) {
cacheHelp.beanCacheApplyInvalidate(ids);
}
/**
@@ -683,7 +683,7 @@ final class BeanDescriptorCacheHelp<T> {
/**
* Remove a bean from the cache given its Id.
*/
void beanCacheInvalidate(Collection<Object> ids) {
void beanCacheApplyInvalidate(Collection<Object> ids) {
if (beanCache != null) {
if (beanLog.isDebugEnabled()) {
beanLog.debug(" REMOVE {}({})", cacheName, ids);
@@ -695,21 +695,6 @@ final class BeanDescriptorCacheHelp<T> {
}
}
/**
* Remove a bean from the cache given its Id.
*/
void beanCacheRemove(Object id) {
if (beanCache != null) {
if (beanLog.isDebugEnabled()) {
beanLog.debug(" REMOVE {}({})", cacheName, id);
}
beanCache.remove(id);
}
for (BeanPropertyAssocOne<?> imported : propertiesOneImported) {
imported.cacheClear();
}
}
/**
* Returns true if it managed to populate/load the bean from the cache.
*/
@@ -740,12 +725,12 @@ final class BeanDescriptorCacheHelp<T> {
/**
* Add appropriate cache changes to support delete by id.
*/
void handleDelete(Object id, CacheChangeSet changeSet) {
void handleDeleteIds(Collection<Object> ids, CacheChangeSet changeSet) {
if (invalidateQueryCache) {
changeSet.addInvalidate(desc);
} else {
if (beanCache != null) {
changeSet.addBeanRemove(desc, id);
changeSet.addBeanRemoveMany(desc, ids);
}
cacheDeleteImported(true, null, changeSet);
}
@@ -131,7 +131,7 @@ public class BeanPersistIds implements BinaryWritable {
// any change invalidates the query cache
beanDescriptor.clearQueryCache();
if (ids != null) {
beanDescriptor.cacheHandleInvalidate(ids);
beanDescriptor.cacheApplyInvalidate(ids);
}
}
}
@@ -29,8 +29,7 @@ public final class DeleteByIdMap {
BeanDescriptor<?> d = deleteIds.getBeanDescriptor();
List<Object> idValues = deleteIds.getIds();
if (idValues != null) {
d.queryCacheClear(changeSet);
d.cacheHandleInvalidate(idValues);
d.cacheHandleDeleteByIds(idValues, changeSet);
}
}
}