Commit d80c8322 authored by Rajko Albrecht's avatar Rajko Albrecht
Browse files

* activesupport cache with cas first implemented

* tests for single cas
parent aa5621bf
require 'active_support'
require 'redis-store-with-cas'
require 'active_support/cache/redis_store'
module ActiveSupport
module Cache
module RedisStoreCas
attr_accessor :read_only
def cas name,options=nil
options = merged_options(options)
key = normalize_key(name, options)
instrument(:cas, name, options) do
ttl = cas_expiration options
@data.cas(key,ttl) do |entry|
value = yield entry.value
break true if read_only
options[:raw].present? ? value : Entry.new(value, options)
end
end
end
def cas_muli
end
private
def cas_expiration(options)
if options[:expires_in].present? && options[:race_condition_ttl].present? && options[:raw].blank?
options[:expires_in].to_f + options[:race_condition_ttl].to_f
else
nil
end
end
end
class RedisStoreWithCas < RedisStore
def initialize(*adresses)
super adresses
check_and_extend_cas
end
def candocas?
@data.is_a?(Redis::Store) && @data.respond_to?(:cas)
end
private
def check_and_extend_cas
extend RedisStoreCas if candocas?
end
end
end
end
require 'redis-store-with-cas'
require 'active_support'
require 'active_support/cache/redis_store_with_cas'
require 'active_support/test_helper'
require 'ostruct'
describe "ActiveSupport::Cache::RedisStoreWithCas" do
def setup
@store = ActiveSupport::Cache::RedisStoreWithCas.new "redis://127.0.0.1:6379/5/cachetest"
@dstore = ActiveSupport::Cache::RedisStoreWithCas.new "redis://127.0.0.1:6379/5", "redis://127.0.0.1:6379/6"
@rabbit = OpenStruct.new :name => "bunny"
@white_rabbit = OpenStruct.new :color => "white"
@black_rabbit = OpenStruct.new :color => 'black'
end
def teardown
@store.instance_variable_get("@data").flushdb
@dstore.instance_variable_get("@data").flushdb
end
describe "Single cas " do
it "should not swap missing key" do
refute @store.cas('rabbit') { |_value| flunk }
end
it "should correct swap value" do
@store.write "rabbit", @rabbit
assert(@store.cas('rabbit') do |value|
assert_equal @rabbit, value
@white_rabbit
end)
@store.read("rabbit").must_equal(@white_rabbit)
end
it "should not swap if value changes" do
@store.write('rabbit', @rabbit)
refute @store.cas('rabbit') { |_value|
@store.write('rabbit', @black_rabbit)
@white_rabbit
}
@store.read("rabbit").must_equal(@black_rabbit)
end
end
end
\ No newline at end of file
require 'bundler/setup'
require 'minitest/autorun'
require 'mocha/setup'
require 'active_support'
require 'active_support/cache/redis_store_with_cas'
puts "Testing against ActiveSupport v.#{ActiveSupport::VERSION::STRING}"
\ No newline at end of file
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