Delete unused item proxy class

We used to do this for weird clever caching tricks that I don't think
were actually very effective. We stopped using this a few months ago,
and now I'm finally cleaning up this supporting code!
This commit is contained in:
Emi Matchu 2023-10-25 12:55:30 -07:00
parent ff1f3aa68c
commit ee3ffe8afe
3 changed files with 0 additions and 133 deletions

View file

@ -631,8 +631,4 @@ class Item < ApplicationRecord
items.values
end
def self.build_proxies(ids)
Item::ProxyArray.new(ids)
end
end

View file

@ -1,73 +0,0 @@
class Item
class Proxy
include FragmentLocalization
attr_reader :id
attr_writer :item, :owned, :wanted
delegate :description, :name, :nc?, :thumbnail_url, :thumbnail, :to_param, to: :item
def self.model_name
Item.model_name
end
def initialize(item_or_id)
if item_or_id.is_a? Item
@item = item_or_id
@id = @item.id
else
@id = item_or_id.to_i
end
@known_outputs = {method: {}, partial: {}}
end
def as_json(options={})
cache_method(:as_json, include_hanger_status: false).tap do |json|
json[:owned] = owned?
json[:wanted] = wanted?
end
end
def owned?
@owned
end
def to_partial_path
# HACK: could break without warning!
Item._to_partial_path
end
def wanted?
@wanted
end
def fragment_key(type, name)
prefix = type == :partial ? 'views/' : ''
base = localize_fragment_key("items/#{@id}##{name}", I18n.locale)
prefix + base
end
def set_known_output(type, name, value)
@known_outputs[type][name] = value
end
def known_partial_output(name)
@known_outputs[:partial][name]
end
private
def cache_method(method_name, *args, &block)
# Two layers of cache: a local copy, in case the method is called again,
# and then the Rails cache, before we hit the actual method call.
@known_outputs[:method][method_name] ||= begin
key = fragment_key(:method, method_name)
Rails.cache.fetch(key) { item.send(method_name, *args) }
end
end
def item
@item ||= Item.find(@id)
end
end
end

View file

@ -1,56 +0,0 @@
class Item
class ProxyArray < Array
# TODO: do we really need to include translations? The search documents
# know the proper name for each locale, so proxies can tell their
# parent items what their names are and save the query entirely.
SCOPES = HashWithIndifferentAccess.new({
method: {
as_json: Item.includes(:translations),
}
})
def initialize(proxies_or_items_or_ids=[])
self.replace(proxies_or_items_or_ids.map { |proxy_or_item_or_id|
if proxy_or_item_or_id.is_a?(Proxy)
proxy_or_item_or_id
else
Proxy.new(proxy_or_item_or_id)
end
})
end
def prepare_method(name)
prepare(:method, name)
end
def prepare_partial(name)
prepare(:partial, name)
end
private
def prepare(type, name)
item_scope = SCOPES[type][name]
raise "unexpected #{type} #{name.inspect}" unless item_scope
# Try to read all values from the cache in one go, setting the proxy
# values as we go along. Delete successfully set proxies, so that
# everything left in proxies_by_key in the end is known to be a miss.
proxies_by_key = {}
self.each do |p|
proxies_by_key[p.fragment_key(type, name)] ||= []
proxies_by_key[p.fragment_key(type, name)] << p
end
Rails.cache.read_multi(*proxies_by_key.keys).each { |k, v|
proxies_by_key.delete(k).each { |p| p.set_known_output(type, name, v) }
}
missed_proxies = proxies_by_key.values.flatten
missed_proxies_by_id = missed_proxies.index_by(&:id)
item_scope.find(missed_proxies_by_id.keys).each do |item|
missed_proxies_by_id[item.id].item = item
end
end
end
end