Permalink
Browse files

a huge speed increase and total re-write of the BasicFile store system

Signed-off-by: Yehuda Katz <wycats@gmail.com>
  • Loading branch information...
1 parent 84cbb50 commit 463e10eb543a7ba7714efe3b1842f7d2c45407c3 @hcatlin hcatlin committed with Jun 7, 2009
Showing with 66 additions and 25 deletions.
  1. +66 −25 lib/moneta/basic_file.rb
View
@@ -3,59 +3,100 @@
# by Hampton Catlin
#
# This cache simply uses a directory that it creates
-# and manages to keep your file stores. If you don't need
-# expiration make sure to turn it off, because it requires
-# an extra file open.
-#
-# This will create two folders.. one for the data and one
-# for the expiration objects.
+# and manages to keep your file stores.
#
+# Specify :skip_expires => true if you aren't using
+# expiration as this will slightly decrease your file size
+# and memory footprint of the library
#
# You can optionally also specify a :namespace
-# option that will name the two folders with that namespace
-# That way, you can have two entire different caches in the
-# same base path.
+# option that will create a subfolder.
#
require 'fileutils'
require File.join(File.dirname(__FILE__), "..", "moneta.rb")
-require File.join(File.dirname(__FILE__), "file")
module Moneta
class BasicFile
include Defaults
- include Moneta::File::Implementation
-
+
def initialize(options = {})
@namespace = options[:namespace]
@directory = ::File.join(options[:path], @namespace.to_s)
- unless options[:skip_expires]
- @expiration_directory = ::File.join(options[:path], [@namespace, "expiration"].compact.join("_"))
- setup_expiration_cache
- self.extend(StringExpires)
- end
+ @expires = !options[:skip_expires]
ensure_directory_created(@directory)
end
- # Override default behaviour to create subdirectories
+ def key?(key)
+ !self[key].nil?
+ end
+
+ alias has_key? key?
+
+ def [](key)
+ if ::File.exist?(path(key))
+ data = raw_get(key)
+ if @expires
+ if data[:expires_at].nil? || data[:expires_at] > Time.now
+ data[:value]
+ else
+ delete!(key)
+ end
+ end
+ end
+ end
+
+ def raw_get(key)
+ Marshal.load(::File.read(path(key)))
+ end
+
def []=(key, value)
+ store(key, value)
+ end
+
+ def store(key, value, options = {})
ensure_directory_created(::File.dirname(path(key)))
- super
+ ::File.open(path(key), "w") do |file|
+ if @expires
+ data = {:value => value}
+ if options[:expires_in]
+ data[:expires_at] = Time.now + options[:expires_in]
+ end
+ contents = Marshal.dump(data)
+ else
+ contents = Marshal.dump(value)
+ end
+ file.puts(contents)
+ end
end
+ def update_key(key, options)
+ store(key, self[key], options)
+ end
+
+ def delete!(key)
+ FileUtils.rm(path(key))
+ nil
+ rescue Errno::ENOENT
+ end
+
+ def delete(key)
+ value = self[key]
+ delete!(key)
+ value
+ end
+
def clear
- super
- if @expiration
- @expiration.clear
- end
+ FileUtils.rm_rf(@directory)
+ FileUtils.mkdir(@directory)
end
private
- def setup_expiration_cache
- @expiration = BasicFile.new(:path => @expiration_directory, :skip_expires => true)
+ def path(key)
+ ::File.join(@directory, key.to_s)
end
def ensure_directory_created(directory_path)

0 comments on commit 463e10e

Please sign in to comment.