Class: Boom::Storage::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/boom/storage/redis.rb

Instance Attribute Summary

Attributes inherited from Base

#lists

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Instance Method Details

#bootstrapObject



23
24
# File 'lib/boom/storage/redis.rb', line 23

def bootstrap
end

#clearObject



43
44
45
46
# File 'lib/boom/storage/redis.rb', line 43

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

#populateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/boom/storage/redis.rb', line 26

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



15
16
17
18
19
20
21
# File 'lib/boom/storage/redis.rb', line 15

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

#saveObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/boom/storage/redis.rb', line 48

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