In my Rails application I see a few 'cache' during SQL query in the server log:
[DEBUG] CACHE (0.0ms) SELECT
regions
.* FROMregions
WHEREregions
.name
= 'NY' LIMIT 1
I want to know if these caching are done by Rails, or by the MySQL itself? In another word, do these cached queries touch the database layer at all?
Rails provides an SQL query cache which is used to cache the results of database queries for the duration of a request. When Rails encounters the same query again within the same request, it uses the cached result set instead of running the query against the database again.
SQL Server does not cache the query results, but it caches the data pages it reads in memory. The data from these pages is then used to produce the query result.
One way is by caching the results of slow database queries. Caching involves storing the results of your database queries temporarily into memory. Subsequent attempts to query the database will use the stored data instead.
Rails (as of 2.1) provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk. Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings.
The correct answer to the question is "the caching is done by Rails; the queries do not ever get to the database".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With