class GraphQL::Language::Cache
This cache is used by GraphQL::Language::Parser.parse_file when it’s enabled.
With Rails, parser caching may enabled by setting config.graphql.parser_cache = true in your Rails application.
The cache may be manually built by assigning GraphQL::Language::Parser.cache = GraphQL::Language::Cache.new(Pathname.new("some_dir"), secret: ENV.fetch("GRAPHQL_CACHE_SECRET")). The secret should be a stable value stored outside of the cache directory. When it isn’t provided, a process-local secret is generated and cache entries are rebuilt after the process restarts. Pass secret: nil to disable cache signing. This should only be used when the cache directory is trusted. This will create a directory (tmp/cache/graphql by default) that stores a cache of parsed files.
Much like bootsnap, the parser cache needs to be cleaned up manually. You will need to clear the cache directory for each new deployment of your application. Also note that the parser cache will grow as your schema is loaded, so the cache directory must be writable.
See GraphQL::Railtie for simple Rails integration
Constants
- DIGEST
- HMAC_SIZE
Public Class Methods
# File lib/graphql/language/cache.rb, line 37 def initialize(path, secret: SecureRandom.random_bytes(32)) @path = path @secret = secret end
Parameters
-
path(Pathname) — The directory where cache entries are stored. -
secret(String, nil) — A stable secret for verifying cache entries. When omitted, a process-local secret is generated. Passnilto disable cache signing.
Public Instance Methods
Source
# File lib/graphql/language/cache.rb, line 47 def fetch(filename) cache_key = cache_key_for(filename) return yield unless cache_key cache_path = @path.join(cache_key) begin return load_cache(cache_path, cache_key) if cache_path.file? rescue InvalidCache, SystemCallError # Rebuild caches created by older versions or with an invalid signature. end payload = yield begin write_cache(cache_path, cache_key, payload) rescue SystemCallError # Parser caching is best-effort; return the parsed payload if the cache cannot be written. end payload end