Skip to content

Commit

Permalink
Implement backup cache for popular tasks
Browse files Browse the repository at this point in the history
Improve data availability bu having a backup cache if the latest is not
availabile

- Added a backup cache mechanism to ensure data is available even if the
latest cache is expired or unavailable.
- Popular tasks data is now stored in both a latest cache (24 hours
expiration) and a backup cache (7 days expiration).
- Fallback to backup cache when the latest cache is missing, ensuring
users always see data even if fresh data retrieval fails.
- Updated methods to handle cache fallback logic, improving robustness
and reducing the likelihood of empty data responses.
  • Loading branch information
beccapearce committed Sep 6, 2024
1 parent bcebb64 commit b6d0eff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 13 additions & 5 deletions app/helpers/browse_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ def display_popular_tasks_for_slug?(slug)
def popular_links_for_slug(slug)
browse_page = slug(slug)

# Try to fetch the cache first
popular_task_data = Rails.cache.read("popular_tasks_#{browse_page}_#{Date.yesterday.strftime("%Y-%m-%d")}")
# Cache keys for the specific browse page
cache_key_latest = "popular_tasks_#{browse_page}_#{Date.yesterday.strftime("%Y-%m-%d")}"
cache_key_backup = "popular_tasks_backup_#{browse_page}"

# If cache is empty fetch fresh data and cache it
# Try to fetch the latest cache first
popular_task_data = Rails.cache.read(cache_key_latest)

# If the latest cache doesn't exist, fall back to the backup cache
if popular_task_data.nil?
popular_task_data = PopularTasks.new.fetch_data("/browse/#{browse_page}")
# Falling back to backup cache
popular_task_data = Rails.cache.read(cache_key_backup)
end

return [] unless popular_task_data
# If both caches are empty, fetch fresh data and cache it
if popular_task_data.nil?
popular_task_data = PopularTasks.new.fetch_data("/browse/#{browse_page}")
end

popular_task_data
end
Expand Down
12 changes: 9 additions & 3 deletions app/services/popular_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class PopularTasks
CACHE_EXPIRATION = 24.hours # Set the cache expiration time
BACKUP_CACHE_EXPIRATION = 7.days # Backup cache can have a longer expiration

def initialize; end

Expand All @@ -11,10 +12,10 @@ def fetch_data(browse_page, date: Date.yesterday)
@fetch_data = client
@date = date.strftime("%Y-%m-%d")

# Define cache keys for the specific browse page
cache_key = "popular_tasks_#{browse_page}_#{@date}"
cache_key_latest = "popular_tasks_#{browse_page}_#{@date}"
cache_key_backup = "popular_tasks_backup_#{browse_page}"

Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRATION) do
Rails.cache.fetch(cache_key_latest, expires_in: CACHE_EXPIRATION) do
# If cache is empty, this block is executed
query = <<~SQL
WITH cte1 as (SELECT
Expand Down Expand Up @@ -60,6 +61,11 @@ def fetch_data(browse_page, date: Date.yesterday)
}
end
@results.sort_by { |link| link[:rank] } # Order the links by their rank

# Cache the results in the backup cache as well
Rails.cache.write(cache_key_backup, @results, expires_in: BACKUP_CACHE_EXPIRATION)

@results
end
end
end

0 comments on commit b6d0eff

Please sign in to comment.