Class: MatrixSdk::Util::AccountDataCache

Inherits:
Object
  • Object
show all
Extended by:
Extensions, Tinycache
Includes:
Enumerable
Defined in:
lib/matrix_sdk/util/account_data_cache.rb

Constant Summary

Constants included from Tinycache

Tinycache::CACHE_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions

events, ignore_inspect

Methods included from Tinycache

adapter, adapter=, cached, extended, tinycache_adapter_config

Constructor Details

#initialize(client, room: nil, cache_time: 1 * 60 * 60, **_params) ⇒ AccountDataCache

Returns a new instance of AccountDataCache.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 15

def initialize(client, room: nil, cache_time: 1 * 60 * 60, **_params)
  raise ArgumentError, 'Must be given a Client instance' unless client.is_a? MatrixSdk::Client

  @client = client
  @cache_time = cache_time

  return unless room

  @room = room
  @room = client.ensure_room room unless @room.is_a? MatrixSdk::Room
end

Instance Attribute Details

#cache_timeObject

Returns the value of attribute cache_time.



11
12
13
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 11

def cache_time
  @cache_time
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 9

def client
  @client
end

#roomObject (readonly)

Returns the value of attribute room.



9
10
11
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 9

def room
  @room
end

Instance Method Details

#[](key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 68

def [](key)
  key = key.to_s unless key.is_a? String
  tinycache_adapter.fetch(key, expires_in: @cache_time) do
    if room
      client.api.(client.mxid, room.id, key)
    else
      client.api.(client.mxid, key)
    end
  rescue MatrixSdk::MatrixNotFoundError
    {}
  end
end

#[]=(key, value) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 81

def []=(key, value)
  key = key.to_s unless key.is_a? String
  if room
    client.api.(client.mxid, room.id, key, value)
  else
    client.api.(client.mxid, key, value)
  end
  tinycache_adapter.write(key, value)
end

#delete(key) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 58

def delete(key)
  key = key.to_s unless key.is_a? String
  if room
    client.api.(client.mxid, room.id, key, {})
  else
    client.api.(client.mxid, key, {})
  end
  tinycache_adapter.delete(key)
end

#each(live: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 47

def each(live: false)
  return to_enum(__method__, live: live) { keys.count } unless block_given?

  keys.each do |key|
    v = live ? self[key] : tinycache_adapter.read(key)
    # hash = v.hash
    yield key, v
    # self[key] = v if hash != v.hash
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 43

def key?(key)
  keys.key?(key.to_s)
end

#keysObject



31
32
33
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 31

def keys
  tinycache_adapter.send(:cache).keys
end

#reload!Object



27
28
29
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 27

def reload!
  tinycache_adapter.clear
end

#sizeObject



39
40
41
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 39

def size
  keys.count
end

#valuesObject



35
36
37
# File 'lib/matrix_sdk/util/account_data_cache.rb', line 35

def values
  keys.map { |key| tinycache_adapter.read(key) }
end