Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ndy2 committed May 26, 2023
1 parent 04f5ef4 commit 52755f0
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

public class SiteStatsDaoRedisImpl implements SiteStatsDao {

Expand Down Expand Up @@ -47,7 +48,7 @@ public void update(MeterReading reading) {
ZonedDateTime day = reading.getDateTime();
String key = RedisSchema.getSiteStatsKey(siteId, day);

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

Expand Down Expand Up @@ -81,6 +82,18 @@ 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);

compareAndUpdateScript.updateIfGreater(t, key, SiteStats.maxWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfLess(t, key, SiteStats.minWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfGreater(t, key, SiteStats.maxCapacityField, getCurrentCapacity(reading));

t.exec();
}
// END Challenge #3
}

Expand Down

0 comments on commit 52755f0

Please sign in to comment.