Sha256: fd2d3540d5083faa400ae961a07b40ccf89ced0a80f8c7dd4407d22043b77d55
Contents?: true
Size: 1.29 KB
Versions: 4
Compression:
Stored size: 1.29 KB
Contents
module BasicCache ## # Time-based cache object class TimeCache < Cache attr_reader :lifetime ## # Generate an empty store, with a default lifetime of 60 seconds def initialize(lifetime = 30) @lifetime = lifetime @cache_item = Struct.new(:stamp, :value) super() end ## # If the key is cached but expired, clear it def cache(key = nil, &code) key ||= BasicCache.get_caller key = key.to_sym # rubocop:disable AndOr unless @store.include? key and Time.now - @store[key].stamp < @lifetime @store[key] = @cache_item.new(Time.now, code.call) end # rubocop:enable AndOr @store[key].value end ## # Check if a value is cached and not expired def include?(key = nil) key ||= BasicCache.get_caller key = key.to_sym # rubocop:disable AndOr @store.include? key and Time.now - @store[key].stamp < @lifetime # rubocop:enable AndOr end def [](key = nil) super.value end ## # Return the size of the cache (don't include expired entries) # By default, purges expired entries while iterating def size(purge = true) valid = @store.select { |k, v| Time.now - v.stamp < @lifetime } @store = valid if purge valid.size end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
basiccache-0.0.24 | lib/caches/timecache.rb |
basiccache-0.0.22 | lib/caches/timecache.rb |
basiccache-0.0.21 | lib/caches/timecache.rb |
basiccache-0.0.20 | lib/caches/timecache.rb |