Sha256: 4ee3088fdc8ce8c9717a1a8163bef9e5a856b8df581f06b469199f4b7e627f54

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

# coding: utf-8
#
# Sup, Redis.
#
begin
  require 'digest'
  require 'redis'
rescue LoadError
end

module Boom
  module Storage
    class Redis < Base
      include Boom::Color
      include Boom::Output

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

      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

      def bootstrap
      end

      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

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

      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
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kaboom-0.3.3 lib/kaboom/storage/redis.rb
kaboom-0.3.2 lib/kaboom/storage/redis.rb
kaboom-0.3.1 lib/kaboom/storage/redis.rb