Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Govind Balaji S committed Oct 14, 2022
1 parent b0e7c82 commit 3ba2146
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class SiteStatsDaoRedisImpl implements SiteStatsDao {

private final int weekSeconds = 60 * 60 * 24 * 7;
private static final int weekSeconds = 60 * 60 * 24 * 7;
private final JedisPool jedisPool;
private final CompareAndUpdateScript compareAndUpdateScript;

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -81,6 +81,16 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) {
// Challenge #3
private void updateOptimized(Jedis jedis, String key, MeterReading reading) {
// START Challenge #3
Transaction transaction = jedis.multi();
String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString();
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();
// END Challenge #3
}

Expand Down

0 comments on commit 3ba2146

Please sign in to comment.