Permalink
Browse files

Adding in a new file store called BasicFile that doesnt require xattr…

… and can be used on any filesystem

Signed-off-by: Yehuda Katz <wycats@gmail.com>
  • Loading branch information...
1 parent 692c8d8 commit 788e11cd056373ecb7e0584fb7e5a59623b96240 @hcatlin hcatlin committed with Jun 7, 2009
Showing with 114 additions and 0 deletions.
  1. +64 −0 lib/moneta/basic_file.rb
  2. +50 −0 spec/moneta_basic_file_spec.rb
View
@@ -0,0 +1,64 @@
+#
+# Basic File Store
+# 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.
+#
+# The only key requirements are there can't be a key named
+# "___expiration__" and any characters your operating system
+# doesn't like! "/" is to be avoided
+#
+
+
+require 'fileutils'
+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
+
+ ensure_directory_created(@directory)
+ end
+
+ # Override default behaviour to create subdirectories
+ def []=(key, value)
+ ensure_directory_created(::File.dirname(path(key)))
+ super
+ end
+
+ def clear
+ super
+ if @expiration
+ @expiration.clear
+ end
+ end
+
+ private
+ def setup_expiration_cache
+ @expiration = BasicFile.new(:path => @expiration_directory, :skip_expires => true)
+ end
+
+ def ensure_directory_created(directory_path)
+ if ::File.file?(directory_path)
+ raise StandardError, "The path you supplied #{directory_path} is a file"
+ elsif !::File.exists?(directory_path)
+ FileUtils.mkdir_p(directory_path)
+ end
+ end
+
+ end
+end
@@ -0,0 +1,50 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+begin
+ require "moneta/basic_file"
+
+ describe "Moneta::BasicFile" do
+ describe "without namespacing" do
+ before(:each) do
+ @cache = Moneta::BasicFile.new(:path => File.join(File.dirname(__FILE__), "basic_file_cache"))
+ @cache.clear
+ end
+
+ if ENV['MONETA_TEST'].nil? || ENV['MONETA_TEST'] == 'basic_file'
+ it_should_behave_like "a read/write Moneta cache"
+ end
+
+ it "should deal with '/' and '#' in a key" do
+ key = "hello/mom#crazycharacters"
+ @cache[key] = "hi"
+ @cache[key].should == "hi"
+ ::File.exists?(File.join(File.dirname(__FILE__), "basic_file_cache", "")).should == true
+ end
+ end
+
+ describe "with namespacing" do
+ before(:each) do
+ @cache = Moneta::BasicFile.new(:path => File.join(File.dirname(__FILE__), "basic_file_cache"), :namespace => "test_namespace")
+ @cache.clear
+ end
+
+ if ENV['MONETA_TEST'].nil? || ENV['MONETA_TEST'] == 'basic_file'
+ it_should_behave_like "a read/write Moneta cache"
+ end
+
+ it "should act as two stores within the same directory" do
+ @second = Moneta::BasicFile.new(:path => File.join(File.dirname(__FILE__), "basic_file_cache"), :namespace => "second_namespace")
+ @second[:key] = "hello"
+ @cache[:key] = "world!"
+ @second[:key].should == "hello"
+ @cache[:key].should == "world!"
+ end
+ end
+
+ after(:all) do
+ FileUtils.rm_rf(File.join(File.dirname(__FILE__), "basic_file_cache"))
+ end
+
+ end
+rescue SystemExit
+end

0 comments on commit 788e11c

Please sign in to comment.