Skip to content

Commit

Permalink
Merge pull request #28 from vzakharchenko/cache_fix
Browse files Browse the repository at this point in the history
cache multiply queries
  • Loading branch information
vzakharchenko authored Mar 19, 2020
2 parents 3761fcd + 97e5aca commit c1e78b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class StatisticCacheHolder<T extends Serializable> implements Serializable {
private final List<T> cacheValues;
private final String uuid;
private final Map<StatisticCacheKey, String> uuids = new HashMap<>();

public StatisticCacheHolder(List<T> cacheValues, String uuid) {
public StatisticCacheHolder(List<T> cacheValues, Map<StatisticCacheKey, String> uuids) {
this.cacheValues = cacheValues;
this.uuid = uuid;
this.uuids.putAll(uuids);
}

public List<T> getCacheValues() {
return cacheValues;
}

public String getUuid() {
return uuid;
}

public boolean valid(QueryStatistic queryStatistic, TransactionalCache transactionCache) {
return queryStatistic.getTables().stream().allMatch(qTable -> {
StatisticCacheKey key = new StatisticCacheKey(qTable.getTableName());
String uuid = uuids.get(key);
String id = transactionCache.getFromCache(
key, String.class);
if (id == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ public List<RelationalPath> getTables() {
return ImmutableList.copyOf(qTables);
}

private String getUuid(TransactionalCache transactionalCache, StatisticCacheKey key) {
String uuid = transactionalCache.getFromCache(key, String.class);
if (uuid == null) {
uuid = UUID.randomUUID().toString();
}
return uuid;
}

@Override
public StatisticCacheHolder get(TransactionalCache transactionalCache,
String sql,
List<? extends Serializable> primaryKeys) {
String uuid = UUID.randomUUID().toString();
getTables().forEach(qTable -> transactionalCache
.putToCache(new StatisticCacheKey(qTable.getTableName()), uuid));
StatisticCacheHolder statisticCacheHolder = new StatisticCacheHolder(primaryKeys, uuid);
Map<StatisticCacheKey, String> uuids = new HashMap<>();
getTables().forEach(qTable -> {
StatisticCacheKey key = new StatisticCacheKey(qTable.getTableName());
String uuid = getUuid(transactionalCache, key);
transactionalCache
.putToCache(key, uuid);
uuids.put(key, uuid);
});
StatisticCacheHolder statisticCacheHolder = new StatisticCacheHolder(primaryKeys, uuids);
transactionalCache.putToCache(sql, statisticCacheHolder);
return statisticCacheHolder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void selectThread12() throws InterruptedException {
dynamicTable2.getStringColumnByName("TestColumn").eq(
dynamicTable1.getStringColumnByName("TestColumn")
))).findAll(ListUtils.union(dynamicTable1.getColumns(), dynamicTable2.getColumns()));
List<DynamicTableModel> tableModels = ormQueryFactory.selectCache().findAll(dynamicTable1);
ormQueryFactory.transactionManager().commit();
}
Thread.sleep(1000);
Expand All @@ -101,6 +102,7 @@ public void selectThread12() throws InterruptedException {
dynamicTable2.getStringColumnByName("TestColumn").eq(
dynamicTable1.getStringColumnByName("TestColumn")
))).findAll(ListUtils.union(dynamicTable1.getColumns(), dynamicTable2.getColumns()));
List<DynamicTableModel> tableModels = ormQueryFactory.selectCache().findAll(dynamicTable1);
ormQueryFactory.transactionManager().commit();

}
Expand Down

0 comments on commit c1e78b5

Please sign in to comment.