class GraphQL::Cache::Marshal
Used to marshal cache fetches into either writes or reads
Attributes
key[RW]
The cache key to marshal around
@return [String] The cache key
Public Class Methods
[](key)
click to toggle source
Initializer helper to allow syntax like `Marshal.read(config, &block)`
@return [GraphQL::Cache::Marshal]
# File lib/graphql/cache/marshal.rb, line 18 def self.[](key) new(key) end
new(key)
click to toggle source
Initialize a new instance of {GraphQL::Cache::Marshal}
# File lib/graphql/cache/marshal.rb, line 23 def initialize(key) self.key = key.to_s end
Public Instance Methods
cache()
click to toggle source
@private
# File lib/graphql/cache/marshal.rb, line 69 def cache GraphQL::Cache.cache end
expiry(config)
click to toggle source
@private
# File lib/graphql/cache/marshal.rb, line 60 def expiry(config) if config.is_a?(Hash) && config[:expiry] config[:expiry] else GraphQL::Cache.expiry end end
logger()
click to toggle source
@private
# File lib/graphql/cache/marshal.rb, line 74 def logger GraphQL::Cache.logger end
read(config, force: false, &block)
click to toggle source
Read a value from cache if it exists and re-hydrate it or execute the block and write it's result to cache
@param config [Hash] The object passed to `cache:` on the field definition @return [Object]
# File lib/graphql/cache/marshal.rb, line 32 def read(config, force: false, &block) # write new data from resolver if forced return write(config, &block) if force cached = cache.read(key) if cached.nil? logger.debug "Cache miss: (#{key})" write config, &block else logger.debug "Cache hit: (#{key})" cached end end
write(config) { || ... }
click to toggle source
Executes the resolution block and writes the result to cache
@see GraphQL::Cache::Deconstruct#perform @param config [Hash] The middleware resolution config hash
# File lib/graphql/cache/marshal.rb, line 51 def write(config) resolved = yield document = Deconstructor[resolved].perform cache.write(key, document, expires_in: expiry(config)) resolved end