mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add job to cache products JSON
This commit is contained in:
15
app/jobs/refresh_products_cache_job.rb
Normal file
15
app/jobs/refresh_products_cache_job.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'open_food_network/products_renderer'
|
||||
|
||||
RefreshProductsCacheJob = Struct.new(:distributor_id, :order_cycle_id) do
|
||||
def perform
|
||||
Rails.cache.write "products-json-#{distributor_id}-#{order_cycle_id}", products_json
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def products_json
|
||||
OpenFoodNetwork::ProductsRenderer.new(distributor_id, order_cycle_id).products_json
|
||||
end
|
||||
|
||||
end
|
||||
15
spec/jobs/refresh_products_cache_job_spec.rb
Normal file
15
spec/jobs/refresh_products_cache_job_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'spec_helper'
|
||||
require 'open_food_network/products_renderer'
|
||||
|
||||
describe RefreshProductsCacheJob do
|
||||
let(:distributor_id) { 123 }
|
||||
let(:order_cycle_id) { 456 }
|
||||
|
||||
it "renders products and writes them to cache" do
|
||||
OpenFoodNetwork::ProductsRenderer.any_instance.stub(:products_json) { 'products' }
|
||||
|
||||
run_job RefreshProductsCacheJob.new distributor_id, order_cycle_id
|
||||
|
||||
expect(Rails.cache.read("products-json-123-456")).to eq 'products'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user