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

Issues with #object_cache_key method #1344

Closed
kevintyll opened this issue Nov 24, 2015 · 3 comments
Closed

Issues with #object_cache_key method #1344

kevintyll opened this issue Nov 24, 2015 · 3 comments

Comments

@kevintyll
Copy link
Contributor

In my use case, I have 2 separate issues that I've come across with the current implementation of the object_cache_key method.

The first is when I use a PORO object that does not have an updated_at attribute, I've defined a cache key, but object_cache_key does not look for that until after it builds object_time_safe and therefore throws an error.

I overrode object_cache_key in my app to look to see if cache_key is defined up front and just use that is it's there.

My other issue is, there are times I use different adapters for the same object in different parts of the app. Since different adapters use the same cache_key, the second adapter to get used returns the wrong format.

I overrode object_cache_key to include the adapter in the cache key to prevent this.

Here is my override.

      # overridden so it uses the object's cache key if it's defined instead of only calling it if @klass._cache_key is not present
      # and to include the adapter type in the cache key.
      def object_cache_key
        @klass._cache_options = Hash(@klass._cache_options) # while we are overriding things, we might as well convert nil options to a Hash to prevent a ReadThis error.
        if @cached_serializer.object.respond_to? :cache_key
          cache_key = "#{self.class.to_s.demodulize.underscore}/#{@cached_serializer.object.cache_key}"
          cache_key.gsub!(cache_key.split('/').second, @klass._cache_key.to_s) if @klass._cache_key
          return cache_key
        end
        object_time_safe = @cached_serializer.object.updated_at
        object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
        "#{self.class.to_s.demodulize.underscore}/#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}"
      end
@kevintyll
Copy link
Contributor Author

I had not seen ActiveModelSerializers, nice.

@kevintyll
Copy link
Contributor Author

PR created. Had to take implement the solution differently since caching is now it's own object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants