From 0b9437cee72352068de6338dfc4a22cc68e65560 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 1 Aug 2018 09:45:50 -0400 Subject: [PATCH] FIX: more resilient/consistent dashboard caching (#6223) --- app/jobs/regular/update_disk_space.rb | 4 ++-- app/models/admin_dashboard_next_general_data.rb | 2 +- app/models/report.rb | 4 +--- lib/disk_space.rb | 10 ++++------ 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/jobs/regular/update_disk_space.rb b/app/jobs/regular/update_disk_space.rb index ce544611aca..5ebd3782ad6 100644 --- a/app/jobs/regular/update_disk_space.rb +++ b/app/jobs/regular/update_disk_space.rb @@ -5,8 +5,8 @@ module Jobs sidekiq_options retry: false def execute(args) - $redis.set(DiskSpace::DISK_SPACE_STATS_CACHE_KEY, DiskSpace.stats.to_json) - $redis.set(DiskSpace::DISK_SPACE_STATS_UPDATED_CACHE_KEY, Time.now.to_i) + Discourse.cache.write(DiskSpace::DISK_SPACE_STATS_CACHE_KEY, DiskSpace.stats.to_json) + Discourse.cache.write(DiskSpace::DISK_SPACE_STATS_UPDATED_CACHE_KEY, Time.now.to_i) end end end diff --git a/app/models/admin_dashboard_next_general_data.rb b/app/models/admin_dashboard_next_general_data.rb index 38d80f28e4b..2028392f3f0 100644 --- a/app/models/admin_dashboard_next_general_data.rb +++ b/app/models/admin_dashboard_next_general_data.rb @@ -14,7 +14,7 @@ class AdminDashboardNextGeneralData < AdminDashboardNextData def get_json { - reports: self.class.reports(reports), + reports: self.class.reports(reports).compact, updated_at: Time.zone.now.as_json } end diff --git a/app/models/report.rb b/app/models/report.rb index be51a6fed4c..8a975c51fd4 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -147,12 +147,10 @@ class Report end def self.cache(report, duration) - Discourse.cache.write(Report.cache_key(report), report.as_json, force: true, expires_in: duration) + Discourse.cache.write(cache_key(report), report.as_json, force: true, expires_in: duration) end def self.find(type, opts = nil) - clear_cache - begin report = _get(type, opts) report_method = :"report_#{type}" diff --git a/lib/disk_space.rb b/lib/disk_space.rb index c2f41295143..8c5e385cec6 100644 --- a/lib/disk_space.rb +++ b/lib/disk_space.rb @@ -41,14 +41,13 @@ class DiskSpace end def self.reset_cached_stats - $redis.del(DISK_SPACE_STATS_UPDATED_CACHE_KEY) - $redis.del(DISK_SPACE_STATS_CACHE_KEY) - Jobs.enqueue(:update_disk_space) + Discourse.cache.delete(DISK_SPACE_STATS_UPDATED_CACHE_KEY) + Discourse.cache.delete(DISK_SPACE_STATS_CACHE_KEY) end def self.cached_stats - stats = $redis.get(DISK_SPACE_STATS_CACHE_KEY) - updated_at = $redis.get(DISK_SPACE_STATS_UPDATED_CACHE_KEY) + stats = Discourse.cache.read(DISK_SPACE_STATS_CACHE_KEY) + updated_at = Discourse.cache.read(DISK_SPACE_STATS_UPDATED_CACHE_KEY) unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes Jobs.enqueue(:update_disk_space) @@ -68,5 +67,4 @@ class DiskSpace def self.used(path) `du -s #{path}`.to_i * 1024 end - end