io.ebeaninternal.server.cache.CacheChangeSet Maven / Gradle / Ivy
Show all versions of ebean Show documentation
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;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* List of changes to be applied to L2 cache.
*/
public class CacheChangeSet {
private final List entries = new ArrayList<>();
private final Set touchedTables = new HashSet<>();
private final Set> queryCaches = new HashSet<>();
private final Set> beanCaches = new HashSet<>();
private final Map, CacheChangeBeanRemove> beanRemoveMap = new HashMap<>();
private final Map manyChangeMap = new HashMap<>();
/**
* Construct specifying if we also need to process invalidation for entities based on views.
*/
public CacheChangeSet() {
}
/**
* Return the touched tables.
*/
public Set touchedTables() {
return touchedTables;
}
/**
* Apply the changes to the L2 cache except entity/view invalidation.
*
* Return the set of table changes to process invalidation for entities based on views.
*/
public void apply() {
for (BeanDescriptor> entry : queryCaches) {
entry.clearQueryCache();
}
for (BeanDescriptor> entry : beanCaches) {
entry.clearBeanCache();
}
for (CacheChange entry : entries) {
entry.apply();
}
for (CacheChange entry : manyChangeMap.values()) {
entry.apply();
}
for (CacheChange entry : beanRemoveMap.values()) {
entry.apply();
}
}
/**
* Add an entry to clear a query cache.
*/
public void addInvalidate(BeanDescriptor> descriptor) {
touchedTables.add(descriptor.getBaseTable());
}
/**
* Add invalidation on a set of tables.
*/
public void addInvalidate(Set tables) {
touchedTables.addAll(tables);
}
/**
* Add an entry to clear a query cache.
*/
public void addClearQuery(BeanDescriptor> descriptor) {
queryCaches.add(descriptor);
touchedTables.add(descriptor.getBaseTable());
}
/**
* Add an entry to clear a bean cache.
*/
public void addClearBean(BeanDescriptor> descriptor) {
beanCaches.add(descriptor);
}
/**
* Add many property clear.
*/
public void addManyClear(BeanDescriptor desc, String manyProperty) {
many(desc, manyProperty).setClear();
}
/**
* Add many property remove.
*/
public void addManyRemove(BeanDescriptor desc, String manyProperty, Object parentId) {
many(desc, manyProperty).addRemove(parentId);
}
/**
* Add many property put.
*/
public void addManyPut(BeanDescriptor desc, String manyProperty, Object parentId, CachedManyIds entry) {
many(desc, manyProperty).addPut(parentId, entry);
}
/**
* On bean insert register table for view based entity invalidation.
*/
public void addBeanInsert(String baseTable) {
touchedTables.add(baseTable);
}
/**
* Remove a bean from the cache.
*/
public void addBeanRemove(BeanDescriptor desc, Object 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 void addBeanRemoveMany(BeanDescriptor desc, Collection