rails / solid_cache

A database-backed ActiveSupport::Cache::Store
MIT License
891 stars 63 forks source link

Remove ActiveRecord query cache workarounds #123

Closed djmb closed 3 months ago

djmb commented 11 months ago

We don't want Solid Cache to interact with the ActiveRecord query cache at all.

So results should not be cached (which we achieve by wrapping queries in uncached blocks, but also we should avoid invalidating the AR query cache when we write).

At the moment this is done by dropping down to lower level methods that are not wrapped with dirties_query_cache.

This is not ideal and is relying on undocumented behaviour that could change in future releases.

Instead I think we need a way to disable the query cache invalidation in Rails. It would be nice to be able to set something on SolidCache::Record to say it doesn't use the query cache at all, but I don't think that will work as the query cache is a connection based, not model based.

We probably need another method similar to uncached that can totally disable the query cache.

jeremy commented 10 months ago

It would be nice to be able to set something on SolidCache::Record to say it doesn't use the query cache at all, but I don't think that will work as the query cache is a connection based, not model based

Think this is ultimately the way to go. It's the model that knows it needs a "direct" mode, so it needs to request as much from the connection it's using, rather than the connection needing to be configured that way.

It is a rare need indeed, though. Getting something like that into Rails could be approached as a side effect of fixing the cross-connection cache invalidation problem. If cluster/schema mutations are advertised for others to observe, the connection could get out of the business of deciding whose caches need clearing. The Solid Cache model could still advertise that it was mutated, but now with the expectation that nobody else is paying attention.

djmb commented 8 months ago

With https://github.com/rails/rails/pull/51204, we'll be able to remove these. We'll do this in a future version once Rails 7.2 is released.