I was following this Thoughtbot tutorial on making fast caching
-
I can'f find in docs what the #cached method is doing here:
class MySerializer < ActiveModel::Serializer cached delegate :cache_key, to: :object end
-
Why does the tutorial say this?:
Location.near('Boston', 15)
Is #near a geocoder gem method? I looked into this and the answer is that it's a scope. Why is it called a scope?
This returns an ActiveRecord::Relation with each location decorated with two pieces of additional data: distance and bearing. This data is dependent upon the point of interest searched; if I were to change my query from Boston to Cambridge (just across the Charles River), the distances and bearings would be affected even though the location data from the database would be the same...
- Why doesn't caching make sense here? Why wouldn't the cache expire when we switched the term from Boston to Cambridge?
However, enabling caching on the LocationSerializer doesn’t make sense because distance changes based on the search term. Finding locations within 15 miles of Boston would cache each relative to that search string; searching in Cambridge would return the previously cached results from Boston meaning most distances would be invalid.
- I see what the tutorial is doing... it's trying to find a way to cache location data but not distance data. But I don't understand some parts of what is going on here. So if we use the SearchSerializer, how is it invalidating Distance but not location?
Object composition is the perfect solution; by writing a brand new serializer that combines two other serializers (one which caches the location data from the database and one which adds distance)...
Is it because the DistanceSerializer isn't caching?
class LocationSerializer < ActiveModel::Serializer
cached
delegate :cache_key, to: :object
attributes :id, :name, :description
attributes :street_one, :city, :state, :postal_code, :country, :latitude, :longitude
# attributes :distance <= removed
has_many :images
has_many :categories
end
class DistanceSerializer < ActiveModel::Serializer
attributes :distance
end
If that's the case, what is the cached method doing?
Aucun commentaire:
Enregistrer un commentaire