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.
This commit is contained in:
Maikel Linke
2022-11-03 13:02:12 +11:00
parent 8009b37723
commit 118c1f7cbd
2 changed files with 4 additions and 4 deletions

View File

@@ -1,8 +1,8 @@
# frozen_string_literal: true
class Api::AddressSerializer < ActiveModel::Serializer
# cached
# delegate :cache_key, to: :object
cached
delegate :cache_key, to: :object
attributes :id, :zipcode, :city, :state_name, :state_id,
:phone, :firstname, :lastname, :address1, :address2, :city, :country_id,

View File

@@ -29,10 +29,10 @@ describe Api::AddressSerializer do
}
end
it "updates even when database wasn't changed" do
it "uses stored result when database wasn't changed" do
expect {
address.first_name = "Nick"
}.to change {
}.to_not change {
serializer.to_json
}
end