Sha256: 17ad5adfc0d9d17b3274438b4b246dd2d997e28b3e144ed674c680e411d74139

Contents?: true

Size: 699 Bytes

Versions: 2

Compression:

Stored size: 699 Bytes

Contents

# A simple LRU disk file cache
require 'lrucache'
require 'fileutils'
require 'securerandom'

class DiskFileCache
  def initialize(path, size=1000)
    @path = File.expand_path(path).chomp('/')
    @cache = LRUCache.new(
      :max_size => size,
      :eviction_handler => lambda { |path| delete_file(path) }
    )
  end

  def fetch(key)
    path = @cache[key]

    return path if path

    ext = File.extname(key)

    path = tmp_file(ext)
    yield(path) if block_given?
    @cache[key] = path

    return path
  end

  def tmp_file(ext)
    "#{@path}/#{SecureRandom.hex}#{ext}"
  end

  private
  def delete_file(path)
    # Delete the file since it was evicted
    FileUtils.rm(path)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volt-image_resizer-0.1.1 app/image_resizer/lib/disk_file_cache.rb
volt-image_resizer-0.1.0 app/s3_image_resizer/lib/disk_file_cache.rb