Skip to content
Snippets Groups Projects

Improve database response time for listing user activity

Merged Andreas Brandl requested to merge 40525-listing-user-activity-timeouts into master
All threads resolved!
2 files
+ 23
6
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -5,9 +5,25 @@ class UserContributedProjects < ActiveRecord::Base
validates :project, presence: true
validates :user, presence: true
CACHE_EXPIRY_TIME = 1.day
def self.track(event)
find_or_create_by!(project: event.project, user: event.author)
rescue ActiveRecord::RecordNotUnique
retry
attributes = {project_id: event.project_id, user_id: event.author_id}
cached_exists?(attributes) do
begin
find_or_create_by!(attributes)
true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique
retry
end
end
end
private
def self.cached_exists?(project_id:, user_id:, &block)
cache_key = "user_contributed_projects:#{project_id}:#{user_id}"
Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRY_TIME, &block)
end
end
Loading