Class: ActionHandle::Adapters::RedisPool

Inherits:
Base
  • Object
show all
Defined in:
lib/action_handle/adapters/redis_pool.rb

Constant Summary collapse

CurrentRedisWapper =
Class.new do
  def with
    yield(Redis.current)
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(pool = nil) ⇒ RedisPool

Returns a new instance of RedisPool.



15
16
17
# File 'lib/action_handle/adapters/redis_pool.rb', line 15

def initialize(pool = nil)
  @pool = pool || CurrentRedisWapper.new
end

Instance Method Details

#claim(key, value, ttl) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/action_handle/adapters/redis_pool.rb', line 53

def claim(key, value, ttl)
  perform_with_expectation('OK') do
    @pool.with do |client|
      client.set(key, value, ex: ttl)
    end
  end
end

#create(key, value, ttl) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/action_handle/adapters/redis_pool.rb', line 19

def create(key, value, ttl)
  perform_with_expectation(true) do
    @pool.with do |client|
      client.set(key, value, ex: ttl, nx: true)
    end
  end
end

#current?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/action_handle/adapters/redis_pool.rb', line 41

def current?(key, value)
  perform_with_expectation(value.to_s) do
    @pool.with { |client| client.get(key) }
  end
end

#expire(key) ⇒ Object



61
62
63
64
65
# File 'lib/action_handle/adapters/redis_pool.rb', line 61

def expire(key)
  perform_with_expectation(1) do
    @pool.with { |client| client.del(key) }
  end
end

#renew(key, value, ttl) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/action_handle/adapters/redis_pool.rb', line 27

def renew(key, value, ttl)
  perform_with_expectation(true) do
    @pool.with do |client|
      client.expire(key, ttl) if current?(key, value)
    end
  end
end

#taken?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/action_handle/adapters/redis_pool.rb', line 35

def taken?(key)
  perform_with_expectation(true) do
    @pool.with { |client| client.exists?(key) }
  end
end

#value(key) ⇒ Object



47
48
49
50
51
# File 'lib/action_handle/adapters/redis_pool.rb', line 47

def value(key)
  safely_perform do
    @pool.with { |client| client.get(key) }
  end
end