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 = { FakeData = {
data: data:
(Date.new(2024, 1, 1)..Date.new(2024, 12, 31)).map do |date| (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, end,
identifier: "activity-calendar", identifier: "activity-calendar",
} }
@ -33,7 +33,13 @@ module DiscourseRewind
) )
.group("generate_series, user_visits.id") .group("generate_series, user_visits.id")
.order("generate_series") .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" } { data: calendar, identifier: "activity-calendar" }
end end

View File

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

View File

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