Skip to content

Commit

Permalink
Expire the stats cache an hour after generating it
Browse files Browse the repository at this point in the history
When we first started caching the stats, generating them was a process
that took several minutes, so we never expired the cache.

However, there have been cases where we run into issues where the stats
shown on the screen were outdated. That's why we introduced a task to
manually expire the cache.

But now, generating the stats only takes a few seconds, so we can
automatically expire them every hour, remove all the logic needed to
manually expire them, and get rid of most of the issues related to the
cache being outdated.
  • Loading branch information
javierm committed Apr 18, 2024
1 parent d6b3f98 commit fe7f61b
Show file tree
Hide file tree
Showing 12 changed files with 12 additions and 85 deletions.
1 change: 0 additions & 1 deletion app/models/budget.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Budget < ApplicationRecord
include Measurable
include Sluggable
include StatsVersionable
include Reportable
include Imageable

Expand Down
2 changes: 1 addition & 1 deletion app/models/budget/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ def supports(supportable)
stats_cache(*stats_methods)

def full_cache_key_for(key)
"budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}"
"budgets_stats/#{budget.id}/#{phases.join}/#{key}"
end
end
6 changes: 1 addition & 5 deletions app/models/concerns/statisticable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ def calculate_percentage(fraction, total)
(fraction * 100.0 / total).round(3)
end

def version
"v#{resource.find_or_create_stats_version.updated_at.to_i}"
end

def advanced?
resource.advanced_stats_enabled?
end
Expand Down Expand Up @@ -227,6 +223,6 @@ def range_description(start, finish)
end

def stats_cache(key, &)
Rails.cache.fetch(full_cache_key_for(key), &)
Rails.cache.fetch(full_cache_key_for(key), expires_in: 1.hour, &)
end
end
11 changes: 0 additions & 11 deletions app/models/concerns/stats_versionable.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Poll < ApplicationRecord
include Notifiable
include Searchable
include Sluggable
include StatsVersionable
include Reportable
include SDG::Relatable

Expand Down
2 changes: 1 addition & 1 deletion app/models/poll/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ def total_unregistered_booth
stats_cache(*stats_methods)

def full_cache_key_for(key)
"polls_stats/#{poll.id}/#{key}/#{version}"
"polls_stats/#{poll.id}/#{key}"
end
end
5 changes: 0 additions & 5 deletions app/models/stats_version.rb

This file was deleted.

8 changes: 8 additions & 0 deletions db/migrate/20240408154814_drop_stats_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DropStatsVersions < ActiveRecord::Migration[6.1]
def change
drop_table :stats_versions do |t|
t.references :process, polymorphic: true, index: true
t.timestamps null: false
end
end
end
10 changes: 1 addition & 9 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_03_22_223950) do
ActiveRecord::Schema[7.0].define(version: 2024_04_08_154814) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
Expand Down Expand Up @@ -1516,14 +1516,6 @@
t.string "locale"
end

create_table "stats_versions", id: :serial, force: :cascade do |t|
t.string "process_type"
t.integer "process_id"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["process_type", "process_id"], name: "index_stats_versions_on_process_type_and_process_id"
end

create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
Expand Down
10 changes: 0 additions & 10 deletions lib/tasks/stats.rake

This file was deleted.

28 changes: 0 additions & 28 deletions spec/models/poll/stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,4 @@
end
end
end

describe "#version", :with_frozen_time do
context "record with no stats" do
it "returns a string based on the current time" do
expect(stats.version).to eq "v#{Time.current.to_i}"
end

it "doesn't overwrite the timestamp when called multiple times" do
time = Time.current

expect(stats.version).to eq "v#{time.to_i}"

unfreeze_time

travel_to 2.seconds.from_now do
expect(stats.version).to eq "v#{time.to_i}"
end
end
end

context "record with stats" do
before { poll.create_stats_version(updated_at: 1.day.ago) }

it "returns the version of the existing stats" do
expect(stats.version).to eq "v#{1.day.ago.to_i}"
end
end
end
end
13 changes: 0 additions & 13 deletions spec/models/stats_version_spec.rb

This file was deleted.

0 comments on commit fe7f61b

Please sign in to comment.