Class: ActiveMatrix::Util::StateEventCache
- Extended by:
- Extensions
- Includes:
- Enumerable
- Defined in:
- lib/active_matrix/util/state_event_cache.rb
Instance Attribute Summary collapse
-
#cache_time ⇒ Object
Returns the value of attribute cache_time.
-
#room ⇒ Object
readonly
Returns the value of attribute room.
Instance Method Summary collapse
- #[](type, key = nil) ⇒ Object
-
#[]=(type, key = nil, value) ⇒ Object
rubocop:disable Style/OptionalArguments Not possible to put optional last.
- #client ⇒ Object
- #delete(type, key = nil) ⇒ Object
- #each(live: false) ⇒ Object
- #expire(type, key = nil) ⇒ Object
- #fetch_state(type, key = nil) ⇒ Object
-
#initialize(room, cache_time: 30 * 60, **_params) ⇒ StateEventCache
constructor
A new instance of StateEventCache.
- #key?(type, key = nil) ⇒ Boolean
- #keys ⇒ Object
- #reload! ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
-
#write(type, value, key = nil) ⇒ Object
Alias for writing without API call.
Methods included from Extensions
Constructor Details
#initialize(room, cache_time: 30 * 60, **_params) ⇒ StateEventCache
Returns a new instance of StateEventCache.
14 15 16 17 18 19 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 14 def initialize(room, cache_time: 30 * 60, **_params) raise ArgumentError, 'Must be given a Room instance' unless room.is_a? ActiveMatrix::Room @room = room @cache_time = cache_time end |
Instance Attribute Details
#cache_time ⇒ Object
Returns the value of attribute cache_time.
10 11 12 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 10 def cache_time @cache_time end |
#room ⇒ Object (readonly)
Returns the value of attribute room.
8 9 10 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 8 def room @room end |
Instance Method Details
#[](type, key = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 63 def [](type, key = nil) type = type.to_s unless type.is_a? String return fetch_state(type, key) if !cache_available? || client.cache == :none begin cached_value = cache.fetch(cache_key(type, key), expires_in: @cache_time) do result = fetch_state(type, key) # Convert Response objects to plain hashes for caching # Response objects extend Hash but contain an @api instance variable that can't be serialized if result.is_a?(Hash) # Create a clean hash with just the data, no instance variables or extended modules # Deep convert to ensure no mock objects are included clean_hash = {} result.each do |key, value| clean_hash[key] = case value when Hash then value.to_h when Array then value.map { |v| v.is_a?(Hash) ? v.to_h : v } else value end end clean_hash else result end end # If it's a hash and we have an API client, convert it back to a Response if cached_value.is_a?(Hash) && !cached_value.empty? && client.respond_to?(:api) ActiveMatrix::Response.new(client.api, cached_value) else cached_value end rescue StandardError # If caching fails, return the direct result fetch_state(type, key) end end |
#[]=(type, key = nil, value) ⇒ Object
rubocop:disable Style/OptionalArguments Not possible to put optional last
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 108 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) return unless cache_available? # Convert to plain hash for caching to avoid serialization issues with Mocha cacheable_value = if value.is_a?(Hash) clean_hash = {} value.each do |k, v| clean_hash[k] = case v when Hash then v.to_h when Array then v.map { |item| item.is_a?(Hash) ? item.to_h : item } else v end end clean_hash else value end cache.write(cache_key(type, key), cacheable_value, expires_in: @cache_time) end |
#client ⇒ Object
21 22 23 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 21 def client @room.client end |
#delete(type, key = nil) ⇒ Object
57 58 59 60 61 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 57 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) cache.delete(cache_key(type, key)) if cache_available? end |
#each(live: false) ⇒ Object
52 53 54 55 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 52 def each(live: false) to_enum(__method__, live: live) { 0 } unless block_given? # Not enumerable with Rails.cache end |
#expire(type, key = nil) ⇒ Object
48 49 50 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 48 def expire(type, key = nil) cache.delete(cache_key(type, key)) if cache_available? end |
#fetch_state(type, key = nil) ⇒ Object
102 103 104 105 106 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 102 def fetch_state(type, key = nil) client.api.get_room_state(room.id, type, **{ key: key }.compact) rescue ActiveMatrix::MatrixNotFoundError {} end |
#key?(type, key = nil) ⇒ Boolean
44 45 46 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 44 def key?(type, key = nil) cache_available? && cache.exist?(cache_key(type, key)) end |
#keys ⇒ Object
30 31 32 33 34 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 30 def keys # State is not enumerable when using Rails.cache # This would require keeping track of keys separately [] end |
#reload! ⇒ Object
25 26 27 28 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 25 def reload! # Clear all cache entries for this room's state cache.delete_matched("activematrix:room:#{room.id}:state:*") if cache_available? end |
#size ⇒ Object
40 41 42 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 40 def size keys.count end |
#values ⇒ Object
36 37 38 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 36 def values [] end |
#write(type, value, key = nil) ⇒ Object
Alias for writing without API call
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/active_matrix/util/state_event_cache.rb', line 132 def write(type, value, key = nil) type = type.to_s unless type.is_a? String return unless cache_available? # Convert to plain hash for caching to avoid serialization issues cacheable_value = if value.is_a?(Hash) clean_hash = {} value.each do |k, v| clean_hash[k] = case v when Hash then v.to_h when Array then v.map { |item| item.is_a?(Hash) ? item.to_h : item } else v end end clean_hash else value end cache.write(cache_key(type, key), cacheable_value, expires_in: @cache_time) end |