Sha256: f3b4e6c4485c6d075565302d222a7a954c4c33fa54b016016563d112065023f8

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require 'speed_gun/store/base'
require 'fileutils'

class SpeedGun::Store::File
  DEFAULT_PATH = '/tmp/speed_gun'
  DEFAULT_EXPIRES_IN_SECONDS = 60 * 60 * 24
  CLEANUP_INTERVAL = 60 * 60

  def initialize(options = {})
    @path = (options[:path] || DEFAULT_PATH).to_s
    @expires = options[:expires] || DEFAULT_EXPIRES_IN_SECONDS
    @lock = Mutex.new

    this = self
    Thread.new do
      begin
        while true
          this.cleanup
          sleep(CLEANUP_INTERVAL)
        end
      rescue
      end
    end
  end

  def [](id)
    @lock.synchronize {
      read(id) if exist?(id)
    }
  end

  def []=(id, val)
    @lock.synchronize {
      write(id, val)
    }
  end

  def cleanup
    @lock.synchronize {
      files = Dir.entries(@path)
      files.each do |file|
        file = File.join(@path, file)
        File.delete(file) if (Time.now - File.mtime(file)) > @expires
      end
    }
  end

  private

  def read(id)
    File.open(File.join(@path, id), 'rb') { |f| f.read }
  end

  def write(id, val)
    FileUtils.mkdir_p(@path)
    File.open(File.join(@path, id), 'wb+') { |f| f.write(val) }
  end

  def exist?(id)
    File.exist?(File.join(@path, id))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
speed_gun-0.0.4 lib/speed_gun/store/file.rb
speed_gun-0.0.3 lib/speed_gun/store/file.rb
speed_gun-0.0.2 lib/speed_gun/store/file.rb
speed_gun-0.0.1 lib/speed_gun/store/file.rb