From ae22a0d4443345f8d6a4f390bcf405e1694488ff Mon Sep 17 00:00:00 2001 From: Govind Balaji S Date: Sun, 16 Oct 2022 12:56:26 +0530 Subject: [PATCH] Review Challenge #2, #3 --- .../RU102J/dao/MetricDaoRedisZsetImpl.java | 8 +++++-- .../RU102J/dao/SiteStatsDaoRedisImpl.java | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java index d95aff6..280bd2b 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java @@ -10,7 +10,10 @@ import java.text.DecimalFormat; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; /** * Retain metrics using Redis sorted sets. @@ -50,7 +53,8 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni // START Challenge #2 String metricKey = RedisSchema.getDayMetricKey(siteId, unit, dateTime); Integer minuteOfDay = getMinuteOfDay(dateTime); - jedis.zadd(metricKey, minuteOfDay, value + ":" + minuteOfDay); + jedis.zadd(metricKey, minuteOfDay, new MeasurementMinute(value, minuteOfDay).toString()); + jedis.expire(metricKey, METRIC_EXPIRATION_SECONDS); // END Challenge #2 } diff --git a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java index 14b030e..615487e 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java @@ -81,16 +81,17 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) { // Challenge #3 private void updateOptimized(Jedis jedis, String key, MeterReading reading) { // START Challenge #3 - Transaction transaction = jedis.multi(); - String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); - transaction.hset(key, SiteStats.reportingTimeField, reportingTime); - transaction.hincrBy(key, SiteStats.countField, 1); - transaction.expire(key, weekSeconds); - compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxWhField, reading.getWhGenerated()); - compareAndUpdateScript.updateIfLess(transaction, key, SiteStats.minWhField, reading.getWhGenerated()); - compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxCapacityField, getCurrentCapacity(reading)); - - transaction.exec(); + try (Transaction transaction = jedis.multi()) { + String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); + transaction.hset(key, SiteStats.reportingTimeField, reportingTime); + transaction.hincrBy(key, SiteStats.countField, 1); + transaction.expire(key, weekSeconds); + compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxWhField, reading.getWhGenerated()); + compareAndUpdateScript.updateIfLess(transaction, key, SiteStats.minWhField, reading.getWhGenerated()); + compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxCapacityField, getCurrentCapacity(reading)); + + transaction.exec(); + } // END Challenge #3 }