Files
openfoodnetwork/app/serializers/api/address_serializer.rb
Maikel Linke 118c1f7cbd Cache address JSON to avoid database queries
We found that our database is quite busy querying the country all the
time. One source of these queries is the shop front which serializes
enterprises including their addresses. But addresses should be safe to
cache because:

- country records never change
- state records never change
- updating any other attribute changes the cache key

The caching was previously removed when the state_name attribute was
added. That is technically correct because it's possible to change a
state's name without updating the address. Then the cache would be out
of date. But we never update state names. And if we did, we would need
to invalidate the cache now.
2022-11-03 13:07:59 +11:00

19 lines
415 B
Ruby

# frozen_string_literal: true
class Api::AddressSerializer < ActiveModel::Serializer
cached
delegate :cache_key, to: :object
attributes :id, :zipcode, :city, :state_name, :state_id,
:phone, :firstname, :lastname, :address1, :address2, :city, :country_id,
:zipcode, :country_name
def country_name
object.country&.name
end
def state_name
object.state&.abbr
end
end