Class: Cistern::Data::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/cistern/data/redis.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Redis

Returns a new instance of Redis.



18
19
20
21
# File 'lib/cistern/data/redis.rb', line 18

def initialize(options={}, &block)
  @client  = options[:client] || ::Redis.new
  @default = block
end

Class Method Details

.marshalObject



4
5
6
7
8
9
10
11
12
# File 'lib/cistern/data/redis.rb', line 4

def self.marshal
  @marshal ||= begin
                 require 'multi_json'
                 MultiJson
               rescue LoadError
                 require 'json'
                 ::JSON
               end
end

.marshal=(marshal) ⇒ Object



14
15
16
# File 'lib/cistern/data/redis.rb', line 14

def self.marshal=(marshal)
  @marshal = marshal
end

Instance Method Details

#clearObject



23
24
25
26
27
# File 'lib/cistern/data/redis.rb', line 23

def clear
  unless (keys = client.keys("*")).empty?
    client.del(*keys)
  end
end

#fetch(key, *args) ⇒ Object Also known as: []



37
38
39
40
41
# File 'lib/cistern/data/redis.rb', line 37

def fetch(key, *args)
  assign_default(key)

  Cistern::Data::Redis.marshal.load(client.get(key, *args))
end

#store(key, value, *args) ⇒ Object Also known as: []=



29
30
31
32
33
# File 'lib/cistern/data/redis.rb', line 29

def store(key, value, *args)
  assign_default(key)

  client.set(key, Cistern::Data::Redis.marshal.dump(value), *args)
end