Add CachedProductsRenderer - wraps ProductsRenderer using Rails cache

This commit is contained in:
Rohan Mitchell
2016-02-04 11:22:37 +11:00
parent 339f3fc2f0
commit d0b7b4ee50
2 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
require 'open_food_network/products_renderer'
# Wrapper for ProductsRenderer that caches the JSON output.
# ProductsRenderer::NoProducts is represented in the cache as nil,
# but re-raised to provide the same interface as ProductsRenderer.
module OpenFoodNetwork
class CachedProductsRenderer
def initialize(distributor, order_cycle)
@distributor = distributor
@order_cycle = order_cycle
end
def products_json
products_json = Rails.cache.fetch("products-json-#{@distributor.id}-#{@order_cycle.id}") do
begin
uncached_products_json
rescue ProductsRenderer::NoProducts
nil
end
end
raise ProductsRenderer::NoProducts.new if products_json.nil?
products_json
end
private
def uncached_products_json
ProductsRenderer.new(@distributor, @order_cycle).products_json
end
end
end