Class: RedisStore::Store

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

Overview

Redis-backed store object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Store

Generate an empty store


23
24
25
# File 'lib/redisstore.rb', line 23

def initialize(params = {})
  @data = Redis.new(params)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


18
19
20
# File 'lib/redisstore.rb', line 18

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object

Retrieve a key


44
45
46
# File 'lib/redisstore.rb', line 44

def [](key)
  parse @data.get(prep key)
end

#[]=(key, value) ⇒ Object

Set a key


51
52
53
# File 'lib/redisstore.rb', line 51

def []=(key, value)
  @data.set prep(key), prep(value)
end

#clear!(key = nil) ⇒ Object

Clears a specified key or the whole store


30
31
32
33
34
35
36
37
38
39
# File 'lib/redisstore.rb', line 30

def clear!(key = nil)
  if key.nil?
    @data.flushdb && {}
  else
    key = prep(key)
    value = @data.get key
    @data.del key
    parse value
  end
end

#include?(key) ⇒ Boolean

Check for a key in the store

Returns:

  • (Boolean)

65
66
67
# File 'lib/redisstore.rb', line 65

def include?(key)
  @data.exists(prep key)
end

#keysObject

Array of keys in the store


72
73
74
# File 'lib/redisstore.rb', line 72

def keys
  @data.keys.map { |x| parse x }
end

#sizeObject

Return the size of the store


58
59
60
# File 'lib/redisstore.rb', line 58

def size
  @data.dbsize
end