Skip to content

Commit

Permalink
Add fresh_when caching to controller actions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Mar 25, 2024
1 parent 3470793 commit 3035d90
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/exports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ExportsController < ApplicationController
def index
@exports = Export.order("date DESC")
fresh_when @exports, public: true
end
end
4 changes: 2 additions & 2 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class HostsController < ApplicationController
def index
@hosts = Host.all.visible.order('repositories_count DESC, commits_count DESC')

fresh_when @hosts, public: true
@scope = Repository.visible.order('last_synced_at DESC').includes(:host)
@pagy, @repositories = pagy_countless(@scope, items: 10)
end

def show
@host = Host.find_by_name!(params[:id])

fresh_when @host, public: true
scope = @host.repositories.visible

sort = params[:sort].presence || 'last_synced_at'
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def lookup
raise ActiveRecord::RecordNotFound unless @host
path = parsed_url.path.delete_prefix('/').chomp('/')
@repository = @host.repositories.find_by('lower(full_name) = ?', path.downcase)
fresh_when @repository, public: true
if @repository
@repository.sync_async(request.remote_ip) unless @repository.last_synced_at.present? && @repository.last_synced_at > 1.day.ago
redirect_to host_repository_path(@host, @repository)
Expand All @@ -22,6 +23,7 @@ def lookup
def show
@host = Host.find_by_name!(params[:host_id])
@repository = @host.repositories.find_by('lower(full_name) = ?', params[:id].downcase)
fresh_when @repository, public: true
if @repository.nil?
@job = @host.sync_repository_async(params[:id], request.remote_ip)
@repository = @host.repositories.find_by('lower(full_name) = ?', params[:id].downcase)
Expand Down

0 comments on commit 3035d90

Please sign in to comment.