From 837801a8dc52f46a69b9dd0a18a01b2008060e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20A=2E=20I=2E=20L=C3=B3pez?= <17472377+J-A-I-L@users.noreply.github.com> Date: Wed, 6 Jul 2022 17:42:02 +0200 Subject: [PATCH] Challenge #2 & 3 reviewed Added key expiration in Challenge #2. Added `try-with-resources` and use already initialised script in Chanllenge #3. --- .../RU102J/dao/MetricDaoRedisZsetImpl.java | 2 +- .../RU102J/dao/SiteStatsDaoRedisImpl.java | 21 +++++++++---------- 2 files changed, 11 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 297207e..cb23b3c 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java @@ -53,7 +53,7 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni Integer minuteOfDay = getMinuteOfDay(dateTime); 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 139331c..6bfad41 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java @@ -81,20 +81,19 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) { // Challenge #3 private void updateOptimized(Jedis jedis, String key, MeterReading reading) { // START Challenge #3 - String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); - - Transaction t = jedis.multi(); - CompareAndUpdateScript script = new CompareAndUpdateScript(jedisPool); + try (Transaction t = jedis.multi()) { + String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); - t.hset(key, SiteStats.reportingTimeField, reportingTime); - t.hincrBy(key, SiteStats.countField, 1); - t.expire(key, weekSeconds); + t.hset(key, SiteStats.reportingTimeField, reportingTime); + t.hincrBy(key, SiteStats.countField, 1); + t.expire(key, weekSeconds); - script.updateIfGreater(t, key, SiteStats.maxWhField, reading.getWhGenerated()); - script.updateIfLess(t, key, SiteStats.minWhField, reading.getWhGenerated()); - script.updateIfGreater(t, key, SiteStats.maxCapacityField, getCurrentCapacity(reading)); + this.compareAndUpdateScript.updateIfGreater(t, key, SiteStats.maxWhField, reading.getWhGenerated()); + this.compareAndUpdateScript.updateIfLess(t, key, SiteStats.minWhField, reading.getWhGenerated()); + this.compareAndUpdateScript.updateIfGreater(t, key, SiteStats.maxCapacityField, getCurrentCapacity(reading)); - t.exec(); + t.exec(); + } // END Challenge #3 }