Skip to content

Commit

Permalink
Challenge redislabs-training#2 & 3 reviewed
Browse files Browse the repository at this point in the history
Added key expiration in Challenge redislabs-training#2.
Added `try-with-resources` and use already initialised script in Chanllenge redislabs-training#3.
  • Loading branch information
J-A-I-L committed Jul 6, 2022
1 parent a262692 commit 837801a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 837801a

Please sign in to comment.