Class: GraphQL::Cache::Marshal
- Inherits:
-
Object
- Object
- GraphQL::Cache::Marshal
- Defined in:
- lib/graphql/cache/marshal.rb
Overview
Used to marshal cache fetches into either writes or reads
Instance Attribute Summary collapse
-
#key ⇒ String
The cache key to marshal around.
Class Method Summary collapse
-
.[](key) ⇒ GraphQL::Cache::Marshal
Initializer helper to allow syntax like
Marshal[key].read(config, &block).
Instance Method Summary collapse
-
#build(cached, config) ⇒ Object
Uses Builder to build a valid GraphQL object from a cached value.
-
#initialize(key) ⇒ Marshal
constructor
Initialize a new instance of Marshal.
-
#read(config, &block) ⇒ Object
Read a value from cache if it exists and re-hydrate it or execute the block and write it's result to cache.
-
#write(config) ⇒ Object
Executes the resolution block and writes the result to cache.
Constructor Details
#initialize(key) ⇒ Marshal
Initialize a new instance of GraphQL::Cache::Marshal
23 24 25 |
# File 'lib/graphql/cache/marshal.rb', line 23 def initialize(key) self.key = key.to_s end |
Instance Attribute Details
#key ⇒ String
The cache key to marshal around
12 13 14 |
# File 'lib/graphql/cache/marshal.rb', line 12 def key @key end |
Class Method Details
.[](key) ⇒ GraphQL::Cache::Marshal
Initializer helper to allow syntax like
Marshal[key].read(config, &block)
18 19 20 |
# File 'lib/graphql/cache/marshal.rb', line 18 def self.[](key) new(key) end |
Instance Method Details
#build(cached, config) ⇒ Object
Uses Builder to build a valid GraphQL object from a cached value
71 72 73 |
# File 'lib/graphql/cache/marshal.rb', line 71 def build(cached, config) Builder[cached].build(config) end |
#read(config, &block) ⇒ Object
Read a value from cache if it exists and re-hydrate it or execute the block and write it's result to cache
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/graphql/cache/marshal.rb', line 32 def read(config, &block) cached = cache.read(key) if cached.nil? logger.debug "Cache miss: (#{key})" write config, &block else logger.debug "Cache hit: (#{key})" build cached, config end end |
#write(config) ⇒ Object
Executes the resolution block and writes the result to cache
48 49 50 51 52 53 54 |
# File 'lib/graphql/cache/marshal.rb', line 48 def write(config) resolved = yield document = Builder[resolved].deconstruct cache.write(key, document, expires_in: expiry(config)) resolved end |