Class: ActiveMatrix::AccountDataCache

Inherits:
Object
  • Object
show all
Extended by:
Extensions
Includes:
Enumerable
Defined in:
lib/active_matrix/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/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/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/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/account_data_cache.rb', line 8

def room
  @room
end

Instance Method Details

#[](key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/active_matrix/account_data_cache.rb', line 67

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

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

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

#[]=(key, value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_matrix/account_data_cache.rb', line 88

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)
end

#delete(key) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/active_matrix/account_data_cache.rb', line 57

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))
end

#each(live: false) ⇒ Object



52
53
54
55
# File 'lib/active_matrix/account_data_cache.rb', line 52

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

#fetch_account_data(key) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/active_matrix/account_data_cache.rb', line 78

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)


48
49
50
# File 'lib/active_matrix/account_data_cache.rb', line 48

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

#keysObject



36
37
38
# File 'lib/active_matrix/account_data_cache.rb', line 36

def keys
  @tracked_keys.to_a.sort
end

#reload!Object



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

def reload!
  # Clear all cache entries for this account data
  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



44
45
46
# File 'lib/active_matrix/account_data_cache.rb', line 44

def size
  keys.count
end

#valuesObject



40
41
42
# File 'lib/active_matrix/account_data_cache.rb', line 40

def values
  []
end

#write(key, value) ⇒ Object

Write data without making API call (for sync responses)



101
102
103
104
105
# File 'lib/active_matrix/account_data_cache.rb', line 101

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)
end