Module: GraphQL::Cache

Defined in:
lib/graphql/cache.rb,
lib/graphql/cache/field.rb,
lib/graphql/cache/rails.rb,
lib/graphql/cache/builder.rb,
lib/graphql/cache/marshal.rb,
lib/graphql/cache/version.rb,
lib/graphql/cache/middleware.rb

Defined Under Namespace

Classes: Builder, Field, Marshal, Middleware, Rails

Constant Summary collapse

VERSION =
'0.2.5'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject

An object that must conform to the same API as ActiveSupport::Cache::Store

Returns:

  • (Object)

    Defaults to Rails.cache in a Rails environment



13
14
15
# File 'lib/graphql/cache.rb', line 13

def cache
  @cache
end

.expiryInteger

Global default cache key expiration time in seconds.

Returns:

  • (Integer)

    Default: 5400 (90 minutes)



17
18
19
# File 'lib/graphql/cache.rb', line 17

def expiry
  @expiry
end

.forceBoolean

When truthy, override all caching (force evalutaion of resolvers)

Returns:

  • (Boolean)

    Default: false



21
22
23
# File 'lib/graphql/cache.rb', line 21

def force
  @force
end

.loggerLogger

Logger instance to use when logging cache hits/misses.

Returns:

  • (Logger)


25
26
27
# File 'lib/graphql/cache.rb', line 25

def logger
  @logger
end

.namespaceString

Global namespace for keys

Returns:

  • (String)

    Default: "GraphQL::Cache"



29
30
31
# File 'lib/graphql/cache.rb', line 29

def namespace
  @namespace
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Provides for initializer syntax

GraphQL::Cache.configure do |c|
  c.namespace = 'MyNamespace'
end

Yields:

  • (_self)

Yield Parameters:



38
39
40
# File 'lib/graphql/cache.rb', line 38

def configure
  yield self
end

.fetch(key, config: {}, &block) ⇒ Object

Fetches/writes a value for key from the cache

Always evaluates the block unless config[:metadata][:cache] is truthy

Parameters:

  • key (String)

    the cache key to attempt to fetch

  • config (Hash) (defaults to: {})

    a hash of middleware config values used to marshal cache data

Options Hash (config:):

  • :metadata (Hash)

    The metadata collected from the field definition

Returns:

  • (Object)


56
57
58
59
60
# File 'lib/graphql/cache.rb', line 56

def self.fetch(key, config: {}, &block)
  return block.call unless config[:metadata][:cache]

  Marshal[key].read(config, &block)
end