Sha256: 7e18409644799d618bc2b4d99ad8762e037bfb23860158944a88ddf55eb9417a

Contents?: true

Size: 1018 Bytes

Versions: 30

Compression:

Stored size: 1018 Bytes

Contents

class Cistern::Data::Redis
  Cistern::Data.backends[:redis] = self

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

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

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

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

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

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

  alias []= store

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

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

  alias [] fetch

  protected

  attr_reader :client, :default

  def assign_default(key)
    if client.keys(key).empty? && default
      default.call(client, key)
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
cistern-0.12.3 lib/cistern/data/redis.rb
cistern-2.2.3 lib/cistern/data/redis.rb
cistern-2.2.1 lib/cistern/data/redis.rb
cistern-2.1.0 lib/cistern/data/redis.rb
cistern-2.0.5 lib/cistern/data/redis.rb
cistern-2.0.4 lib/cistern/data/redis.rb
cistern-0.12.2 lib/cistern/data/redis.rb
cistern-0.12.1 lib/cistern/data/redis.rb
cistern-2.0.3 lib/cistern/data/redis.rb
cistern-2.0.2 lib/cistern/data/redis.rb
cistern-0.11.3 lib/cistern/data/redis.rb
cistern-2.0.1 lib/cistern/data/redis.rb
cistern-1.0.1.pre6 lib/cistern/data/redis.rb
cistern-1.0.1.pre5 lib/cistern/data/redis.rb
cistern-1.0.1.pre4 lib/cistern/data/redis.rb
cistern-1.0.1.pre3 lib/cistern/data/redis.rb
cistern-1.0.1.pre2 lib/cistern/data/redis.rb
cistern-0.11.2 lib/cistern/data/redis.rb
cistern-1.0.1.pre1 lib/cistern/data/redis.rb
cistern-1.0.0.pre lib/cistern/data/redis.rb