@@ -47,7 +47,7 @@ public void update(MeterReading reading) {
47
47
ZonedDateTime day = reading .getDateTime ();
48
48
String key = RedisSchema .getSiteStatsKey (siteId , day );
49
49
50
- updateBasic (jedis , key , reading );
50
+ updateOptimized (jedis , key , reading );
51
51
}
52
52
}
53
53
@@ -80,8 +80,19 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) {
80
80
81
81
// Challenge #3
82
82
private void updateOptimized (Jedis jedis , String key , MeterReading reading ) {
83
- // START Challenge #3
84
- // END Challenge #3
83
+ String reportingTime = ZonedDateTime .now (ZoneOffset .UTC ).toString ();
84
+ try (Transaction transaction = jedis .multi ()) {
85
+ transaction .hset (key , SiteStats .reportingTimeField , reportingTime );
86
+ transaction .hincrBy (key , SiteStats .countField , 1 );
87
+ transaction .expire (key , weekSeconds );
88
+
89
+ compareAndUpdateScript .updateIfGreater (transaction , key , SiteStats .maxWhField , reading .getWhGenerated ());
90
+ compareAndUpdateScript .updateIfLess (transaction , key , SiteStats .minWhField , reading .getWhGenerated ());
91
+ compareAndUpdateScript .updateIfGreater (transaction , key , SiteStats .maxCapacityField , getCurrentCapacity (reading ));
92
+
93
+ transaction .exec ();
94
+ }
95
+
85
96
}
86
97
87
98
private Double getCurrentCapacity (MeterReading reading ) {
0 commit comments