Sha256: 5f9d16b8ad51f758e112f7757b414792dc55f3910f0fb7f1ca957773e85d6871

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

##
# Extends BasicCache to add a time-based cache
module BasicCache
  ##
  # Timecache item struct, timestamp and value

  TimeCacheItem = Struct.new(:stamp, :value)

  ##
  # Time-based cache object
  class TimeCache < Cache
    attr_reader :lifetime

    ##
    # Generate an empty store, with a default lifetime of 60 seconds

    def initialize(params = {})
      params = { store: params } unless params.is_a? Hash
      @lifetime = params.fetch :lifetime, 60
      super
    end

    ##
    # Return the size of the cache (don't include expired entries)

    def size
      @store.keys.count { |k| Time.now - @store[k].stamp < @lifetime }
    end

    ##
    # Return a value from the cache, or calculate it and store it
    # Recalculate if the cached value has expired

    def cache(key = nil, &code)
      key ||= BasicCache.caller_name
      key = key.to_sym
      if include? key
        @store[key].value
      else
        value = code.call
        @store[key] = TimeCacheItem.new Time.now, value
        value
      end
    end

    ##
    # Check if a value is cached and not expired

    def include?(key = nil)
      key ||= BasicCache.caller_name
      key = key.to_sym
      @store.include?(key) && Time.now - @store[key].stamp < @lifetime
    end

    ##
    # Retrieve a value

    def [](key = nil)
      super.value
    end

    ##
    # Remove a value, or clear the cache

    def clear!(key = nil)
      resp = super
      resp.class == TimeCacheItem ? resp.value : resp
    end

    ##
    # Prune expired keys

    def prune
      @store.keys.reject { |k| include? k }.map { |k| clear!(k) && k }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basiccache-1.0.0 lib/basiccache/caches/timecache.rb
basiccache-0.2.2 lib/basiccache/caches/timecache.rb
basiccache-0.2.1 lib/basiccache/caches/timecache.rb