Skip to content

Commit

Permalink
Added SDBM store. Appears faster than file or xattr, and has no depen…
Browse files Browse the repository at this point in the history
…dencies.
  • Loading branch information
dsrw committed Mar 4, 2009
1 parent fff24fb commit 4a6712d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/moneta/sdbm.rb
@@ -0,0 +1,40 @@
require "sdbm"

module Moneta
class BasicSDBM < ::SDBM

def [](key)
if val = super
Marshal.load(val)
end
end

def []=(key, value)
super(key, Marshal.dump(value))
end

def fetch(key, default)
self[key] || default
end

def store(key, value, options = {})
self[key] = value
end

def delete(key)
if val = super
Marshal.load(val)
end
end
end

class SDBM < BasicSDBM
include Expires

def initialize(options = {})
raise "No :file option specified" unless file = options[:file]
@expiration = BasicSDBM.new("#{file}_expires")
super(file)
end
end
end
17 changes: 17 additions & 0 deletions spec/moneta_sdbm_spec.rb
@@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/spec_helper'
require "moneta/sdbm"

describe "Moneta::SDBM" do
before(:each) do
@cache = Moneta::SDBM.new(:file => File.join(File.dirname(__FILE__), "sdbm_cache"))
@cache.clear
end

after(:all) do
FileUtils.rm_rf(Dir.glob(File.join(File.dirname(__FILE__), "sdbm_cache*")))
end

if ENV['MONETA_TEST'].nil? || ENV['MONETA_TEST'] == 'sdbm'
it_should_behave_like "a read/write Moneta cache"
end
end

0 comments on commit 4a6712d

Please sign in to comment.