Class: BetterAuth::RateLimiter::MemoryStore

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

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



8
9
10
11
# File 'lib/better_auth/rate_limiter.rb', line 8

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

Instance Method Details

#get(key) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/better_auth/rate_limiter.rb', line 13

def get(key)
  @mutex.synchronize do
    entry = @entries[key]
    return nil unless entry

    if Time.now.to_f >= entry[:expires_at]
      @entries.delete(key)
      return nil
    end

    entry[:data]
  end
end

#set(key, value, ttl:, update: false) ⇒ Object



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

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