lib/datacenter/cache.rb in datacenter-0.2.0 vs lib/datacenter/cache.rb in datacenter-0.2.1
- old
+ new
@@ -5,33 +5,31 @@
@expiration_time = expiration_time
@data = {}
end
def fetch(key, &block)
- set key, &block if !key?(key) || expired?(key)
+ set key, block.call if !data.key?(key) || expired?(key)
get key
end
private
+ attr_reader :data, :expiration_time
+
def get(key)
- @data[key][:value]
+ data[key][:value]
end
- def set(key, &block)
- @data[key] = {
- value: block.call,
+ def set(key, value)
+ data[key] = {
+ value: value,
fetched_at: Time.now
}
end
- def key?(key)
- @data.key? key
- end
-
def expired?(key)
- return false unless @expiration_time
- Time.now >= @data[key][:fetched_at] + @expiration_time
+ return false unless expiration_time
+ Time.now >= data[key][:fetched_at] + expiration_time
end
end
end
\ No newline at end of file