Module: WorkOS::Cache
- Defined in:
- lib/workos/cache.rb
Overview
The Cache module provides a simple in-memory cache for storing values This module is not meant to be instantiated in a user space, and is used internally by the SDK
Defined Under Namespace
Classes: Entry
Class Method Summary collapse
-
.clear ⇒ Object
Clears all values from the cache.
-
.delete(key) ⇒ Object
Deletes a value from the cache.
-
.exist?(key) ⇒ Boolean
Checks if a value exists in the cache.
-
.fetch(key, expires_in: nil, force: false, &block) ⇒ Object
Fetches a value from the cache, or calls the block to fetch the value if it is not present.
-
.read(key) ⇒ Object
Reads a value from the cache.
-
.write(key, value, expires_in: nil) ⇒ Object
Writes a value to the cache.
Class Method Details
.clear ⇒ Object
Clears all values from the cache
74 75 76 |
# File 'lib/workos/cache.rb', line 74 def clear store.clear end |
.delete(key) ⇒ Object
Deletes a value from the cache
69 70 71 |
# File 'lib/workos/cache.rb', line 69 def delete(key) store.delete(key) end |
.exist?(key) ⇒ Boolean
Checks if a value exists in the cache
81 82 83 84 |
# File 'lib/workos/cache.rb', line 81 def exist?(key) entry = store[key] !(entry.nil? || entry.expired?) end |
.fetch(key, expires_in: nil, force: false, &block) ⇒ Object
Fetches a value from the cache, or calls the block to fetch the value if it is not present
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/workos/cache.rb', line 35 def fetch(key, expires_in: nil, force: false, &block) entry = store[key] if force || entry.nil? || entry.expired? value = block.call store[key] = Entry.new(value, expires_in) return value end entry.value end |
.read(key) ⇒ Object
Reads a value from the cache
50 51 52 53 54 55 |
# File 'lib/workos/cache.rb', line 50 def read(key) entry = store[key] return nil if entry.nil? || entry.expired? entry.value end |