class Memcached def fetch(key) if value = get(key) value elsif block_given? begin add(key, value = yield) rescue Memcached::NotStored => e value = get(key) end value end end end