Using cache when within a transaction if already loaded#275
Using cache when within a transaction if already loaded#275
Conversation
No, after_commit avoids that problem.
Yes, the whole point of this check to see if you are in a transaction is that we are trying to avoid using cached records as part of an operation that modifies the database, and the whole point of a transaction is to modify the database. E.g. if you read a record from the cache, modify it, then save it, you will have propagated any cache corruption to the database. Similarly, if you calculate an aggregate value from values in an association that was fetched from the cache, you will similarly have propagated cache corruption to the database. As a workaround
Perhaps we should also have a method like |
Well that should never be a thing, for the reasons Dylan explains, I know is hard to avoid in the general case tho. I think we should document this in the readme better; but the whole point was to make the library a noop inside explicit transactions and hook on every single transaction to follow the changes happening to the actual objects. |
b7ec057 to
373c01c
Compare
I don't have the full context, but it is my understanding we do not want to send the cache backend destructive request while within a transaction for fear of corrupting with an operation that would never be commit on mysql.
We are hitting some code behaviours that are hard to diagnose and counter intuitive in active record callback code paths (those that imply transactions)
Here would be the current behaviour without these changes:
This lead to extra mysql calls for no reasons.
A "worst" example would be where you want to load data from IDC to edit it.
It somewhat counter intuitive to "rollback" the cached relation to a state pre-transaction.
Is there any pitfalls to use the cache if already populated even within a transaction? Is there a proper work around?
Thoughts? @dylanahsmith @chrisdonaldson
cc: @christianblais