Skip to content

Identity cache inside transaction #533

Open
@DharmaPriya-V

Description

@DharmaPriya-V

Identity cache gem does not fetch from the cache store when fetch method was used inside any transaction, this is to avoid inconsistency in cache data as the changes will be commited at the end of transaction.

But they might be use case where we only load and not do updation. One way to solve this having a transaction which is not joinable to parent transaction. Usually identity_cache has should_use_cache? method which decides to fetch from cache or DB. should_use_cache? checks for any open transaction which is joinable, if so it make the DB call instead of fetching from cache. But if we wrap the fetch call inside transaction which is new and not joinable , then we can make use of advantage of the cache store and can save query at many places.

In the below code, fetch is happening inside the new transaction which is not joinable, should_use_cache? method returns true in this case and fetch from cache store.

Sample code:

ApplicationRecord.transaction do
    ApplicationRecord.transaction(requires_new: true, joinable: false) do
        ApplicationRecord.fetch(1)
     end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions