Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Cache registrations_for_user (#663)
Browse files Browse the repository at this point in the history
* cache registrations_for_user

* use caching in mine
  • Loading branch information
FinnIckler authored Aug 30, 2024
1 parent 896cd5a commit 149bc12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/controllers/internal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def list_registrations

def registrations_for_user
user_id = params.require(:user_id)
registrations = Registration.where(user_id: user_id).to_a
registrations = Rails.cache.fetch("#{user_id}-registrations-by-user") do
Registration.where(user_id: user_id).to_a
end
render json: registrations
end

Expand Down
8 changes: 7 additions & 1 deletion app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def process_update(update_request)
EmailApi.send_update_email(@competition_id, user_id, status, @current_user)
end

# Invalidate cache
Rails.cache.delete("#{user_id}-registrations-by-user")

{
user_id: updated_registration['user_id'],
guests: updated_registration.guests,
Expand Down Expand Up @@ -178,7 +181,10 @@ def list
end

def mine
my_registrations = Registration.where(user_id: @current_user).map { |x| { competition_id: x.competition_id, status: x.competing_status } }
registrations = Rails.cache.fetch("#{@current_user}-registrations-by-user") do
Registration.where(user_id: @current_user).to_a
end
my_registrations = registrations.map { |x| { competition_id: x.competition_id, status: x.competing_status } }
render json: my_registrations
rescue Dynamoid::Errors::Error => e
# Render an error response
Expand Down
3 changes: 3 additions & 0 deletions app/jobs/registration_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def perform(message)
message[:step_details][:guests],
message[:created_at])
end

# Invalidate Cache
Rails.cache.delete("#{message[:user_id]}-registrations-by-user")
end

private
Expand Down

0 comments on commit 149bc12

Please sign in to comment.