File tree 1 file changed +30
-2
lines changed
1 file changed +30
-2
lines changed Original file line number Diff line number Diff line change 19
19
#include < array>
20
20
#include < chrono>
21
21
#include < exception>
22
+ #include < shared_mutex>
22
23
#include < string_view>
23
24
24
25
#include < boost/asio/buffer.hpp>
@@ -441,11 +442,38 @@ bool Connection::is_method_allowed(boost::beast::http::verb method) {
441
442
442
443
std::string Connection::get_date_time () {
443
444
static const absl::TimeZone kTz {absl::LocalTimeZone ()};
444
- const absl::Time now{absl::Now ()};
445
+ static std::pair<int64_t , std::string> cache;
446
+ static std::shared_mutex cache_mutex;
447
+
448
+ // read cache
449
+ std::pair<int64_t , std::string> result;
450
+ {
451
+ std::shared_lock lock{cache_mutex};
452
+ result = cache;
453
+ }
454
+
455
+ const int64_t ts = absl::ToUnixSeconds (absl::Now ());
456
+
457
+ // if timestamp matches - return the cached result
458
+ if (ts == result.first ) {
459
+ return std::move (result.second );
460
+ }
445
461
462
+ // otherwise - format
463
+ const absl::Time now = absl::FromUnixSeconds (ts);
446
464
std::stringstream ss;
447
465
ss << absl::FormatTime (" %a, %d %b %E4Y %H:%M:%S " , now, kTz ) << kTz .name ();
448
- return ss.str ();
466
+ result = {ts, ss.str ()};
467
+
468
+ // update cache if timestamp increased
469
+ {
470
+ std::unique_lock lock{cache_mutex};
471
+ if (ts > cache.first ) {
472
+ cache = result;
473
+ }
474
+ }
475
+
476
+ return std::move (result.second );
449
477
}
450
478
451
479
Task<void > Connection::compress (const std::string& clear_data, std::string& compressed_data) {
You can’t perform that action at this time.
0 commit comments