cache fix

This commit is contained in:
Rafael Silva 2025-01-15 12:06:06 -03:00
parent 19af3cb107
commit 4b1af6f717
No known key found for this signature in database
3 changed files with 40 additions and 34 deletions

View File

@ -7,7 +7,7 @@ module DiscourseRewind
FakeData = {
data:
(Date.new(2024, 1, 1)..Date.new(2024, 12, 31)).map do |date|
{ date:, post_count: rand(0..20), visited: false }
{ date: date.strftime("%Y-%m-%d"), post_count: rand(0..20), visited: false }
end,
identifier: "activity-calendar",
}
@ -33,7 +33,13 @@ module DiscourseRewind
)
.group("generate_series, user_visits.id")
.order("generate_series")
.map { |row| { date: row.date, post_count: row.post_count, visited: row.visited } }
.map do |row|
{
date: row.date.strftime("%Y-%m-%d"),
post_count: row.post_count,
visited: row.visited,
}
end
{ data: calendar, identifier: "activity-calendar" }
end

View File

@ -6,32 +6,32 @@ module DiscourseRewind
FakeData = {
data: {
post_received_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
open_mouth: 2,
cat: 32,
dog: 34,
heart: 45,
grinning: 82,
},
post_used_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
open_mouth: 2,
cat: 32,
dog: 34,
heart: 45,
grinning: 82,
},
chat_used_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
open_mouth: 2,
cat: 32,
dog: 34,
heart: 45,
grinning: 82,
},
chat_received_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
open_mouth: 2,
cat: 32,
dog: 34,
heart: 45,
grinning: 82,
},
},
identifier: "reactions",

View File

@ -51,19 +51,19 @@ module DiscourseRewind
end
def fetch_reports(date:, user:, guardian:, year:)
# key = "rewind:#{guardian.user.username}:#{year}"
# reports = Discourse.redis.get(key)
key = "rewind:#{guardian.user.id}:#{year}"
cached_reports = Discourse.redis.get(key)
# if Rails.env.development? || !reports
reports =
::DiscourseRewind::Rewind::Action::BaseReport
.descendants
.filter { _1.enabled? }
.map { |report| report.call(date:, user:, guardian:) }
# Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
# else
# reports = MultiJson.load(reports.compact, symbolize_keys: true)
# end
if !cached_reports
reports =
::DiscourseRewind::Rewind::Action::BaseReport
.descendants
.filter { _1.enabled? }
.map { |report| report.call(date:, user:, guardian:) }
Discourse.redis.setex(key, CACHE_DURATION, reports.to_json)
else
reports = JSON.parse(cached_reports, symbolize_names: true)
end
reports
end