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 01bc8e9..abb18a3 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java @@ -51,6 +51,7 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni String metricKey = RedisSchema.getDayMetricKey(siteId, unit, dateTime); Integer minuteOfDay = getMinuteOfDay(dateTime); jedis.zadd(metricKey, minuteOfDay, new MeasurementMinute(value, minuteOfDay).toString()); + jedis.expire(metricKey, METRIC_EXPIRATION_SECONDS); } /** 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 1da030f..4ea8671 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java @@ -47,7 +47,7 @@ public void update(MeterReading reading) { ZonedDateTime day = reading.getDateTime(); String key = RedisSchema.getSiteStatsKey(siteId, day); - updateBasic(jedis, key, reading); + updateOptimized(jedis, key, reading); } } @@ -80,8 +80,19 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) { // Challenge #3 private void updateOptimized(Jedis jedis, String key, MeterReading reading) { - // START Challenge #3 - // END Challenge #3 + String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); + try(Transaction transaction = jedis.multi()) { + 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(); + } + } private Double getCurrentCapacity(MeterReading reading) {