Skip to content

Cannot override default cache key with fragment caching #1804

Open
@onomated

Description

@onomated

Expected vs Actual Behavior

I have a model say User that I would like to present in two representations say a UserSerializer and ProfileSerializer. My serializers are defined as follows:

class UserSerializer < ApplicationSerializer
  cache skip_digest: true except: [:num_following]
  type 'users'
  attributes :first_name, :last_name, :num_following

  def object_cache_key
    "api:users:#{super}"
  end
end

class ProfileSerializer < UserSerializer
  cache skip_digest: true except: [:num_following]
  type 'profiles'
  attributes :personal_info_attribute

  def object_cache_key
    "api:profiles:#{super}"
  end
end

The generated cache keys do not follow the overidden object_cache_key and fall-back to the AMS default that is based on the model name. So ProfileSerializer responses overwrite responses of the UserSerializer in cache since they both act on the same model.
Is there a way to provide a cache prefix or overwrite the default cache key generated in fragment caching? I have tried cache key: 'some_prefix', skip_digest: true except: [:num_following] and that also doesn't work.

This issue only occurs with fragment caching due to the dynamic creation of Cached vs NonCached variants of the serializer. Works fine without fragment caching.

Steps to reproduce

See examples above

Environment

Rails 4.2.2, Grape 0.16

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions