Sha256: d31a4acaaa626715016560e2f89e53a29263b035944e9544ed3c29d74c954b67

Contents?: true

Size: 1015 Bytes

Versions: 13

Compression:

Stored size: 1015 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

  class << self
    attr_writer :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_method :[]=, :store

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

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

  alias_method :[], :fetch

  protected

  attr_reader :client, :default

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cistern-2.8.0 lib/cistern/data/redis.rb
cistern-2.7.2 lib/cistern/data/redis.rb
cistern-2.7.1 lib/cistern/data/redis.rb
cistern-2.7.0 lib/cistern/data/redis.rb
cistern-2.6.0 lib/cistern/data/redis.rb
cistern-2.5.0 lib/cistern/data/redis.rb
cistern-2.4.1 lib/cistern/data/redis.rb
cistern-2.4.0 lib/cistern/data/redis.rb
cistern-2.3.0 lib/cistern/data/redis.rb
cistern-2.2.7 lib/cistern/data/redis.rb
cistern-2.2.6 lib/cistern/data/redis.rb
cistern-2.2.5 lib/cistern/data/redis.rb
cistern-2.2.4 lib/cistern/data/redis.rb