class MatrixSdk::Util::TinycacheAdapter

Constants

Value

Attributes

cache[R]
client[RW]
config[RW]

Public Class Methods

new() click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 7
def initialize
  @config = {}

  clear
end

Public Instance Methods

cleanup() click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 61
def cleanup
  @cache.delete_if { |_, v| v.expired? }
end
clear() click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 57
def clear
  @cache = {}
end
delete(key) click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 50
def delete(key)
  return false unless exist?(key)

  cache.delete key
  true
end
exist?(key) click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 30
def exist?(key)
  cache.key?(key)
end
fetch(key, expires_in: nil, cache_level: nil, **_opts) { || ... } click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 38
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) click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 13
def read(key)
  cache[key]&.value
end
valid?(key) click to toggle source
# File lib/matrix_sdk/util/tinycache_adapter.rb, line 34
def valid?(key)
  exist?(key) && !cache[key].expired?
end
write(key, value, expires_in: nil, cache_level: nil) click to toggle source
# 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)
  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