Class: Boom::Storage::Redis

Inherits:
Base
  • Object
show all
Includes:
Color, Output
Defined in:
lib/kaboom/storage/redis.rb

Constant Summary

Constants included from Color

Color::CODES

Instance Attribute Summary

Attributes inherited from Base

#lists

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Output

#output

Methods included from Color

#colorize, included

Methods inherited from Base

#handle, #initialize, #item_exists?, #items, #list_exists?, #to_hash

Constructor Details

This class inherits a constructor from Boom::Storage::Base

Class Method Details

.sample_configObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/kaboom/storage/redis.rb', line 17

def self.sample_config
  %(
    {"backend": "redis",
      "redis": {
        "port": "6379",
        "host": "<host>"
      }
    }
  )
end

Instance Method Details

#bootstrapObject



35
36
# File 'lib/kaboom/storage/redis.rb', line 35

def bootstrap
end

#clearObject



55
56
57
58
# File 'lib/kaboom/storage/redis.rb', line 55

def clear
  redis.del "boom:lists"
  redis.del "boom:items"
end

#populateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kaboom/storage/redis.rb', line 38

def populate
  lists = redis.smembers("boom:lists") || []

  lists.each do |sha|
    list_name  = redis.get("boom:lists:#{sha}:name")
    @lists << list = List.new(list_name)

    shas = redis.lrange("boom:lists:#{sha}:items",0,-1) || []

    shas.each do |item_sha|
      name  = redis.get "boom:items:#{item_sha}:name"
      value = redis.get "boom:items:#{item_sha}:value"
      list.add_item(Item.new(name, value))
    end
  end
end

#redisObject



28
29
30
31
32
33
# File 'lib/kaboom/storage/redis.rb', line 28

def redis
  @redis ||= ::Redis.new :host => Boom.config.attributes["redis"]["host"],
                         :port => Boom.config.attributes["redis"]["port"]
rescue  Exception => exception
  handle exception, "You don't have Redis installed yet:\n  gem install redis"
end

#saveObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kaboom/storage/redis.rb', line 60

def save
    clear

    lists.each do |list|
      list_sha = Digest::SHA1.hexdigest(list.name)
      redis.set   "boom:lists:#{list_sha}:name", list.name
      redis.sadd  "boom:lists", list_sha

      list.items.each do |item|
        item_sha = Digest::SHA1.hexdigest(item.name)
        redis.rpush "boom:lists:#{list_sha}:items", item_sha
        redis.set   "boom:items:#{item_sha}:name", item.name
        redis.set   "boom:items:#{item_sha}:value", item.value
      end
    end

end