Commit 211d8eae authored by Rajko Albrecht's avatar Rajko Albrecht

Refactoring class

parent 1c88ba0e
......@@ -4,9 +4,7 @@ require 'redis/store-with-cas'
class Redis
class Store < self
class StoreWithCas < self
end
class StoreWithCas < Store
end
end
......@@ -7,10 +7,9 @@ require 'redis/store/namespace'
require 'redis/store_with_cas/namespace'
class Redis
class Store < self
class StoreWithCas < self
include Cas
end
class StoreWithCas < Store
include Cas
end
end
end
class Redis
class Store
class StoreWithCas < self
class StoreWithCas < Store
module Cas
module Cas
def cas key, ttl=nil
watch(key) do
break false unless exists(key)
value = get(key)
value = yield value
ires = multi do |multi|
multi.set(key,value,{:expire_in => ttl})
end
return_value ires
def cas key, ttl=nil
watch(key) do
break false unless exists(key)
value = get(key)
value = yield value
ires = multi do |multi|
multi.set(key,value,{:expire_in => ttl})
end
return_value ires
end
end
def cas_multi *keys
#options = extract_options keys
return if keys.empty?
watch(*keys) do
values = read_multi(*keys)
options = extract_options keys
valuehash = yield values
result = {}
def cas_multi *keys
#options = extract_options keys
return if keys.empty?
watch(*keys) do
values = read_multi(*keys)
options = extract_options keys
valuehash = yield values
result = {}
v = valuehash.map do |name,value|
ires = multi do |multi|
multi.set(name,value,options) if values.key?(name)
end
result[name] = value if return_value(ires)
v = valuehash.map do |name,value|
ires = multi do |multi|
multi.set(name,value,options) if values.key?(name)
end
true
result[name] = value if return_value(ires)
end
true
end
end
def read_multi *keys
options = extract_options keys
keys = keys.select {|k| exists(k)}
return {} if keys.empty?
values = mget(*(keys+[options]))
resulthash = {}
keys.zip(values) { |a,b| resulthash[a.to_s] = b} unless values.nil?
resulthash
end
def read_multi *keys
options = extract_options keys
keys = keys.select {|k| exists(k)}
return {} if keys.empty?
values = mget(*(keys+[options]))
resulthash = {}
keys.zip(values) { |a,b| resulthash[a.to_s] = b} unless values.nil?
resulthash
end
private
private
def return_value result
result.is_a?(Array) && result[0] == 'OK'
end
def return_value result
result.is_a?(Array) && result[0] == 'OK'
end
def extract_options array
if array.last.is_a?(Hash) && array.last.instance_of?(Hash)
array.pop
else
{}
end
def extract_options array
if array.last.is_a?(Hash) && array.last.instance_of?(Hash)
array.pop
else
{}
end
end
end
end
end
require 'redis/store'
class Redis
class Store < self
class StoreWithCas < self
VERSION = '0.0.1'
end
class StoreWithCas < Store
VERSION = '0.0.1'
end
end
......@@ -4,7 +4,7 @@ require 'redis/store_with_cas/version'
Gem::Specification.new do |s|
s.name = 'redis-store-with-cas'
s.version = Redis::Store::StoreWithCas::VERSION
s.version = Redis::StoreWithCas::VERSION
s.authors = ['Rajko Albrecht']
s.email = ['ral@alwins-world.de']
s.homepage = 'http://redis-store.org/redis-store'
......
require 'test_helper'
describe Redis::Store::StoreWithCas do
describe Redis::StoreWithCas do
def setup
@store = Redis::Store::StoreWithCas.new :namespace => 'storetest'
@store = Redis::StoreWithCas.new :namespace => 'storetest'
end
def teardown
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment