Sha256: a36b74ee49cabc2a7305b70f8d5deb08401c3eb926ccc949cbe6628f8abf98dd

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module Cachetastic
  module Adapters
    class Redis < Cachetastic::Adapters::Base

      def initialize(klass)
        define_accessor(:redis_host)
        define_accessor(:redis_options)
        define_accessor(:delete_delay)
        self.redis_host ||= "redis://localhost:6379/"
        self.redis_options = ::Redis::Client::DEFAULTS.merge({
          db: "cachetastic",
          url: self.redis_host
        })
        super(klass)
        self.marshal_method = :yaml if self.marshal_method == :none
        connection
      end

      def get(key) # :nodoc:
        connection.get(transform_key(key))
      end # get

      def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) # :nodoc:
        connection.setex(transform_key(key), expiry_time, marshal(value))
        return value
      end # set

      def delete(key) # :nodoc:
        connection.del(transform_key(key))
      end # delete

      def expire_all # :nodoc:
        connection.flushdb
        return nil
      end # expire_all

      def transform_key(key) # :nodoc:
        key.to_s.hexdigest
      end

      def valid?
        !connection.nil?
      end

      private
      def connection
        if @connection.nil?
         @connection = ::Redis.new(self.redis_options)
        end
        return @connection
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cachetastic-3.5.2 lib/cachetastic/adapters/redis.rb
cachetastic-3.5.1 lib/cachetastic/adapters/redis.rb