Sha256: 14e763a300947b616055c8d23d17848f8593c49e76eb7932b70221bfb884747a
Contents?: true
Size: 1.85 KB
Versions: 6
Compression:
Stored size: 1.85 KB
Contents
require 'uri' require 'fileutils' require 'tmpdir' module Merb module Caching module Store class FileCache def initialize(name = "cache", keepalive = nil) @path = File.join(MERB_ROOT, name) @keepalive = keepalive FileUtils.mkdir_p(@path, :mode => 0700) end def []=(k,v) fn = File.join(@path, escape_filename(k.to_s) ) encode_file(fn, v) end alias :put :[]= alias :write :[]= def [](k) fn = File.join(@path, escape_filename(k.to_s) ) return nil unless File.exists?(fn) decode_file(fn) end alias :get :[] alias_method :read, :[] def delete(k) f = File.join(@path, escape_filename(k.to_s)) File.delete(f) if File.exists?(f) end def gc! return unless @keepalive now = Time.now all.each do |fn| expire_time = File.stat(fn).atime + @keepalive File.delete(fn) if now > expire_time end end private def decode_file(fn) val = nil File.open(fn,"rb") do |f| f.flock(File::LOCK_EX) val = Marshal.load( f.read ) f.flock(File::LOCK_UN) end return val end def encode_file(fn, value) File.open(fn, "wb") do |f| f.flock(File::LOCK_EX) f.chmod(0600) f.write(Marshal.dump(value)) f.flock(File::LOCK_UN) end end def all Dir.glob( File.join(@path, '*' ) ) end # need this for fat filesystems def escape_filename(fn) URI.escape(fn, /["\/:;|=,\[\]]/) end end end end end
Version data entries
6 entries across 6 versions & 1 rubygems