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

Fix an issue with redis keys not hitting #1043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/jsonapi/cached_resource_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def self.lookup(resource_klass, serializer_config_key, context, context_key, cac
hits = JSONAPI.configuration.resource_cache.read_multi(*keys).reject{|_,v| v.nil? }
return keys.each_with_object({}) do |key, hash|
(_, id, _, _) = key

if JSONAPI.configuration.resource_cache_key_format_function
key = JSONAPI.configuration.resource_cache_key_format_function(key)
end

if hits.has_key?(key)
hash[id] = self.from_cache_value(resource_klass, context, hits[key])
else
Expand Down
10 changes: 10 additions & 0 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Configuration
:resource_cache,
:default_resource_cache_field,
:resource_cache_digest_function,
:resource_cache_key_format_function,
:resource_cache_usage_report_function

def initialize
Expand Down Expand Up @@ -140,6 +141,12 @@ def initialize
# Optionally provide a callable which JSONAPI will call with information about cache
# performance. Should accept three arguments: resource name, hits count, misses count.
self.resource_cache_usage_report_function = nil

# Resource cache key formating
# Optionally provide a callable which JSONAPI will call in order to format your cache
# key, this is useful for stores such as redis, which cannot be queried using an array
# the way the rails in memory cache can
self.resource_cache_usage_report_function = nil
end

def cache_formatters=(bool)
Expand Down Expand Up @@ -256,7 +263,10 @@ def default_record_accessor_klass=(default_record_accessor_klass)

attr_writer :resource_cache_digest_function

attr_writer :resource_cache_key_format_function

attr_writer :resource_cache_usage_report_function

end

class << self
Expand Down