Sha256: 3028c299c4d44706124eea58c36ea3ef14c6182e8e42e8ca61dbac548ae9db3f

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

require 'rubygems'
require 'redis'

module Lawnchair
  
  class << self
    attr_reader :redis
    
    def connectdb(redis=nil)
      @redis ||= Redis.new(:db => 11)
    end

    def flushdb
      redis.flushdb
    end
  end
  
  class Cache
    def self.me(options = {}, &block)
      raise "Cache key please!" unless options.has_key?(:key)
      
      if exists?(options[:key])
        Marshal.load(Lawnchair.redis[compute_key(options[:key])])
      else
        val = block.call
        expires_in = compute_expiry(options[:expires_in])
        Lawnchair.redis.set(compute_key(options[:key]), Marshal.dump(val), expires_in)
        return val
      end
    end
    
    def self.compute_key(key)
      "Lawnchair:#{key}"
    end
    
    def self.expire(key)
      Lawnchair.redis.del compute_key(key)
    end
    
    def self.exists?(key)
      return !!Lawnchair.redis[compute_key(key)]
    end
    
    def self.compute_expiry(seconds)
      seconds || 3600
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lawnchair-0.3.1 lib/lawnchair.rb