Class: MatrixSdk::Util::TinycacheAdapter
- 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
- #fetch(key, expires_in: nil, cache_level: nil, **_opts) ⇒ Object
-
#initialize ⇒ TinycacheAdapter
constructor
A new instance of TinycacheAdapter.
- #read(key) ⇒ Object
- #write(key, value, expires_in: nil, cache_level: nil) ⇒ Object
Constructor Details
#initialize ⇒ TinycacheAdapter
Returns a new instance of TinycacheAdapter.
7 8 9 10 11 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 7 def initialize @config = {} clear end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 5 def client @client end |
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 5 def config @config end |
Instance Method Details
#cleanup ⇒ Object
56 57 58 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 56 def cleanup @cache.delete_if { |_, v| v.expired? } end |
#clear ⇒ Object
52 53 54 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 52 def clear @cache = {} end |
#delete(key) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 45 def delete(key) return false unless exist?(key) cache.delete key true end |
#exist?(key) ⇒ Boolean
29 30 31 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 29 def exist?(key) cache.key?(key) end |
#fetch(key, expires_in: nil, cache_level: nil, **_opts) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 33 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
13 14 15 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 13 def read(key) cache[key]&.value end |
#write(key, value, expires_in: nil, cache_level: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/matrix_sdk/util/tinycache_adapter.rb', line 17 def write(key, value, expires_in: nil, cache_level: nil) expires_in ||= config.dig(key, :expires) 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 |