Class: GraphQL::FragmentCache::MemoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/fragment_cache/memory_store.rb

Overview

Memory adapter for storing cached fragments

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expires_in: nil, **other) ⇒ MemoryStore

Returns a new instance of MemoryStore.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/graphql/fragment_cache/memory_store.rb', line 15

def initialize(expires_in: nil, **other)
  raise ArgumentError, "Unsupported options: #{other.keys.join(",")}" unless other.empty?

  @default_expires_in = expires_in
  @storage = {}
end

Instance Attribute Details

#default_expires_inObject (readonly)

Returns the value of attribute default_expires_in.



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

def default_expires_in
  @default_expires_in
end

Instance Method Details

#clearObject



52
53
54
# File 'lib/graphql/fragment_cache/memory_store.rb', line 52

def clear
  storage.clear
end

#delete(key) ⇒ Object



47
48
49
50
# File 'lib/graphql/fragment_cache/memory_store.rb', line 47

def delete(key)
  key = key.to_s
  storage.delete(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/graphql/fragment_cache/memory_store.rb', line 26

def exist?(key)
  storage.key?(key)
end

#keysObject



22
23
24
# File 'lib/graphql/fragment_cache/memory_store.rb', line 22

def keys
  storage.keys
end

#read(key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql/fragment_cache/memory_store.rb', line 30

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, options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/graphql/fragment_cache/memory_store.rb', line 41

def write(key, value, options = {})
  expires_in = options[:expires_in] || default_expires_in
  key = key.to_s
  @storage[key] = Entry.new(value: value, expires_at: expires_in ? Time.now + expires_in : nil)
end