mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
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:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user