This commit is contained in:
grosser 2012-07-04 22:06:21 -07:00
Родитель 94f9188312
Коммит 2bad783a7c
2 изменённых файлов: 26 добавлений и 0 удалений

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

@ -98,6 +98,17 @@ class Memcached
set(key, value, options[:ttl] || @default_ttl, options[:raw]) set(key, value, options[:ttl] || @default_ttl, options[:raw])
end 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. # Wraps Memcached#add so that it doesn't raise.
def add(key, value, ttl=@default_ttl, raw=false) def add(key, value, ttl=@default_ttl, raw=false)
super(key, value, ttl, !raw) super(key, value, ttl, !raw)

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

@ -175,6 +175,21 @@ class RailsTest < Test::Unit::TestCase
assert_equal @value, result assert_equal @value, result
end 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 private
def key def key