Sha256: fee97c6d57ddcc7c94d7aeb798687b774754a5a90201e624f87722f4d0ade3ed

Contents?: true

Size: 902 Bytes

Versions: 2

Compression:

Stored size: 902 Bytes

Contents

class DiskStore
  class Reaper
    module LRU
      def files_to_evict
        # Collect and sort files based on last access time
        sorted_files = files
          .map { |file|
            data = nil
            File.new(file, 'rb').tap { |fd|
              data = { path: file, last_fetch: fd.atime, size: fd.size }
            }.close
            data
          }
          .sort { |a, b| a[:last_fetch] <=> b[:last_fetch] } # Oldest first

        # Determine which files to evict
        space_to_evict = current_cache_size - maximum_cache_size
        space_evicted = 0
        evictions = []
        while space_evicted < space_to_evict
          evicted_file = sorted_files.shift
          space_evicted += evicted_file[:size]
          evictions << evicted_file
        end

        evictions
      end

      def directories_to_evict
        empty_directories
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
disk_store-0.2.1 lib/disk_store/eviction_strategies/lru.rb
disk_store-0.2.0 lib/disk_store/eviction_strategies/lru.rb