Class: ActiveMatrix::Util::AccountDataCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions

events, ignore_inspect

Constructor Details

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

Returns a new instance of AccountDataCache.

Raises:

  • (ArgumentError)


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

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

  @client = client
  @cache_time = cache_time
  @tracked_keys = Set.new

  return unless room

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

Instance Attribute Details

#cache_timeObject

Returns the value of attribute cache_time.



10
11
12
# File 'lib/active_matrix/util/account_data_cache.rb', line 10

def cache_time
  @cache_time
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/active_matrix/util/account_data_cache.rb', line 8

def client
  @client
end

#roomObject (readonly)

Returns the value of attribute room.



8
9
10
# File 'lib/active_matrix/util/account_data_cache.rb', line 8

def room
  @room
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  key = key.to_s unless key.is_a? String

  # Track the key whenever it's accessed
  @tracked_keys.add(key)

  return (key) unless cache_available?

  cache.fetch(cache_key(key), expires_in: @cache_time) do
    (key)
  end
end

#[]=(key, value) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_matrix/util/account_data_cache.rb', line 92

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

  @tracked_keys.add(key)
  cache.write(cache_key(key), value, expires_in: @cache_time) if cache_available?
end

#delete(key) ⇒ Object



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

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
  cache.delete(cache_key(key)) if cache_available?
end

#each(live: false) ⇒ Object



54
55
56
57
# File 'lib/active_matrix/util/account_data_cache.rb', line 54

def each(live: false)
  to_enum(__method__, live: live) { 0 } unless block_given?
  # Not enumerable with Rails.cache
end

#fetch_account_data(key) ⇒ Object



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

def (key)
  if room
    client.api.(client.mxid, room.id, key)
  else
    client.api.(client.mxid, key)
  end
rescue ActiveMatrix::MatrixNotFoundError
  {}
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_matrix/util/account_data_cache.rb', line 50

def key?(key)
  cache_available? && cache.exist?(cache_key(key))
end

#keysObject



38
39
40
# File 'lib/active_matrix/util/account_data_cache.rb', line 38

def keys
  @tracked_keys.to_a.sort
end

#reload!Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/active_matrix/util/account_data_cache.rb', line 27

def reload!
  # Clear all cache entries for this account data
  return unless cache_available?

  if room
    cache.delete_matched("activematrix:account_data:#{client.mxid}:room:#{room.id}:*")
  else
    cache.delete_matched("activematrix:account_data:#{client.mxid}:global:*")
  end
end

#sizeObject



46
47
48
# File 'lib/active_matrix/util/account_data_cache.rb', line 46

def size
  keys.count
end

#valuesObject



42
43
44
# File 'lib/active_matrix/util/account_data_cache.rb', line 42

def values
  []
end

#write(key, value) ⇒ Object

Write data without making API call (for sync responses)



105
106
107
108
109
# File 'lib/active_matrix/util/account_data_cache.rb', line 105

def write(key, value)
  key = key.to_s unless key.is_a? String
  @tracked_keys.add(key)
  cache.write(cache_key(key), value, expires_in: @cache_time) if cache_available?
end