Ensure that the session cache is only used on entity objects marked @Cacheable.

This commit is contained in:
Greg Burd 2017-10-23 14:40:00 -04:00
parent 852ee59da2
commit d25061366b
12 changed files with 47 additions and 18 deletions

View file

@ -213,8 +213,10 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
}); });
// log.record(txn::abort) // log.record(txn::abort)
// cache.invalidateSince(txn::start time) // cache.invalidateSince(txn::start time)
elapsedTime_.stop(); if (!hasAborted()) {
logTimers("aborted"); elapsedTime_.stop();
logTimers("aborted");
}
} }
private void mergeCache(Table<String, String, Object> from) { private void mergeCache(Table<String, String, Object> from) {

View file

@ -72,9 +72,9 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
try { try {
Optional<E> result = Optional.empty(); Optional<E> result = Optional.empty();
E cacheResult = null; E cacheResult = null;
boolean updateCache = true; boolean updateCache = isSessionCacheable();
if (enableCache) { if (enableCache && isSessionCacheable()) {
List<Facet> facets = bindFacetValues(); List<Facet> facets = bindFacetValues();
String tableName = CacheUtil.schemaName(facets); String tableName = CacheUtil.schemaName(facets);
cacheResult = (E)sessionOps.checkCache(tableName, facets); cacheResult = (E)sessionOps.checkCache(tableName, facets);
@ -95,7 +95,10 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
} }
if (updateCache && result.isPresent()) { if (updateCache && result.isPresent()) {
sessionOps.updateCache(result.get(), getFacets()); List<Facet> facets = getFacets();
if (facets != null && facets.size() > 1) {
sessionOps.updateCache(result.get(), facets);
}
} }
return result; return result;
} finally { } finally {
@ -123,10 +126,12 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
result = Optional.of(cacheResult); result = Optional.of(cacheResult);
updateCache = false; updateCache = false;
} else { } else {
String tableName = CacheUtil.schemaName(facets); if (isSessionCacheable()) {
cacheResult = (E)sessionOps.checkCache(tableName, facets); String tableName = CacheUtil.schemaName(facets);
if (cacheResult != null) { cacheResult = (E) sessionOps.checkCache(tableName, facets);
result = Optional.of(cacheResult); if (cacheResult != null) {
result = Optional.of(cacheResult);
}
} }
} }
timer.stop(); timer.stop();

View file

@ -66,9 +66,9 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
try { try {
Stream<E> resultStream = null; Stream<E> resultStream = null;
E cacheResult = null; E cacheResult = null;
boolean updateCache = true; boolean updateCache = isSessionCacheable();
if (enableCache) { if (enableCache && isSessionCacheable()) {
List<Facet> facets = bindFacetValues(); List<Facet> facets = bindFacetValues();
String tableName = CacheUtil.schemaName(facets); String tableName = CacheUtil.schemaName(facets);
cacheResult = (E) sessionOps.checkCache(tableName, facets); cacheResult = (E) sessionOps.checkCache(tableName, facets);
@ -89,13 +89,15 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
} }
if (updateCache && resultStream != null) { if (updateCache && resultStream != null) {
List<E> again = new ArrayList<>();
List<Facet> facets = getFacets(); List<Facet> facets = getFacets();
resultStream.forEach(result -> { if (facets != null && facets.size() > 1) {
sessionOps.updateCache(result, facets); List<E> again = new ArrayList<>();
again.add(result); resultStream.forEach(result -> {
}); sessionOps.updateCache(result, facets);
resultStream = again.stream(); again.add(result);
});
resultStream = again.stream();
}
} }
return resultStream; return resultStream;

View file

@ -39,4 +39,7 @@ public final class BoundOperation<E> extends AbstractOperation<E, BoundOperation
public Statement buildStatement(boolean cached) { public Statement buildStatement(boolean cached) {
return boundStatement; return boundStatement;
} }
@Override
public boolean isSessionCacheable() { return delegate.isSessionCacheable(); }
} }

View file

@ -41,4 +41,7 @@ public final class BoundOptionalOperation<E> extends AbstractOptionalOperation<E
public Statement buildStatement(boolean cached) { public Statement buildStatement(boolean cached) {
return boundStatement; return boundStatement;
} }
@Override
public boolean isSessionCacheable() { return delegate.isSessionCacheable(); }
} }

View file

@ -49,4 +49,7 @@ public final class BoundStreamOperation<E> extends AbstractStreamOperation<E, Bo
public Statement buildStatement(boolean cached) { public Statement buildStatement(boolean cached) {
return boundStatement; return boundStatement;
} }
@Override
public boolean isSessionCacheable() { return delegate.isSessionCacheable(); }
} }

View file

@ -104,4 +104,6 @@ public abstract class Operation<E> {
return null; return null;
} }
public boolean isSessionCacheable() { return false; }
} }

View file

@ -43,4 +43,5 @@ public final class PreparedOperation<E> {
public String toString() { public String toString() {
return preparedStatement.getQueryString(); return preparedStatement.getQueryString();
} }
} }

View file

@ -59,4 +59,7 @@ public final class SelectFirstOperation<E> extends AbstractFilterOptionalOperati
public Optional<E> transform(ResultSet resultSet) { public Optional<E> transform(ResultSet resultSet) {
return delegate.transform(resultSet).findFirst(); return delegate.transform(resultSet).findFirst();
} }
@Override
public boolean isSessionCacheable() { return delegate.isSessionCacheable(); }
} }

View file

@ -54,4 +54,7 @@ public final class SelectFirstTransformingOperation<R, E>
public Optional<R> transform(ResultSet resultSet) { public Optional<R> transform(ResultSet resultSet) {
return delegate.transform(resultSet).findFirst().map(fn); return delegate.transform(resultSet).findFirst().map(fn);
} }
@Override
public boolean isSessionCacheable() { return delegate.isSessionCacheable(); }
} }

View file

@ -185,6 +185,9 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
return this; return this;
} }
@Override
public boolean isSessionCacheable() { return isCacheable; }
@Override @Override
public List<Facet> getFacets() { public List<Facet> getFacets() {
HelenusEntity entity = props.get(0).getEntity(); HelenusEntity entity = props.get(0).getEntity();

View file

@ -21,7 +21,6 @@ import net.helenus.mapping.annotation.PartitionKey;
import net.helenus.mapping.annotation.Table; import net.helenus.mapping.annotation.Table;
@Table("simple_users") @Table("simple_users")
@Cacheable
public interface User { public interface User {
@PartitionKey @PartitionKey