Skip to content

Commit

Permalink
dumped JsonRedis, (en|de)code where necessary. added push/pop and som…
Browse files Browse the repository at this point in the history
…e other useful methods. refactored #write
  • Loading branch information
oleriesenberg committed Feb 16, 2011
1 parent a29383e commit d475047
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 94 deletions.
14 changes: 0 additions & 14 deletions lib/redis/distributed_json_redis.rb

This file was deleted.

4 changes: 0 additions & 4 deletions lib/redis/distributed_marshaled_redis.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/redis/json_redis.rb

This file was deleted.

4 changes: 0 additions & 4 deletions lib/redis/marshaled_redis.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/redis/redis_factory.rb
Expand Up @@ -5,9 +5,9 @@ class << self
def create(*addresses) def create(*addresses)
addresses = extract_addresses(addresses) addresses = extract_addresses(addresses)
if addresses.size > 1 if addresses.size > 1
DistributedMarshaledRedis.new addresses Redis::Distributed.new addresses
else else
JsonRedis.new addresses.first || {} Redis.new addresses.first || {}
end end
end end


Expand Down
14 changes: 3 additions & 11 deletions lib/redis_buddy.rb
@@ -1,11 +1,3 @@
require 'redis' module RedisBuddy
require 'redis/distributed' # Your code goes here...
require 'redis/namespace' end

require 'redis/redis_factory'
require 'redis/json_redis'
require 'redis/distributed_json_redis'
require 'redis/marshaled_redis'
require 'redis/distributed_marshaled_redis'

require 'redis_buddy/cache_store'
99 changes: 69 additions & 30 deletions lib/redis_buddy/cache_store.rb
Expand Up @@ -4,60 +4,99 @@ class RedisBuddyStore < Store
def initialize(*adresses) def initialize(*adresses)
ns = Rails.application.class.to_s.split('::').first.downcase ns = Rails.application.class.to_s.split('::').first.downcase
@r = RedisFactory.create(adresses) @r = RedisFactory.create(adresses)
@data = Redis::Namespace.new(ns, :redis => @r) @client = Redis::Namespace.new(ns, :redis => @r)
end end


def write(key, value, options = nil) def write(key, value, options = {})
method = options && options[:unless_exist] ? :set_unless_exists : :set method = :getset if options[:return]
method = :setnx if options[:nx]
method, ttl = :setex, options[:ttl] if options[:ttl]
method = :set unless method
value = encode(value)


if options && options[:expire] ttl ? @client.send(method, key, ttl, value) : @client.send(method, key, value)
if (info.is_a?(Array) ? info.first['redis_version'] : info['redis_version']) <= '1.2.6' end
@data.send(:set, key, value)
@data.send(:expire, key, options[:expire]) def push(key, value)
else @client.lpush(key, encode(value))
@data.send(:setex, key, value,options[:expire]) end
end
else def increment(key, amount = nil)
@data.send(method, key, value) amount.nil? ? @client.incr(key) : @client.incrby(key, amount)
end end

def decrement(key, amount = nil)
amount.nil? ? @client.decr(key) : @client.decrby(key, amount)
end

def expire_in(key, ttl)
@client.expire(key, ttl)
end

def exipire_at(key, time)
@client.exipireat(key, time)
end end


def read(key) def read(key)
@data.get(key) value = @client.get(key)
value ? decode(value) : nil
end end


def ttl(key) def pop(key)
@data.ttl(key) value = @client.rpop(key)
value ? decode(value) : nil
end end


def delete(key) def length(key)
@data.del(key) @client.llen(key)
end end


def exist?(key) def ttl(key)
@data.exists(key) @client.ttl(key)
end end


def increment(key, amount = nil) def type(key)
amount.nil? ? @data.incr(key) : @data.incrby(key, amount) @client.type(key)
end end


def decrement(key, amount = nil) def exists?(key)
amount.nil? ? @data.decr(key) : @data.decrby(key, amount) @client.exists(key)
end end


def delete_matched(matcher) def delete(key)
super do @client.del(key)
@data.keys(matcher).each { |key| @data.del(key) }
end
end end


def clear def clear
@data.flushdb @client.flushdb
end end


def info def info
@data.info @client.info
end

def keys(pattern = "*")
@client.keys(pattern)
end

def client
@client
end

private
def decode(value)
value = Yajl.load(value)
value.with_indifferent_access if value.is_a?(Hash)
value
end

def encode(value)
return value unless encode?(value)
Yajl.dump(value)
end

def encode?(value)
!value.is_a?(Integer)
end end
end end
end end
Expand Down
3 changes: 3 additions & 0 deletions lib/redis_buddy/version.rb
@@ -0,0 +1,3 @@
module RedisBuddy
VERSION = "0.2.1"
end

0 comments on commit d475047

Please sign in to comment.