зеркало из https://github.com/github/memcached.git
add fetch
This commit is contained in:
Родитель
94f9188312
Коммит
2bad783a7c
|
@ -98,6 +98,17 @@ class Memcached
|
|||
set(key, value, options[:ttl] || @default_ttl, options[:raw])
|
||||
end
|
||||
|
||||
def fetch(key, options={})
|
||||
result = read(key, options)
|
||||
if result.nil?
|
||||
result = yield
|
||||
write(key, result, options)
|
||||
result
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
# Wraps Memcached#add so that it doesn't raise.
|
||||
def add(key, value, ttl=@default_ttl, raw=false)
|
||||
super(key, value, ttl, !raw)
|
||||
|
|
|
@ -175,6 +175,21 @@ class RailsTest < Test::Unit::TestCase
|
|||
assert_equal @value, result
|
||||
end
|
||||
|
||||
def test_fetch
|
||||
# returns
|
||||
assert_equal @value, @cache.fetch("x"){ @value }
|
||||
|
||||
# sets
|
||||
assert_equal @value, @cache.read("x")
|
||||
|
||||
# reads
|
||||
assert_equal @value, @cache.fetch("x"){ 1 }
|
||||
|
||||
# works with options
|
||||
@cache.expects(:write).with("y", 1, :foo => :bar)
|
||||
@cache.fetch("y", :foo => :bar){ 1 }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def key
|
||||
|
|
Загрузка…
Ссылка в новой задаче