Support single-value cache block synax
aprescott opened this issue · comments
Sometimes you don't need a full key-value store, you just want to cache a single value. It would be nice to have a simple way to do that.
@cached_value ||= TimeoutCache.value do
"foo"
end
The only way to do that with the existing syntax is:
@cache ||= TimeoutCache.new
value = @cache[:foo]
return value if value
@cache[:foo] = expensive_operation
The ||=
part might be misleading. If there's a way to uniquely identify the caller, it would be cool to instead have:
def foo
TimeoutCache.value do
expensive_operation
end
end
Which would, I suppose, require a class instance of TimeoutCache
.