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
ActiveRecord::Base.uncached() - still cached... association cached #2878
Comments
agree, I seem to be having the same problem. It's driving me nuts. I also don't want to completely wipe out the association cache for one specific query, just bypass it seems good enough? |
Is this still an issue? |
This seems absolutely crazy, but @tenderlove or @jonleighton would be the men for the job! |
The |
This is an old issue, but since it's still open I decided to post the solution I used for it in case somebody else needs it. I added a class Foo < ApplicationRecord
has_many :bars
after_touch :clear_association_cache
end
class Bar < ApplicationRecord
belongs_to :foo, touch: true
end Now any time a Bar record is modified for a Foo, it clears the association cache on the Foo model. Unfortunately, this doesn't work for multi-threaded servers since it only clears the cache for the current thread. I don't know how to get around that, other than just clearing the association cache inside the |
I'm working with: Rails 3.0.0
I'm working in a multi-process environment. Caching of SQL data is a bad
thing for me (mostly). I use ActiveRecord::Base.uncached() as a wrapper
around my SQL queries.
However, I discovered that some queries were failing. When I looked,
the problem was very difficult to find.
There's an association cache. Who knew, it's very well hidden. Can't even
find reference in the documentation. Had to find it by listing methods
and looking for 'cache'.
I have models like
In my code I do:
If later on in my code I try to do tmp.bar again (gotcha comment line),
and generate the same query, the association cache will remember my prior
use and give me the old data. I know it's the old data because of the
sleep(). The update_at time stamp will be old.
Going a step further, I can see that the data for my Bar row has been
correctly updated, even before I try the association with foo/tmp to
retrieve the bar value for the second time. (See example code in second
transaction block.
So, my gotcha line is really doing the wrong thing.
Going a step further, I can prove that the association caching is the
problem. If I add clearing of the association cache as indicated, this
will fix the 'gotcha' so that the proper data is returned.
I submit that if one runs code in 'uncached()' that ALL caching needs to
be turned off. Getting old data is really a bad thing.
If you believe this is a user problem, then I'd submit a warning needs to
be added to the documentation so that folks know uncached doesn't turn
off all caching. This documentation should also explain why the name of
the function is uncached when the effect is 'partially uncached'.
PS. It would be really nice if one could select a default caching mode
(cached/uncached) and then use the uncached/cached methods to deviate.
... instead of there being one choice only, and those of us writing
applications that need mostly uncached having to code around the default
caching everywhere.
PPS. Rails sure beats the heck out of what I had to do before. Thanks :-)
The text was updated successfully, but these errors were encountered: