Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 906aa52

Browse files
committed
added caching for fetching challenge
1 parent 444aca5 commit 906aa52

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

app/controllers/challenges_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def comment
338338
private
339339

340340
def current_challenge
341-
@current_challenge ||= Challenge.find params[:id]
341+
@current_challenge ||= Challenge.find params[:id], current_user
342342
end
343343

344344
def throw_404_for_draft_challenge

app/models/challenge.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ def initialize(params={})
5151
super(params)
5252
end
5353

54+
# for challenge, cache it for 5 minutes
55+
def self.find(entity, current_user)
56+
if current_user
57+
puts "[CHALLENGE][CACHE] ======== calling find challenge"
58+
Rails.cache.fetch("#{self.api_endpoint}/#{entity}-#{current_user.username}", :expires_in => ENV['MEMCACHE_CHALLENGE_EXPIRY'].to_i.minute) do
59+
puts "[CHALLENGE][CACHE] ======== making call to #{self.api_endpoint}/#{entity}-#{current_user.username}"
60+
super entity
61+
end
62+
else
63+
super entity
64+
end
65+
end
66+
5467
def self.api_endpoint
5568
"challenges"
5669
end

0 commit comments

Comments
 (0)