Skip to content

Commit

Permalink
Adds Challenge redislabs-training#3 solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Prickett committed May 30, 2020
1 parent 0c9153c commit 955f23e
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void update(MeterReading reading) {
ZonedDateTime day = reading.getDateTime();
String key = RedisSchema.getSiteStatsKey(siteId, day);

updateBasic(jedis, key, reading);
//updateBasic(jedis, key, reading);
updateOptimized(jedis, key, reading);
}
}

Expand Down Expand Up @@ -81,6 +82,20 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) {
// Challenge #3
private void updateOptimized(Jedis jedis, String key, MeterReading reading) {
// START Challenge #3
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);
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();
}
// END Challenge #3
}

Expand Down

0 comments on commit 955f23e

Please sign in to comment.