Class: MatrixSdk::Util::StateEventCache

Inherits:
Object
  • Object
show all
Extended by:
Extensions, Tinycache
Includes:
Enumerable
Defined in:
lib/matrix_sdk/util/state_event_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(room, cache_time: 30 * 60, **_params) ⇒ StateEventCache

Returns a new instance of StateEventCache.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 15

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

  @room = room
  @cache_time = cache_time
end

Instance Attribute Details

#cache_timeObject

Returns the value of attribute cache_time.



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

def cache_time
  @cache_time
end

#roomObject (readonly)

Returns the value of attribute room.



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

def room
  @room
end

Instance Method Details

#[](type, key = nil) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 77

def [](type, key = nil)
  type = type.to_s unless type.is_a? String
  tinycache_adapter.fetch("#{type}#{key ? "|#{key}" : ''}", expires_in: @cache_time) do
    client.api.get_room_state(room.id, type, **{ key: key }.compact)
  rescue MatrixSdk::MatrixNotFoundError
    {}
  end
end

#[]=(type, key = nil, value) ⇒ Object

rubocop:disable Style/OptionalArguments Not possible to put optional last



86
87
88
89
90
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 86

def []=(type, key = nil, value) # rubocop:disable Style/OptionalArguments Not possible to put optional last
  type = type.to_s unless type.is_a? String
  client.api.set_room_state(room.id, type, value, **{ state_key: key }.compact)
  tinycache_adapter.write("#{type}#{key ? "|#{key}" : ''}", value)
end

#clientObject



22
23
24
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 22

def client
  @room.client
end

#delete(type, key = nil) ⇒ Object



71
72
73
74
75
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 71

def delete(type, key = nil)
  type = type.to_s unless type.is_a? String
  client.api.set_room_state(room.id, type, {}, **{ state_key: key }.compact)
  tinycache_adapter.delete("#{type}#{key ? "|#{key}" : ''}")
end

#each(live: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 56

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

  keys.each do |type|
    real_type = type.split('|').first
    state_key = type.split('|').last
    state_key = nil if state_key == real_type

    v = live ? self[real_type, key: state_key] : tinycache_adapter.read(type)
    # hash = v.hash
    yield [real_type, state_key], v
    # self[key] = v if hash != v.hash
  end
end

#expire(type, key = nil) ⇒ Object



52
53
54
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 52

def expire(type, key = nil)
  tinycache_adapter.expire("#{type}#{key ? "|#{key}" : ''}")
end

#key?(type, key = nil) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 48

def key?(type, key = nil)
  keys.key?("#{type}#{key ? "|#{key}" : ''}")
end

#keysObject



30
31
32
33
34
35
36
37
38
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 30

def keys
  tinycache_adapter.send(:cache).keys.map do |type|
    real_type = type.split('|').first
    state_key = type.split('|').last
    state_key = nil if state_key == real_type

    [real_type, state_key]
  end
end

#reload!Object



26
27
28
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 26

def reload!
  tinycache_adapter.clear
end

#sizeObject



44
45
46
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 44

def size
  keys.count
end

#valuesObject



40
41
42
# File 'lib/matrix_sdk/util/state_event_cache.rb', line 40

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