diff --git a/ebean-redis/src/main/java/io/ebean/redis/RedisCache.java b/ebean-redis/src/main/java/io/ebean/redis/RedisCache.java index 0eb7588bd..d9dbbb067 100644 --- a/ebean-redis/src/main/java/io/ebean/redis/RedisCache.java +++ b/ebean-redis/src/main/java/io/ebean/redis/RedisCache.java @@ -8,6 +8,7 @@ import io.ebean.meta.MetricVisitor; import io.ebean.metric.CountMetric; import io.ebean.metric.MetricFactory; import io.ebean.metric.TimedMetric; +import io.ebean.metric.TimedMetricStats; import io.ebean.redis.encode.Encode; import io.ebean.redis.encode.EncodePrefixKey; import org.slf4j.Logger; @@ -188,6 +189,7 @@ final class RedisCache implements ServerCache { multi.set(key(entry.getKey()), value(entry.getValue()), expiration); } } + multi.exec(); } metricPutAll.addSinceNanos(start); } catch (Exception e) { @@ -285,9 +287,13 @@ final class RedisCache implements ServerCache { cacheStats.setCacheName(cacheKey); cacheStats.setHitCount(hitCount.get(reset)); cacheStats.setMissCount(missCount.get(reset)); - cacheStats.setPutCount(metricPut.collect(reset).count()); - cacheStats.setRemoveCount(metricRemove.collect(reset).count()); - cacheStats.setClearCount(metricClear.collect(reset).count()); + cacheStats.setPutCount(count(metricPut.collect(reset))); + cacheStats.setRemoveCount(count(metricRemove.collect(reset))); + cacheStats.setClearCount(count(metricClear.collect(reset))); return cacheStats; } + + private long count(TimedMetricStats stats) { + return stats == null ? 0 : stats.count(); + } } diff --git a/ebean-redis/src/test/java/org/domain/RCust.java b/ebean-redis/src/test/java/org/domain/RCust.java new file mode 100644 index 000000000..8c1d01538 --- /dev/null +++ b/ebean-redis/src/test/java/org/domain/RCust.java @@ -0,0 +1,22 @@ +package org.domain; + + +import io.ebean.annotation.Cache; +import io.ebean.annotation.CacheBeanTuning; +import io.ebean.annotation.Index; + +import javax.persistence.Entity; +import java.time.LocalDate; + +@Cache(naturalKey = "name") +@Entity +public class RCust extends EBase { + + @Index(unique = true) + String name; + + public RCust(String name) { + this.name = name; + } + +} diff --git a/ebean-redis/src/test/java/org/integration/IntegrationTest.java b/ebean-redis/src/test/java/org/integration/IntegrationTest.java index 646229d65..3766a62ab 100644 --- a/ebean-redis/src/test/java/org/integration/IntegrationTest.java +++ b/ebean-redis/src/test/java/org/integration/IntegrationTest.java @@ -1,20 +1,67 @@ package org.integration; import io.ebean.DB; +import io.ebean.cache.ServerCache; +import io.ebean.cache.ServerCacheStatistics; import org.domain.Person; +import org.domain.RCust; import org.domain.query.QPerson; +import org.domain.query.QRCust; import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; -public class IntegrationTest { +class IntegrationTest { @Test - public void test() throws InterruptedException { + void mput() throws InterruptedException { + + ServerCache beanCache = DB.cacheManager().beanCache(RCust.class); + beanCache.clear(); + beanCache.statistics(true); + + List people = new ArrayList<>(); + for (String name : new String[]{"mp0", "mp1", "mp2"}) { + people.add(new RCust(name)); + } + DB.saveAll(people); + List ids = people.stream().map(RCust::getId).collect(Collectors.toList()); + + List f0 = new QRCust() + .setIdIn(ids.toArray()) + .findList(); + + assertThat(f0).hasSize(3); + ServerCacheStatistics stats0 = beanCache.statistics(true); + assertThat(stats0.getHitCount()).isEqualTo(0); + + Thread.sleep(5); + + // we will hit the cache this time + List f1 = new QRCust() + .setIdIn(ids.toArray()) + .findList(); + + assertThat(f1).hasSize(3); + ServerCacheStatistics stats1 = beanCache.statistics(true); + assertThat(stats1.getHitCount()).isEqualTo(3); + + // we will hit the cache again + List f2 = new QRCust() + .setIdIn(ids.toArray()) + .findList(); + assertThat(f2).hasSize(3); + ServerCacheStatistics stats2 = beanCache.statistics(true); + assertThat(stats2.getHitCount()).isEqualTo(3); + } + + @Test + void test() throws InterruptedException { insertSomePeople();