Class: GraphQL::FragmentCache::Fragment
- Inherits:
-
Object
- Object
- GraphQL::FragmentCache::Fragment
- Defined in:
- lib/graphql/fragment_cache/fragment.rb
Overview
Represents a single fragment to cache
Constant Summary collapse
- NIL_IN_CACHE =
Object.new
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_key ⇒ Object
-
#initialize(context, **options) ⇒ Fragment
constructor
A new instance of Fragment.
- #read ⇒ Object
- #value ⇒ Object
- #with_final_value? ⇒ Boolean
Constructor Details
#initialize(context, **options) ⇒ Fragment
Returns a new instance of Fragment.
53 54 55 56 57 58 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 53 def initialize(context, **) @context = context @keep_in_context = .delete(:keep_in_context) @options = @path = interpreter_context[:current_path] end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
51 52 53 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 51 def context @context end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
51 52 53 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 51 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
51 52 53 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 51 def path @path end |
Class Method Details
.read_multi(fragments) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 14 def read_multi(fragments) unless FragmentCache.cache_store.respond_to?(:read_multi) return fragments.map { |f| [f, f.read] }.to_h end fragments_to_cache_keys = fragments.map { |f| [f, f.cache_key] }.to_h # Filter out all the cache_keys for fragments with renew_cache: true in their context cache_keys = fragments_to_cache_keys.reject { |k, _v| k.context[:renew_cache] == true }.values # If there are cache_keys look up values with read_multi otherwise return an empty hash cache_keys_to_values = if cache_keys.empty? {} else FragmentCache.cache_store.read_multi(*cache_keys) end if GraphQL::FragmentCache.monitoring_enabled begin fragments.map do |fragment| cache_lookup_event( cache_key: fragment.cache_key, operation_name: fragment.context.query.operation_name, path: fragment.path, cache_hit: cache_keys_to_values.key?(fragment.cache_key) ) end rescue # Allow cache_lookup_event to fail when we do not have all of the requested attributes end end # Fragmenst without values or with renew_cache: true in their context will have nil values like the read method fragments_to_cache_keys.map { |fragment, cache_key| [fragment, cache_keys_to_values[cache_key]] }.to_h end |
Instance Method Details
#cache_key ⇒ Object
67 68 69 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 67 def cache_key @cache_key ||= CacheKeyBuilder.call(path: path, query: context.query, **) end |
#read ⇒ Object
60 61 62 63 64 65 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 60 def read return nil if context[:renew_cache] == true return read_from_context { value_from_cache } if @keep_in_context value_from_cache end |
#value ⇒ Object
75 76 77 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 75 def value final_value.dig(*path) end |
#with_final_value? ⇒ Boolean
71 72 73 |
# File 'lib/graphql/fragment_cache/fragment.rb', line 71 def with_final_value? !final_value.nil? end |