diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java index 2d534e70..8cfd0dd2 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java @@ -108,6 +108,11 @@ public AmazonCloudWatchClient get() { */ private final transient Htpasswd htpasswd; + /** + * Holder of host stats. + */ + private final transient Stats statistics; + /** * Amazon Cloudwatch Client. */ @@ -136,6 +141,7 @@ public AmazonCloudWatchClient get() { this.bucket = bckt; this.htpasswd = new Htpasswd(this); this.cloudwatch = cwatch; + this.statistics = new HostStats(this.bucket.bucket()); } @Override @@ -254,7 +260,7 @@ public String syslog() { @Override public Stats stats() { - return new HostStats(); + return this.statistics; } /** @@ -348,15 +354,23 @@ public String toString() { /** * Stats for this domain. - * - * @todo #173 We should be caching the results of this method for a short - * period somehow, or at least prevent unnecessary repeated requests to - * Amazon CloudWatch API. This is so that we can improve performance and - * reduce access costs. */ @Loggable(Loggable.DEBUG) + @EqualsAndHashCode(of = "bucket") private final class HostStats implements Stats { + /** + * The S3 bucket. + */ + private final transient String bucket; + /** + * Public ctor. + * @param bckt The name of the bucket + */ + public HostStats(final String bckt) { + this.bucket = bckt; + } @Override + @Cacheable(lifetime = Tv.THIRTY, unit = TimeUnit.MINUTES) public long bytesTransferred() { final Date now = new Date(); final List datapoints = @@ -368,7 +382,7 @@ public long bytesTransferred() { .withDimensions( new Dimension() .withName("Bucket") - .withValue(DefaultHost.this.bucket.bucket()) + .withValue(this.bucket) ) .withUnit(StandardUnit.Bytes) .withPeriod((int) TimeUnit.DAYS.toSeconds(Tv.SEVEN)) diff --git a/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultHostTest.java b/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultHostTest.java index 7cc92646..09ede2a6 100644 --- a/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultHostTest.java +++ b/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultHostTest.java @@ -289,10 +289,10 @@ public void showsVersionListingForIndexHtml() throws Exception { } /** - * DefaultHost can retrieve Cloudwatch stats. + * DefaultHost can retrieve Cloudwatch stats and cache the results. */ @Test - public void retrievesCloudWatchStats() { + public void retrievesAndCachesCloudWatchStats() { final long sum = 10; final CloudWatch cloudwatch = this.cloudWatch(); final GetMetricStatisticsResult result = new GetMetricStatisticsResult() @@ -304,7 +304,18 @@ public void retrievesCloudWatchStats() { new BucketMocker().mock(), cloudwatch ).stats().bytesTransferred(), - Matchers.is(sum) + Matchers.allOf( + Matchers.is(sum), + Matchers.is( + new DefaultHost( + new BucketMocker().mock(), + cloudwatch + ).stats().bytesTransferred() + ) + ) + ); + Mockito.verify(cloudwatch.get(), Mockito.times(1)).getMetricStatistics( + Mockito.any(GetMetricStatisticsRequest.class) ); }