Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip the query cache entirely #71

Merged
merged 1 commit into from
Sep 21, 2023
Merged

Skip the query cache entirely #71

merged 1 commit into from
Sep 21, 2023

Conversation

djmb
Copy link
Collaborator

@djmb djmb commented Sep 19, 2023

We want to avoid using the ActiveRecord query cache entirely with SolidCache:

  • Read queries don't need to be cached as that is handled by the local cache
  • Write queries should not clear the query cache, as you would not expect Rails.cache.write to do that.

Ideally we'd just be able to do something like:

class SolidCache::Record
  self.uses_query_cache = false
end

Absent that we need to do a bunch of gymnastics to get the behaviour we want.

  1. Selects - This is easy enough, we just wrap the query in an uncached block

  2. Upserts - Here we need to avoid calling connection.exec_insert_all as that will dirty the query cache. Instead we have to construct the SQL manually and execute it with connection.exec_query.

  3. Deletes - Similarly we need to avoid calling connection.delete here. Again we construct the SQL manually and execute it with connection.exec_delete.

We want to avoid using the ActiveRecord query cache entirely with
SolidCache:

- Read queries don't need to be cached as that is handled by the local
cache
- Write queries should not clear the query cache, as you would not
expect `Rails.cache.write` to clear out the entire query cache.

Ideally we'd just be able to do something like:

```ruby
class SolidCache::Record
  self.uses_query_cache = false
end
```

Absent that we need to do a bunch of gymnastics to get the behaviour we
want.

1. Selects
This is easy enough, we just wrap the query in an `uncached` block

2. Upserts
Here we need to avoid calling `connection.exec_insert_all` as that will
dirty the query cache. Instead we have to construct the SQL manually and
execute it with `connection.exec_query`.

3. Deletes
Similarly we need to avoid calling `connection.delete` here. Again we
construct the SQL manually and execute it with `connection.exec_delete`.
@djmb djmb merged commit d6b88fb into main Sep 21, 2023
3 checks passed
@djmb djmb deleted the skip-query-cache branch September 21, 2023 10:11
@djmb djmb restored the skip-query-cache branch September 21, 2023 10:12
@djmb djmb deleted the skip-query-cache branch October 3, 2023 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant