Merge pull request #127 from joe1chen/memcached-rails-fix-write-unless-exist

Add support for Memcached::Rails write option :unless_exist
This commit is contained in:
Evan Weaver 2013-06-13 23:18:53 -07:00
Родитель d632ce881e 86a802069d
Коммит 7070aba1b6
2 изменённых файлов: 25 добавлений и 1 удалений

Просмотреть файл

@ -97,7 +97,13 @@ class Memcached
# Alternative to #set. Accepts a key, value, and an optional options hash supporting the
# options :raw and :ttl.
def write(key, value, options = {})
set(key, value, options[:ttl] || options[:expires_in] || @default_ttl, options[:raw])
value = value.to_s if options && options[:raw]
ttl = options[:ttl] || options[:expires_in] || @default_ttl
if options && options[:unless_exist]
add(key, value, ttl, options[:raw])
else
set(key, value, ttl, options[:raw])
end
end
def fetch(key, options={})

Просмотреть файл

@ -196,6 +196,24 @@ class RailsTest < Test::Unit::TestCase
@cache.write key, @value, :expires_in => 123
end
def test_write_with_unless_exist
@cache.write key, @value, :unless_exist => true
result = @cache.get key
assert_equal @value, result
rand_value = "rand-value-#{rand}"
@cache.write key, rand_value, :unless_exist => true
result = @cache.get key
assert_equal @value, result
end
def test_write_with_fixnum_and_raw
@cache.write key, 1, :raw => true
result = @cache.read key, :raw => true
assert_equal "1", result
end
def test_add_with_duration
@cache.add key, @value, @duration
result = @cache.get key