diff --git a/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/query/cache/StatisticCacheHolder.java b/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/query/cache/StatisticCacheHolder.java index caea5a0..4d83fb4 100644 --- a/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/query/cache/StatisticCacheHolder.java +++ b/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/query/cache/StatisticCacheHolder.java @@ -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 implements Serializable { private final List cacheValues; - private final String uuid; + private final Map uuids = new HashMap<>(); - public StatisticCacheHolder(List cacheValues, String uuid) { + public StatisticCacheHolder(List cacheValues, Map uuids) { this.cacheValues = cacheValues; - this.uuid = uuid; + this.uuids.putAll(uuids); } public List 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) { diff --git a/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/statistic/QueryStatisticImpl.java b/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/statistic/QueryStatisticImpl.java index 2e930ea..fc1523b 100644 --- a/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/statistic/QueryStatisticImpl.java +++ b/dynamic-orm-core/src/main/java/com/github/vzakharchenko/dynamic/orm/core/statistic/QueryStatisticImpl.java @@ -30,14 +30,27 @@ public List 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 primaryKeys) { - String uuid = UUID.randomUUID().toString(); - getTables().forEach(qTable -> transactionalCache - .putToCache(new StatisticCacheKey(qTable.getTableName()), uuid)); - StatisticCacheHolder statisticCacheHolder = new StatisticCacheHolder(primaryKeys, uuid); + Map 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; } diff --git a/dynamic-orm-core/src/test/java/com/github/vzakharchenko/dynamic/orm/core/ConcurrentSelectCacheTest.java b/dynamic-orm-core/src/test/java/com/github/vzakharchenko/dynamic/orm/core/ConcurrentSelectCacheTest.java index 7b327e2..c1a3444 100644 --- a/dynamic-orm-core/src/test/java/com/github/vzakharchenko/dynamic/orm/core/ConcurrentSelectCacheTest.java +++ b/dynamic-orm-core/src/test/java/com/github/vzakharchenko/dynamic/orm/core/ConcurrentSelectCacheTest.java @@ -91,6 +91,7 @@ public void selectThread12() throws InterruptedException { dynamicTable2.getStringColumnByName("TestColumn").eq( dynamicTable1.getStringColumnByName("TestColumn") ))).findAll(ListUtils.union(dynamicTable1.getColumns(), dynamicTable2.getColumns())); + List tableModels = ormQueryFactory.selectCache().findAll(dynamicTable1); ormQueryFactory.transactionManager().commit(); } Thread.sleep(1000); @@ -101,6 +102,7 @@ public void selectThread12() throws InterruptedException { dynamicTable2.getStringColumnByName("TestColumn").eq( dynamicTable1.getStringColumnByName("TestColumn") ))).findAll(ListUtils.union(dynamicTable1.getColumns(), dynamicTable2.getColumns())); + List tableModels = ormQueryFactory.selectCache().findAll(dynamicTable1); ormQueryFactory.transactionManager().commit(); }