Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruby-redis
redis-activesupport-with-cas
Commits
d80c8322
Commit
d80c8322
authored
Jul 26, 2017
by
Rajko Albrecht
Browse files
* activesupport cache with cas first implemented
* tests for single cas
parent
aa5621bf
Changes
4
Hide whitespace changes
Inline
Side-by-side
lib/active_support/cache/redis_store_with_cas.rb
0 → 100644
View file @
d80c8322
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
lib/active_support/redis-activesupport-with-cas.rb
0 → 100644
View file @
d80c8322
require
'redis-store-with-cas'
require
'active_support'
require
'active_support/cache/redis_store_with_cas'
test/active_support/cache/redis_store_with_cas_test.rb
0 → 100644
View file @
d80c8322
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
test/active_support/test_helper.rb
0 → 100644
View file @
d80c8322
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment