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)


19
20
21
22
23
24
# 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

Instance Attribute Details

#default_expires_inObject (readonly)

Returns the value of attribute default_expires_in.



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

def default_expires_in
  @default_expires_in
end

Instance Method Details

#clearObject



56
57
58
# File 'lib/graphql/fragment_cache/memory_store.rb', line 56

def clear
  storage.clear
end

#delete(key) ⇒ Object



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

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

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/graphql/fragment_cache/memory_store.rb', line 30

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

#keysObject



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

def keys
  storage.keys
end

#read(key) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# 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, options = {}) ⇒ Object



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

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