I have a question about getting low level caching to work. I currently need this feature for my web app
- Make it so that the first non-filtered page would only make a single database request per day (24 hours). - not done
So low level caching is probably the way forward for this? Ive tried to implement it based on the documentation, but im struggling a little with the set up. This is my code in my homecontroller currently
class HomeController < ApplicationController
before_action :set_freelancer, only: %i[ show edit update destroy ]
def cache
freelancers_scope = Rails.cache.fetch("freelancers", expires_in: 24.hours) do
Freelancer.order(featured: :desc)
end
index(freelancers_scope)
end
def index
if(freelancers_scope == nil)
cache
else
freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_lower_than]) if params[:cost_greater_than].present?
freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?
@pagy, @freelancers = pagy(freelancers_scope)
end
end
private
def set_freelancer
@freelancer = Freelancer.find(params[:id])
end
end
And this is it without the caching feature being added
lass HomeController < ApplicationController
before_action :set_freelancer, only: %i[ show edit update destroy ]
def index
freelancers_scope = Freelancer.order(featured: :desc)
freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_lower_than]) if params[:cost_greater_than].present?
freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?
@pagy, @freelancers = pagy(freelancers_scope)
end
private
def set_freelancer
@freelancer = Freelancer.find(params[:id])
end
end
How do i cache the result of Freelancer.order(featured: :desc) for 24 hours, and then when its been deleted from the cache, run the query again? Im just getting errors this way currently and its driving me a little mad :)
UPDATE:
class HomeController < ApplicationController
before_action :set_freelancer, only: %i[ show edit update destroy ]
def index
freelancers = Rails.cache.fetch('freelancers', expires_in: 24.hours) do
Freelancer.order(featured: :desc).map(&:attributes)
end
freelancers = freelancers.select do |freelancer|
freelancer['cost'].in?((params[:cost_lower_than].to_i || 0)..params[:cost_greater_than].presence)
end
@pagy, @freelancers = pagy_array(freelancers)
end
private
def set_freelancer
@freelancer = Freelancer.find(params[:id])
end
end
https://ibb.co/mzFXWfd This is the error i get now?