Class: ActionHandle::Adapters::CacheStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil) ⇒ CacheStore

Returns a new instance of CacheStore.



8
9
10
# File 'lib/action_handle/adapters/cache_store.rb', line 8

def initialize(cache = nil)
  @client = cache
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/action_handle/adapters/cache_store.rb', line 6

def client
  @client
end

Instance Method Details

#claim(key, value, ttl) ⇒ Object



40
41
42
43
44
# File 'lib/action_handle/adapters/cache_store.rb', line 40

def claim(key, value, ttl)
  perform_with_expectation('OK') do
    client.write(key, value, expires_in: ttl)
  end
end

#create(key, value, ttl) ⇒ Object



12
13
14
15
16
# File 'lib/action_handle/adapters/cache_store.rb', line 12

def create(key, value, ttl)
  perform_with_expectation('OK') do
    client.write(key, value, expires_in: ttl) unless taken?(key)
  end
end

#current?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/action_handle/adapters/cache_store.rb', line 30

def current?(key, value)
  perform_with_expectation(value.to_s) do
    client.read(key)
  end
end

#expire(key) ⇒ Object



46
47
48
49
50
# File 'lib/action_handle/adapters/cache_store.rb', line 46

def expire(key)
  perform_with_expectation(true) do
    client.delete(key).to_i > 0
  end
end

#renew(key, value, ttl) ⇒ Object



18
19
20
21
22
# File 'lib/action_handle/adapters/cache_store.rb', line 18

def renew(key, value, ttl)
  perform_with_expectation('OK') do
    client.write(key, value, expires_in: ttl) if current?(key, value)
  end
end

#taken?(key) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/action_handle/adapters/cache_store.rb', line 24

def taken?(key)
  perform_with_expectation(true) do
    client.exist?(key)
  end
end

#value(key) ⇒ Object



36
37
38
# File 'lib/action_handle/adapters/cache_store.rb', line 36

def value(key)
  safely_perform { client.read(key) }
end