I follow the low-level caching guide found on the Heroku website . Copying their example is almost verbatim, here is my method:
def Trip.test_low_level
Rails.cache.fetch("test_destinations", expires_in: 12.hours) do
Trip.all.joins(:destinations).where("destinations.id = ?", 382)
end
end
As far as I know, this should:
- Run query on first call
Trip.test_low_level() - The second time,
Trip.test_low_level()it should look for a cache named "test_destinations" and return the result without querying the database .
However, no matter how many times I run the method, the request is still called. Here are the results of my terminal.
Trip.test_low_level()
Trip Load (6.2ms) SELECT "trips".* FROM "trips" INNER JOIN "destination_orders" ON "destination_orders"."trip_id" = "trips"."id" INNER JOIN "destinations" ON "destinations"."id" = "destination_orders"."destination_id" WHERE (destinations.id = 382)
=>
Trip.test_low_levle()
Trip Load (3.6ms) SELECT "trips".* FROM "trips" INNER JOIN "destination_orders" ON "destination_orders"."trip_id" = "trips"."id" INNER JOIN "destinations" ON "destinations"."id" = "destination_orders"."destination_id" WHERE (destinations.id = 382)
=>
Even if I call the cache directly, I get the same result:
Rails.cache.fetch('test_destinations')
Trip Load (5.0ms) SELECT "trips".* FROM "trips" INNER JOIN "destination_orders" ON "destination_orders"."trip_id" = "trips"."id" INNER JOIN "destinations" ON "destinations"."id" = "destination_orders"."destination_id" WHERE (destinations.id = 382)
=>
How can I prevent it from getting into the database a second time?
EDIT: Here are my settings in development.rb
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = true
production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true