Class: GraphQL::Cache::Marshal

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/cache/marshal.rb

Overview

Used to marshal data to/from cache on cache hits/misses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Marshal



15
16
17
# File 'lib/graphql/cache/marshal.rb', line 15

def initialize(key)
  self.key = key.to_s
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/graphql/cache/marshal.rb', line 9

def key
  @key
end

Class Method Details

.[](key) ⇒ Object



11
12
13
# File 'lib/graphql/cache/marshal.rb', line 11

def self.[](key)
  new(key)
end

Instance Method Details

#build(cached, config) ⇒ Object



41
42
43
# File 'lib/graphql/cache/marshal.rb', line 41

def build(cached, config)
  Builder[cached].build(config)
end

#cacheObject



45
46
47
# File 'lib/graphql/cache/marshal.rb', line 45

def cache
  GraphQL::Cache.cache
end

#loggerObject



49
50
51
# File 'lib/graphql/cache/marshal.rb', line 49

def logger
  GraphQL::Cache.logger
end

#read(config, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/cache/marshal.rb', line 19

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



31
32
33
34
35
36
37
38
39
# File 'lib/graphql/cache/marshal.rb', line 31

def write(config)
  resolved = yield
  expiry = config[:metadata][:expiry] || GraphQL::Cache.expiry

  document = Builder[resolved].deconstruct

  cache.write(key, document, expires_in: expiry)
  resolved
end