1
0
Fork 0
forked from OpenNeo/impress
impress/app/models/item/proxy_array.rb
Matchu e42de795dd Use item proxies for JSON caching
That is, once we get our list of IDs from the search engine, only
fetch records whose JSON we don't already have cached.

It's simpler here to use as_json, but it'd probably be even faster
if I figure out how to serve a plain JSON string from a Rails
controller. In the meantime, requests of entirely cached items
are coming in at about 85ms on average on my box (dev, cache
classes, many items), about 10ms better than the last
iteration.
2013-06-26 23:01:12 -07:00

19 lines
No EOL
545 B
Ruby

class Item
class ProxyArray < Array
METHOD_SCOPES = {as_json: Item.includes(:translations)}
def initialize(ids)
self.replace(ids.map { |id| Proxy.new(id.to_i) })
end
def prepare_method(method_name)
missed_proxies_by_id = self.
reject { |p| p.method_cached?(method_name) }.
index_by(&:id)
item_scope = METHOD_SCOPES[method_name.to_sym] || Item.scoped
item_scope.find(missed_proxies_by_id.keys).each do |item|
missed_proxies_by_id[item.id].item = item
end
end
end
end