Class: BrainzLab::Utilities::RateLimiter::MemoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/utilities/rate_limiter.rb

Overview

Simple in-memory store (for development/single-instance)

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



163
164
165
166
# File 'lib/brainzlab/utilities/rate_limiter.rb', line 163

def initialize
  @data = {}
  @mutex = Mutex.new
end

Instance Method Details

#clear!Object



193
194
195
196
197
# File 'lib/brainzlab/utilities/rate_limiter.rb', line 193

def clear!
  @mutex.synchronize do
    @data.clear
  end
end

#delete(key) ⇒ Object



187
188
189
190
191
# File 'lib/brainzlab/utilities/rate_limiter.rb', line 187

def delete(key)
  @mutex.synchronize do
    @data.delete(key)
  end
end

#get(key) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/brainzlab/utilities/rate_limiter.rb', line 168

def get(key)
  @mutex.synchronize do
    entry = @data[key]
    return nil unless entry
    return nil if entry[:expires_at] && Time.now > entry[:expires_at]

    entry[:value]
  end
end

#set(key, value, ttl: nil) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/brainzlab/utilities/rate_limiter.rb', line 178

def set(key, value, ttl: nil)
  @mutex.synchronize do
    @data[key] = {
      value: value,
      expires_at: ttl ? Time.now + ttl : nil
    }
  end
end