Skip to content

Rails cache versioning changes breaks AMS cache module #2287

Closed
@cintamani

Description

@cintamani

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.

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