Class: MatrixSdk::Util::TinycacheAdapter
- Extended by:
- Extensions
- Defined in:
- lib/matrix_sdk/util/tinycache_adapter.rb
Defined Under Namespace
Classes: Value
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #expire(key) ⇒ Object
- #fetch(key, expires_in: nil, cache_level: nil, **_opts) ⇒ Object
-
#initialize ⇒ TinycacheAdapter
constructor
A new instance of TinycacheAdapter.
- #read(key) ⇒ Object
- #valid?(key) ⇒ Boolean
- #write(key, value, expires_in: nil, cache_level: nil) ⇒ Object
Methods included from Extensions
Constructor Details
#initialize ⇒ TinycacheAdapter
Returns a new instance of TinycacheAdapter.
11 12 13 14 15 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 11 def initialize @config = {} clear end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 7 def client @client end |
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 7 def config @config end |
Instance Method Details
#cleanup ⇒ Object
71 72 73 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 71 def cleanup @cache.select { |_, v| v.expired? }.each { |_, v| v.value = nil } end |
#clear ⇒ Object
67 68 69 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 67 def clear @cache = {} end |
#delete(key) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 54 def delete(key) return false unless exist?(key) cache.delete key true end |
#exist?(key) ⇒ Boolean
34 35 36 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 34 def exist?(key) cache.key?(key) end |
#expire(key) ⇒ Object
61 62 63 64 65 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 61 def expire(key) return unless exist? key cache[key].expires_at = Time.at(0) end |
#fetch(key, expires_in: nil, cache_level: nil, **_opts) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 42 def fetch(key, expires_in: nil, cache_level: nil, **_opts) expires_in ||= config.dig(key, :expires) cache_level ||= client&.cache cache_level ||= :all cache_level = Tinycache::CACHE_LEVELS[cache_level] return read(key) if exist?(key) && !cache[key].expired? value = yield write(key, value, expires_in: expires_in, cache_level: cache_level) end |
#read(key) ⇒ Object
17 18 19 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 17 def read(key) cache[key]&.value end |
#valid?(key) ⇒ Boolean
38 39 40 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 38 def valid?(key) exist?(key) && !cache[key].expired? end |
#write(key, value, expires_in: nil, cache_level: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 21 def write(key, value, expires_in: nil, cache_level: nil) expires_in ||= config.dig(key, :expires) expires_in ||= 24 * 60 * 60 cache_level ||= client&.cache cache_level ||= :all cache_level = Tinycache::CACHE_LEVELS[cache_level] unless cache_level.is_a? Integer return value if cache_level < Tinycache::CACHE_LEVELS[config.dig(key, :level) || :none] cache[key] = Value.new(value, Time.now, Time.now + expires_in) value end |