Sha256: a2f83a09c3dd15d813c18397bc5afc3d37ade1db92081b32cf958e04a90c1738

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 KB

Contents

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

module Boom
  module Storage
    class Redis < Base

      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

      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

6 entries across 6 versions & 1 rubygems

Version Path
boom-0.2.3 lib/boom/storage/redis.rb
boom-0.2.2 lib/boom/storage/redis.rb
boom-0.2.1 lib/boom/storage/redis.rb
boom-0.2.0 lib/boom/storage/redis.rb
boom-0.1.2 lib/boom/storage/redis.rb
boom-0.1.1 lib/boom/storage/redis.rb