Class: ActiveMatrix::StateEventCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions

events, ignore_inspect

Constructor Details

#initialize(room, cache_time: 30 * 60, **_params) ⇒ StateEventCache

Returns a new instance of StateEventCache.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/active_matrix/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_timeObject

Returns the value of attribute cache_time.



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

def cache_time
  @cache_time
end

#roomObject (readonly)

Returns the value of attribute room.



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

def room
  @room
end

Instance Method Details

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



61
62
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
# File 'lib/active_matrix/state_event_cache.rb', line 61

def [](type, key = nil)
  type = type.to_s unless type.is_a? String
  return fetch_state(type, key) if 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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_matrix/state_event_cache.rb', line 106

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)

  # 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

#delete(type, key = nil) ⇒ Object



55
56
57
58
59
# File 'lib/active_matrix/state_event_cache.rb', line 55

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

#each(live: false) ⇒ Object



50
51
52
53
# File 'lib/active_matrix/state_event_cache.rb', line 50

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

#expire(type, key = nil) ⇒ Object



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

def expire(type, key = nil)
  cache.delete(cache_key(type, key))
end

#fetch_state(type, key = nil) ⇒ Object



100
101
102
103
104
# File 'lib/active_matrix/state_event_cache.rb', line 100

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

Returns:

  • (Boolean)


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

def key?(type, key = nil)
  cache.exist?(cache_key(type, key))
end

#keysObject



28
29
30
31
32
# File 'lib/active_matrix/state_event_cache.rb', line 28

def keys
  # State is not enumerable when using Rails.cache
  # This would require keeping track of keys separately
  []
end

#reload!Object



23
24
25
26
# File 'lib/active_matrix/state_event_cache.rb', line 23

def reload!
  # Clear all cache entries for this room's state
  cache.delete_matched("activematrix:room:#{room.id}:state:*")
end

#sizeObject



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

def size
  keys.count
end

#valuesObject



34
35
36
# File 'lib/active_matrix/state_event_cache.rb', line 34

def values
  []
end

#write(type, value, key = nil) ⇒ Object

Alias for writing without API call



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/active_matrix/state_event_cache.rb', line 128

def write(type, value, key = nil)
  type = type.to_s unless type.is_a? String

  # 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