Class: Undertow::Store::RedisStore

Inherits:
Base
  • Object
show all
Defined in:
lib/undertow/store/redis_store.rb

Overview

Store adapter backed by Redis.

Accepts a Redis client or a connection pool (any object responding to #with).

Undertow.configure do |c|
  c.store = Undertow::Store::RedisStore.new(Redis.new(url: ENV['REDIS_URL']))
end

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RedisStore

Returns a new instance of RedisStore.



14
15
16
17
# File 'lib/undertow/store/redis_store.rb', line 14

def initialize(client)
  super()
  @client = client
end

Instance Method Details

#add_members(key, members) ⇒ Object



19
20
21
# File 'lib/undertow/store/redis_store.rb', line 19

def add_members(key, members)
  with_redis { |r| r.sadd(key, members) }
end

#lock_acquire(key, ttl:) ⇒ Object



39
40
41
# File 'lib/undertow/store/redis_store.rb', line 39

def lock_acquire(key, ttl:)
  with_redis { |r| r.set(key, '1', nx: true, ex: ttl) } || false
end

#lock_release(key) ⇒ Object



43
44
45
# File 'lib/undertow/store/redis_store.rb', line 43

def lock_release(key)
  with_redis { |r| r.del(key) }
end

#member_count(key) ⇒ Object



35
36
37
# File 'lib/undertow/store/redis_store.rb', line 35

def member_count(key)
  with_redis { |r| r.scard(key) } || 0
end

#members(key) ⇒ Object



27
28
29
# File 'lib/undertow/store/redis_store.rb', line 27

def members(key)
  with_redis { |r| r.smembers(key) } || []
end

#pop_members(key, count) ⇒ Object



31
32
33
# File 'lib/undertow/store/redis_store.rb', line 31

def pop_members(key, count)
  with_redis { |r| r.spop(key, count) } || []
end

#remove_member(key, member) ⇒ Object



23
24
25
# File 'lib/undertow/store/redis_store.rb', line 23

def remove_member(key, member)
  with_redis { |r| r.srem(key, member) }
end