Sha256: c383a7be344b43422482923d4d96d8347eedc72bdd0140350a10c248d37edcb8

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Modl
  module Parser
    # Store any files for up to 1 hour by default.
    class ObjectCache
      # A cache record to keep track of the time since an object was last cached.
      class CacheEntry
        TTL_ONE_HOUR = 3_600 # seconds

        attr_reader :object

        # Initialiase the CacheEntry with an object and an optional ttl in seconds (default 1 hour)
        def initialize(object, ttl = nil)
          ttl = TTL_ONE_HOUR if ttl.nil?
          @object = object
          @expiry_time = Time.now + ttl
        end

        # Check whether the CacheEntry is live
        def expired?
          @expiry_time < Time.now
        end
      end

      # Set up and empty cache.
      def initialize
        @cache = {}
      end

      # Cache an object with the given key and optional ttl in seconds (default 1 hour)
      def put(key, object, ttl = nil)
        @cache[key] = CacheEntry.new(object, ttl) unless key.nil? || object.nil?
      end

      # Evict a cache entry
      def evict(key)
        @cache.delete(key) unless key.nil?
      end

      # Return the object with the given key if one exists and has not expired.
      def get(key)
        # Return nothing if not in the cache or it has expired.
        return if key.nil?

        entry = @cache[key]
        return unless entry
        return if entry.expired?

        # Otherwise return the cached object.
        entry.object
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
modl-0.3.2 lib/modl/parser/object_cache.rb
modl-0.3.1 lib/modl/parser/object_cache.rb
modl-0.3.0 lib/modl/parser/object_cache.rb