module SimpleRedis

Constants

DEFAULT_DB
HOST
NORMAL_DATA_TYPES
PORT
VERSION

Attributes

current_opts[RW]
current_redis[RW]
default_db[RW]
host[RW]
port[RW]

Public Class Methods

cache(redis, key, value) click to toggle source

PRIVATE METHODS

# File lib/simple_redis.rb, line 45
def self.cache(redis, key, value)
  redis.set key, normalize(value)
  value
end
configuration() { |self| ... } click to toggle source
# File lib/simple_redis.rb, line 12
def self.configuration
  yield self
end
delete_matched(key, opts={}) click to toggle source
# File lib/simple_redis.rb, line 39
def self.delete_matched(key, opts={})
  redis = get_redis(opts)
  redis.del(*redis.keys(key)) rescue 0
end
fetch(opts={}) { |: opts))| ... } click to toggle source
# File lib/simple_redis.rb, line 16
def self.fetch(opts={})
  redis = get_redis(opts)
  result = redis.get opts[:key]
  get_result(result || cache(redis, opts[:key], block_given? ? yield : opts[:value]))
end
get(key, opts={}) click to toggle source
# File lib/simple_redis.rb, line 28
def self.get(key, opts={})
  redis = get_redis(opts)
  result = redis.get key
  get_result(result)
end
get_db(opts={}) click to toggle source
# File lib/simple_redis.rb, line 75
def self.get_db(opts={})
  opts[:db] || default_db || DEFAULT_DB
end
get_host(opts={}) click to toggle source
# File lib/simple_redis.rb, line 67
def self.get_host(opts={})
  opts[:host] || host || HOST
end
get_port(opts={}) click to toggle source
# File lib/simple_redis.rb, line 71
def self.get_port(opts={})
  opts[:port] || port || PORT
end
get_redis(opts={}) click to toggle source
# File lib/simple_redis.rb, line 58
def self.get_redis(opts={})
  current_opts.eql?(opts) ? current_redis : new_redis(opts)
end
get_result(redis_result) click to toggle source
# File lib/simple_redis.rb, line 54
    def self.get_result(redis_result)
      begin eval(redis_result) rescue redis_result end
    end

    def self.get_redis(opts={})
      current_opts.eql?(opts) ? current_redis : new_redis(opts)
    end

    def self.new_redis(opts={})
      current_opts = opts
      current_redis = Redis.new(host: get_host(opts), port: get_port(opts), db: get_db(opts))
    end

    def self.get_host(opts={})
      opts[:host] || host || HOST
    end

    def self.get_port(opts={})
      opts[:port] || port || PORT
    end

    def self.get_db(opts={})
      opts[:db] || default_db || DEFAULT_DB
    end
    private_class_method :cache, :normalize, :get_result, :get_redis, :new_redis,
      :get_host, :get_port, :get_db

end
new_redis(opts={}) click to toggle source
# File lib/simple_redis.rb, line 62
def self.new_redis(opts={})
  current_opts = opts
  current_redis = Redis.new(host: get_host(opts), port: get_port(opts), db: get_db(opts))
end
normalize(value) click to toggle source
# File lib/simple_redis.rb, line 50
def self.normalize(value)
  value.class.in?(NORMAL_DATA_TYPES) ? value : value.to_json
end
set(key, value, opts={}) click to toggle source
# File lib/simple_redis.rb, line 22
def self.set(key, value, opts={})
  redis = get_redis(opts)
  result = cache(redis, key, value)
  get_result(result)
end
total_matches(key, opts={}) click to toggle source
# File lib/simple_redis.rb, line 34
def self.total_matches(key, opts={})
  redis = get_redis(opts)
  redis.keys(key).size
end