Sha256: 9b672ff90ffd6559e9bd5975f572116defc2b021f288f4d641facd4ca9a076a7

Contents?: true

Size: 926 Bytes

Versions: 16

Compression:

Stored size: 926 Bytes

Contents

require "digest/md5"
require "fileutils"

module Ruco
  class FileStore
    def initialize(folder, options)
      @folder = folder
      @options = options
    end

    def set(key, value)
      FileUtils.mkdir_p @folder unless File.exist? @folder
      File.write(file(key), serialize(value))
      cleanup
    end

    def get(key)
      file = file(key)
      deserialize File.binary_read(file) if File.exist?(file)
    end

    private

    def entries
      (Dir.entries(@folder) - ['.','..']).
        map{|entry| File.join(@folder, entry) }.
        sort_by{|file| File.mtime(file) }
    end

    def cleanup
      delete = entries[0...-@options[:keep]] || []
      delete.each{|f| File.delete(f) }
    end

    def file(key)
      "#{@folder}/#{Digest::MD5.hexdigest(key)}.yml"
    end

    def serialize(value)
      Marshal.dump(value)
    end

    def deserialize(value)
      Marshal.load(value)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruco-0.2.0.beta2 lib/ruco/file_store.rb
ruco-0.1.14 lib/ruco/file_store.rb
ruco-0.1.13 lib/ruco/file_store.rb
ruco-0.1.12 lib/ruco/file_store.rb
ruco-0.1.11 lib/ruco/file_store.rb
ruco-0.1.10 lib/ruco/file_store.rb
ruco-0.1.9 lib/ruco/file_store.rb
ruco-0.1.8 lib/ruco/file_store.rb
ruco-0.1.7 lib/ruco/file_store.rb
ruco-0.1.6 lib/ruco/file_store.rb
ruco-0.1.5 lib/ruco/file_store.rb
ruco-0.1.4 lib/ruco/file_store.rb
ruco-0.1.3 lib/ruco/file_store.rb
ruco-0.1.2 lib/ruco/file_store.rb
ruco-0.1.1 lib/ruco/file_store.rb
ruco-0.1.0 lib/ruco/file_store.rb