Class: GraphQL::FragmentCache::MemoryStore
- Inherits:
-
Object
- Object
- GraphQL::FragmentCache::MemoryStore
- 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
-
#default_expires_in ⇒ Object
readonly
Returns the value of attribute default_expires_in.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
-
#initialize(expires_in: nil, **other) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #keys ⇒ Object
- #read(key) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(expires_in: nil, **other) ⇒ MemoryStore
Returns a new instance of MemoryStore.
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_in ⇒ Object (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
#clear ⇒ Object
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
26 27 28 |
# File 'lib/graphql/fragment_cache/memory_store.rb', line 26 def exist?(key) storage.key?(key) end |
#keys ⇒ Object
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, = {}) expires_in = [: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 |