class GraphQL::FragmentCache::MemoryStore

Memory adapter for storing cached fragments

Attributes

default_expires_in[R]
storage[R]

Public Class Methods

new(expires_in: nil, **other) click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 19
def initialize(expires_in: nil, **other)
  raise ArgumentError, "Unsupported options: #{other.keys.join(",")}" unless other.empty?

  @default_expires_in = expires_in
  @storage = {}
end

Public Instance Methods

clear() click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 55
def clear
  storage.clear
end
delete(key) click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 50
def delete(key)
  key = key.to_s
  storage.delete(key)
end
exist?(key) click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 30
def exist?(key)
  storage.key?(key)
end
keys() click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 26
def keys
  storage.keys
end
read(key) click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 34
def read(key)
  key = key.to_s
  storage[key]&.then do |entry|
    if entry.expired?
      delete(key)
      next
    end
    entry.value
  end
end
write(key, value, expires_in: default_expires_in, **options) click to toggle source
# File lib/graphql/fragment_cache/memory_store.rb, line 45
def write(key, value, expires_in: default_expires_in, **options)
  key = key.to_s
  @storage[key] = Entry.new(value: value, expires_at: expires_in ? Time.now + expires_in : nil)
end