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.
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_in ⇒ Object (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
#clear ⇒ Object
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
30 31 32 |
# File 'lib/graphql/fragment_cache/memory_store.rb', line 30 def exist?(key) storage.key?(key) end |
#keys ⇒ Object
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, = {}) 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 |