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

Rails cache versioning changes breaks AMS cache module #2287

Closed
cintamani opened this issue Sep 26, 2018 · 4 comments
Closed

Rails cache versioning changes breaks AMS cache module #2287

cintamani opened this issue Sep 26, 2018 · 4 comments

Comments

@cintamani
Copy link

cintamani commented Sep 26, 2018

Expected behaviour vs actual behaviour

Expected

The cache is invalidated when a record is updated in Rails app running 5.1 or higher versions of the gem with the ActiveRecord.cache_versioning config set to true

Actual

The cache is not invalidated and the old record details are returned.

Steps to reproduce

Given a new rails application: rails new my-test-app.
Given an RSpec test suite setup.
Given a user model rails g model User name:string
Given AMS 10.7

Run this test to see the error:

require 'rails_helper'

RSpec.describe UserSerializer do
  it 'invalidate the serializer cache when the record is updated' do
    ActiveModelSerializers.config.perform_caching = true

    user = User.create(name: 'My Name')

    expect(UserSerializer.new(user).as_json[:name]).to eq('My Name')

    user.update_attributes(name: 'My New Name')

    expect(UserSerializer.new(user).as_json[:name]).to eq('My New Name')
  end
end

Environment

ActiveModelSerializers Version: 10.7

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

OS Type & Version: High Sierra, 10.13.3

Integrated application and version: Rails 5.2.1

Additional helpful information

The source of the issue has been identified in the code here: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer/concerns/caching.rb#L285
The issue is the module relying on the #cache_key method to return the version.
An easy fix would involve changing the #cache_key to #cache_key_with_version https://api.rubyonrails.org/classes/ActiveRecord/Integration.html#method-i-cache_key_with_version
a more long-term solution would be to implement cache versioning within AMS cache model.

@bf4
Copy link
Member

bf4 commented Sep 26, 2018

Would you be up to writing a PR?

@cintamani
Copy link
Author

I can't implement the versioned cache system but I will be happy to open a PR to solve this using the #cache_key_with_version method

@bf4
Copy link
Member

bf4 commented Sep 27, 2018 via email

cintamani added a commit to cintamani/active_model_serializers that referenced this issue Oct 1, 2018
In order to keep compatibility between the AMS cache feature and with Rails > 5.1  cache versioning, we have to use the `cache_key_with_version`. 
**NOTE** - This is a quick fix to the issue, if there will be future plans a proper cache versioning with recyclable key needs to be implemented. 

More info: rails-api#2287
@cintamani cintamani changed the title Rails cache versioning changes breaks AMS cache module [WIP] - Rails cache versioning changes breaks AMS cache module Oct 1, 2018
@cintamani cintamani changed the title [WIP] - Rails cache versioning changes breaks AMS cache module Rails cache versioning changes breaks AMS cache module Oct 1, 2018
@maxrosecollins
Copy link

I am having issues with this.
Is there a fix?

cintamani added a commit to cintamani/active_model_serializers that referenced this issue Jan 29, 2019
In order to keep compatibility between the AMS cache feature and with Rails > 5.1  cache versioning, we have to use the `cache_key_with_version`. 
**NOTE** - This is a quick fix to the issue, if there will be future plans a proper cache versioning with recyclable key needs to be implemented. 

More info: rails-api#2287
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

No branches or pull requests

3 participants