active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Rails cache versioning changes breaks AMS cache module

Open cintamani opened this issue 7 years ago • 4 comments

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.

cintamani avatar Sep 26 '18 14:09 cintamani

Would you be up to writing a PR?

bf4 avatar Sep 26 '18 15:09 bf4

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

cintamani avatar Sep 26 '18 17:09 cintamani

I'm so sorry that I haven't been more active lately on AMS. I'm just not using it now. I've happy to help anyone interesting in maintaining it.

On Wed, Sep 26, 2018 at 12:41 PM cintamani [email protected] wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rails-api/active_model_serializers/issues/2287#issuecomment-424806529, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIuQsJ4l-b9YdS4N8ioBuW96MlxF4RRks5ue7xZgaJpZM4W6vEC .

bf4 avatar Sep 27 '18 00:09 bf4

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

maxrosecollins avatar Jan 22 '19 15:01 maxrosecollins