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