Class: ActiveMatrix::AccountDataCache
- Inherits:
-
Object
- Object
- ActiveMatrix::AccountDataCache
- Extended by:
- Extensions
- Includes:
- Enumerable
- Defined in:
- lib/active_matrix/account_data_cache.rb
Instance Attribute Summary collapse
-
#cache_time ⇒ Object
Returns the value of attribute cache_time.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#room ⇒ Object
readonly
Returns the value of attribute room.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each(live: false) ⇒ Object
- #fetch_account_data(key) ⇒ Object
-
#initialize(client, room: nil, cache_time: 1 * 60 * 60, **_params) ⇒ AccountDataCache
constructor
A new instance of AccountDataCache.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #reload! ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
-
#write(key, value) ⇒ Object
Write data without making API call (for sync responses).
Methods included from Extensions
Constructor Details
#initialize(client, room: nil, cache_time: 1 * 60 * 60, **_params) ⇒ AccountDataCache
Returns a new instance of AccountDataCache.
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_time ⇒ Object
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 |
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/active_matrix/account_data_cache.rb', line 8 def client @client end |
#room ⇒ Object (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 fetch_account_data(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.set_room_account_data(client.mxid, room.id, key, value) else client.api.set_account_data(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.set_room_account_data(client.mxid, room.id, key, {}) else client.api.set_account_data(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 fetch_account_data(key) if room client.api.get_room_account_data(client.mxid, room.id, key) else client.api.get_account_data(client.mxid, key) end rescue ActiveMatrix::MatrixNotFoundError {} end |
#key?(key) ⇒ Boolean
48 49 50 |
# File 'lib/active_matrix/account_data_cache.rb', line 48 def key?(key) cache.exist?(cache_key(key)) end |
#keys ⇒ Object
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 |
#size ⇒ Object
44 45 46 |
# File 'lib/active_matrix/account_data_cache.rb', line 44 def size keys.count end |
#values ⇒ Object
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 |