Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Get all the adapters (and a couple new ones) working on the new spec

  • Loading branch information...
commit eab31e7bb849fd6dab940fc668f5e27a70b8fbf0 1 parent 4542cf3
@wycats authored
Showing with 496 additions and 687 deletions.
  1. +1 −1  SPEC.markdown
  2. +13 −5 lib/moneta.rb
  3. +16 −25 lib/moneta/adapters/basic_file.rb
  4. +0 −49 lib/moneta/adapters/berkeley.rb
  5. +9 −22 lib/moneta/adapters/couch.rb
  6. +27 −68 lib/moneta/adapters/datamapper.rb
  7. +5 −5 lib/moneta/adapters/file.rb
  8. +62 −0 lib/moneta/adapters/fog.rb
  9. +16 −8 lib/moneta/adapters/lmc.rb
  10. +13 −19 lib/moneta/adapters/memcache.rb
  11. +8 −25 lib/moneta/adapters/memory.rb
  12. +40 −46 lib/moneta/adapters/mongodb.rb
  13. +14 −0 lib/moneta/adapters/rackspace.rb
  14. +30 −38 lib/moneta/adapters/redis.rb
  15. +0 −41 lib/moneta/adapters/rufus.rb
  16. +8 −156 lib/moneta/adapters/s3.rb
  17. +22 −21 lib/moneta/adapters/sdbm.rb
  18. +51 −0 lib/moneta/adapters/tokyo_cabinet.rb
  19. +30 −43 lib/moneta/adapters/tyrant.rb
  20. +5 −5 lib/moneta/adapters/xattr.rb
  21. +4 −4 lib/moneta/adapters/yaml.rb
  22. +5 −1 lib/moneta/builder.rb
  23. +0 −26 spec/moneta_berkeley_spec.rb
  24. +1 −5 spec/moneta_couch_spec.rb
  25. +22 −24 spec/moneta_datamapper_spec.rb
  26. +4 −5 spec/moneta_mongodb_spec.rb
  27. +21 −0 spec/moneta_rackspace_spec.rb
  28. +6 −7 spec/moneta_redis_spec.rb
  29. +0 −15 spec/moneta_rufus_spec.rb
  30. +11 −9 spec/moneta_s3_spec.rb
  31. +8 −6 spec/moneta_sdbm_spec.rb
  32. +19 −0 spec/moneta_tokyo_cabinet_spec.rb
  33. +6 −6 spec/moneta_tyrant_spec.rb
  34. +19 −2 spec/shared.rb
View
2  SPEC.markdown
@@ -96,4 +96,4 @@ The base Moneta specification does not specify any atomicity guarantees. However
# Expiry
-The base Moneta specification does not specify any mechanism for time-based expiry. However, extensions to this spec may specify mechanisms (using <code>store</code> and <code>update_key</code>) to provide expiration semantics.
+The base Moneta specification does not specify any mechanism for time-based expiry. However, extensions to this spec may specify mechanisms (using <code>store</code> and <code>update_key</code>) to provide expiration semantics.
View
18 lib/moneta.rb
@@ -2,20 +2,28 @@
module Moneta
module Defaults
- def fetch(key, value = nil)
- self[key_for(key)] || begin
+ def fetch(key, value = nil, *)
+ self[key] || begin
value ||= block_given? ? yield(key) : default
- self[key_for(key)] || value
+ self[key] || value
end
end
- def store(key, value, options = {})
- self[key_for(key)] = value
+ def []=(key, value)
+ store(key, value)
end
private
def key_for(key)
key.is_a?(String) ? key : Marshal.dump(key)
end
+
+ def serialize(value)
+ Marshal.dump(value)
+ end
+
+ def deserialize(value)
+ value && Marshal.load(value)
+ end
end
end
View
41 lib/moneta/adapters/basic_file.rb
@@ -29,7 +29,7 @@ def initialize(options = {})
ensure_directory_created(@directory)
end
- def key?(key)
+ def key?(key, *)
!self[key].nil?
end
@@ -39,39 +39,20 @@ def [](key)
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 = {})
+ def store(key, value, *)
ensure_directory_created(::File.dirname(path(key)))
::File.open(path(key), "w") do |file|
- contents = Marshal.dump(value)
- file.puts(contents)
+ file.puts(serialize(value))
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)
+ def delete(key, *)
value = self[key]
delete!(key)
value
end
- def clear
+ def clear(*)
FileUtils.rm_rf(@directory)
FileUtils.mkdir(@directory)
end
@@ -81,6 +62,16 @@ def path(key)
::File.join(@directory, key_for(key))
end
+ def raw_get(key)
+ deserialize(::File.read(path(key)))
+ end
+
+ def delete!(key)
+ FileUtils.rm(path(key))
+ nil
+ rescue Errno::ENOENT
+ end
+
def ensure_directory_created(directory_path)
if ::File.file?(directory_path)
raise StandardError, "The path you supplied #{directory_path} is a file"
@@ -90,4 +81,4 @@ def ensure_directory_created(directory_path)
end
end
end
-end
+end
View
49 lib/moneta/adapters/berkeley.rb
@@ -1,49 +0,0 @@
-begin
- require 'bdb'
-rescue LoadError
- puts "You need bdb gem to use Bdb moneta store"
- exit
-end
-
-module Moneta
- module Adapters
- class Berkeley
- include Defaults
-
- @@dbs = []
-
- def self.close_all
- @@dbs.each {|d| d.close(0) }
- end
-
- def initialize(options={})
- file = @file = options[:file]
- @db = Bdb::Db.new
- @db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
- @@dbs << @db
- end
-
- def key?(key)
- nil | self[key_for(key)]
- end
-
- def []=(key,value)
- @db[key_for(key)] = value
- end
-
- def [](key)
- @db[key_for(key)]
- end
-
- def delete(key)
- value = self[key]
- @db.del(nil,key_for(key),0) if value
- value
- end
-
- def clear
- @db.truncate(nil)
- end
- end
- end
-end
View
31 lib/moneta/adapters/couch.rb
@@ -9,56 +9,43 @@ module Moneta
module Adapters
class Couch
include Defaults
-
+
def initialize(options = {})
@db = ::CouchRest.database!(options[:db])
end
- def key?(key)
+ def key?(key, *)
!self[key_for(key)].nil?
rescue RestClient::ResourceNotFound
false
end
- alias has_key? key?
-
def [](key)
- @db.get(key_for(key))["data"]
+ deserialize(@db.get(key_for(key))["data"])
rescue RestClient::ResourceNotFound
nil
end
- def []=(key, value)
- @db.save_doc("_id" => key_for(key), :data => value)
+ def store(key, value, *)
+ @db.save_doc("_id" => key_for(key), :data => serialize(value))
rescue RestClient::RequestFailed
self[key_for(key)]
end
- def delete(key)
+ def delete(key, *)
value = @db.get(key_for(key))
-
+
if value
@db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
- value["data"]
+ deserialize(value["data"])
end
rescue RestClient::ResourceNotFound
nil
end
- def update_key(key, options = {})
- val = self[key]
- self.store(key, val, options)
- rescue RestClient::ResourceNotFound
- nil
- end
-
- def clear
+ def clear(*)
@db.recreate!
end
-
- def delete_store
- @db.delete!
- end
end
end
end
View
95 lib/moneta/adapters/datamapper.rb
@@ -1,6 +1,9 @@
begin
- gem "dm-core", "0.9.10"
+ gem "dm-core", "~> 1.0.0"
+ gem "dm-migrations", "~> 1.0.0"
+ gem "dm-sqlite-adapter", "~> 1.0.0"
require "dm-core"
+ require "dm-migrations"
rescue LoadError
puts "You need the dm-core gem in order to use the DataMapper moneta store"
exit
@@ -11,7 +14,6 @@ class MonetaHash
property :the_key, String, :key => true
property :value, Object, :lazy => false
- property :expires, Time
def self.value(key)
obj = self.get(key)
@@ -20,98 +22,55 @@ def self.value(key)
end
module Moneta
- class DataMapper
- class Expiration
- def initialize(klass, repository)
- @klass = klass
- @repository = repository
+ module Adapters
+ class DataMapper
+ include Moneta::Defaults
+
+ def initialize(options = {})
+ @repository = options.delete(:repository) || :moneta
+ ::DataMapper.setup(@repository, options[:setup])
+ MonetaHash.auto_upgrade!(@repository)
+ @hash = MonetaHash
end
- def [](key)
- if obj = get(key)
- obj.expires
- end
- end
-
- def []=(key, value)
- obj = get(key)
- obj.expires = value
- obj.save(@repository)
- end
-
- def delete(key)
- obj = get(key)
- obj.expires = nil
- obj.save(@repository)
- end
-
- private
- def get(key)
- repository(@repository) { @klass.get(key) }
- end
- end
-
- def initialize(options = {})
- @repository = options.delete(:repository) || :moneta
- ::DataMapper.setup(@repository, options[:setup])
- repository_context { MonetaHash.auto_upgrade! }
- @hash = MonetaHash
- @expiration = Expiration.new(MonetaHash, @repository)
- end
-
- module Implementation
- def key?(key)
- repository_context { !!@hash.get(key) }
- end
-
- def has_key?(key)
- repository_context { !!@hash.get(key) }
+ def key?(key, *)
+ repository_context { !!@hash.get(key_for(key)) }
end
def [](key)
- repository_context { @hash.value(key) }
+ repository_context { @hash.value(key_for(key)) }
end
- def []=(key, value)
+ def store(key, value, *)
+ string_key = key_for(key)
repository_context {
- obj = @hash.get(key)
+ obj = @hash.get(string_key)
if obj
- obj.update(key, value)
+ obj.update(string_key, value)
else
- @hash.create(:the_key => key, :value => value)
+ @hash.create(:the_key => string_key, :value => value)
end
}
end
- def fetch(key, value = nil)
- repository_context {
- value ||= block_given? ? yield(key) : default
- self[key] || value
- }
- end
+ def delete(key, *)
+ string_key = key_for(key)
- def delete(key)
repository_context {
value = self[key]
- @hash.all(:the_key => key).destroy!
+ @hash.all(:the_key => string_key).destroy!
value
}
end
- def store(key, value, options = {})
- repository_context { self[key] = value }
- end
-
- def clear
+ def clear(*)
repository_context { @hash.all.destroy! }
end
- private
+ private
def repository_context
repository(@repository) { yield }
end
end
- include Implementation
- include Expires
end
-end
+end
View
10 lib/moneta/adapters/file.rb
@@ -14,7 +14,7 @@ def initialize(options = {})
end
end
- def key?(key)
+ def key?(key, *)
::File.exist?(path(key))
end
@@ -24,21 +24,21 @@ def [](key)
end
end
- def []=(key, value)
+ def store(key, value, *)
::File.open(path(key), "w") do |file|
contents = Marshal.dump(value)
file.puts(contents)
end
end
- def delete(key)
+ def delete(key, *)
value = self[key]
FileUtils.rm(path(key))
value
rescue Errno::ENOENT
end
- def clear
+ def clear(*)
FileUtils.rm_rf(@directory)
FileUtils.mkdir(@directory)
end
@@ -49,4 +49,4 @@ def path(key)
end
end
end
-end
+end
View
62 lib/moneta/adapters/fog.rb
@@ -0,0 +1,62 @@
+begin
+ require "fog"
+rescue LoadError
+ puts "You need the Fog gem to use the S3 moneta store"
+ exit
+end
+
+module Moneta
+ module Adapters
+ class Fog
+ include Moneta::Defaults
+
+ def initialize(options = {})
+ bucket = options.delete(:namespace)
+ s3 = options.delete(:cloud).new(options)
+ @directory = s3.directories.create(:key => bucket)
+ end
+
+ def key?(key)
+ !@directory.files.head(key_for(key)).nil?
+ end
+
+ def [](key)
+ if value = get(key)
+ deserialize(value.body)
+ end
+ end
+
+ def delete(key)
+ value = get(key)
+ if value
+ value.destroy
+ deserialize(value.body)
+ end
+ end
+
+ def store(key, value, options = {})
+ #perms = options[:perms]
+ #headers = options[:headers] || {}
+ @directory.files.create(:key => key_for(key), :body => serialize(value))
+ end
+
+ # def update_key(key, options = {})
+ # debug "update_key(key=#{key}, options=#{options.inspect})"
+ # k = s3_key(key, false)
+ # k.save_meta(meta_headers_from_options(options)) unless k.nil?
+ # end
+
+ def clear
+ @directory.files.all.each do |file|
+ file.destroy
+ end
+ self
+ end
+
+ private
+ def get(key)
+ @directory.files.get(key_for(key))
+ end
+ end
+ end
+end
View
24 lib/moneta/adapters/lmc.rb
@@ -14,19 +14,27 @@ def initialize(options = {})
@hash = LocalMemCache.new(:filename => options[:filename])
end
- def [](key) @hash[Marshal.dump(key)] end
- def []=(key, value) @hash[Marshal.dump(key)] = value end
- def clear() @hash.clear end
+ def [](key)
+ deserialize(@hash[key_for(key)])
+ end
+
+ def store(key, value, *)
+ @hash[key_for(key)] = serialize(value)
+ end
- def key?(key)
- @hash.keys.include?(Marshal.dump(key))
+ def clear(*)
+ @hash.clear
end
- def delete(key)
+ def key?(key, *)
+ @hash.keys.include?(key_for(key))
+ end
+
+ def delete(key, *)
value = self[key]
- @hash.delete(Marshal.dump(key))
+ @hash.delete(key_for(key))
value
end
end
end
-end
+end
View
32 lib/moneta/adapters/memcache.rb
@@ -11,44 +11,38 @@
module Moneta
module Adapters
class Memcache
- include Defaults
+ include Moneta::Defaults
def initialize(options = {})
- @cache = MemCache.new(options.delete(:server), options)
+ @cache = ::MemCache.new(options.delete(:server), options)
end
- def key?(key)
+ def key?(key, *)
!self[key].nil?
end
- alias has_key? key?
-
def [](key)
- @cache.get(key_for(key))
- end
-
- def []=(key, value)
- store(key, value)
+ deserialize(@cache.get(key_for(key)))
+ rescue MemCache::NotFound
end
- def delete(key)
+ def delete(key, *)
value = self[key]
@cache.delete(key_for(key)) if value
value
end
- def store(key, value, options = {})
- args = [key_for(key), value, options[:expires_in]].compact
- @cache.set(*args)
+ def store(key, value, *)
+ @cache.set(key_for(key), serialize(value))
end
- def update_key(key, options = {})
- val = self[key]
- self.store(key, val, options)
+ def clear(*)
+ @cache.flush
end
- def clear
- @cache.flush_all
+ private
+ def key_for(key)
+ [super].pack("m").strip
end
end
end
View
33 lib/moneta/adapters/memory.rb
@@ -1,40 +1,23 @@
module Moneta
module Adapters
class Memory < Hash
- def initialize(*args)
- @expiration = {}
- super
- end
+ include Moneta::Defaults
def [](key)
- key = Marshal.dump(key)
- super
- end
-
- def []=(key, value)
- key = Marshal.dump(key)
- super
- end
-
- def key?(key)
- key = Marshal.dump(key)
- super
+ deserialize(super(key_for(key)))
end
- def fetch(key, *args)
- key = Marshal.dump(key)
- super
+ def key?(key, *)
+ super(key_for(key))
end
- def store(key, *args)
- key = Marshal.dump(key)
- super
+ def store(key, value, *args)
+ super(key_for(key), serialize(value), *args)
end
def delete(key, *args)
- key = Marshal.dump(key)
- super
+ deserialize(super(key_for(key), *args))
end
end
end
-end
+end
View
86 lib/moneta/adapters/mongodb.rb
@@ -6,52 +6,46 @@
end
module Moneta
- class MongoDB
- include Defaults
-
- def initialize(options = {})
- options = {
- :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
- :port => ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT,
- :db => 'cache',
- :collection => 'cache'
- }.update(options)
- conn = XGen::Mongo::Driver::Connection.new(options[:host], options[:port])
- @cache = conn.db(options[:db]).collection(options[:collection])
- end
-
- def key?(key)
- !!self[key]
- end
-
- def [](key)
- res = @cache.find_first('_id' => key)
- res = nil if res && res['expires'] && Time.now > res['expires']
- res && res['data']
- end
-
- def []=(key, value)
- store(key, value)
- end
-
- def delete(key)
- value = self[key]
- @cache.remove('_id' => key) if value
- value
- end
-
- def store(key, value, options = {})
- exp = options[:expires_in] ? (Time.now + options[:expires_in]) : nil
- @cache.update({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => exp })
- end
-
- def update_key(key, options = {})
- val = self[key]
- self.store(key, val, options)
- end
-
- def clear
- @cache.clear
+ module Adapters
+ class MongoDB
+ include Moneta::Defaults
+
+ def initialize(options = {})
+ options = {
+ :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
+ :port => ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT,
+ :db => 'cache',
+ :collection => 'cache'
+ }.update(options)
+ conn = Mongo::Connection.new(options[:host], options[:port])
+ @cache = conn.db(options[:db]).collection(options[:collection])
+ end
+
+ def key?(key, *)
+ !!self[key]
+ end
+
+ def [](key)
+ res = @cache.find_one('_id' => key_for(key))
+ res && deserialize(res['data'])
+ end
+
+ def delete(key, *)
+ string_key = key_for(key)
+
+ value = self[key]
+ @cache.remove('_id' => string_key) if value
+ value
+ end
+
+ def store(key, value, *)
+ key = key_for(key)
+ @cache.insert({ '_id' => key, 'data' => serialize(value) })
+ end
+
+ def clear(*)
+ @cache.remove
+ end
end
end
end
View
14 lib/moneta/adapters/rackspace.rb
@@ -0,0 +1,14 @@
+require "moneta/adapters/fog"
+
+module Moneta
+ module Adapters
+ class Rackspace < Fog
+
+ def initialize(options)
+ options[:cloud] = ::Fog::Rackspace::Files
+ super
+ end
+
+ end
+ end
+end
View
68 lib/moneta/adapters/redis.rb
@@ -6,44 +6,36 @@
end
module Moneta
- class Redis
- include Defaults
-
- def initialize(options = {})
- @cache = ::Redis.new(options)
- end
-
- def key?(key)
- !@cache[key].nil?
- end
-
- alias has_key? key?
-
- def [](key)
- @cache.get(key)
- end
-
- def []=(key, value)
- store(key, value)
- end
-
- def delete(key)
- value = @cache[key]
- @cache.delete(key) if value
- value
- end
-
- def store(key, value, options = {})
- @cache.set(key, value, options[:expires_in])
- end
-
- def update_key(key, options = {})
- val = @cache[key]
- self.store(key, val, options)
- end
-
- def clear
- @cache.flush_db
+ module Adapters
+ class Redis
+ include Defaults
+
+ def initialize(options = {})
+ @cache = ::Redis.new(options)
+ end
+
+ def key?(key, *)
+ !@cache[key_for(key)].nil?
+ end
+
+ def [](key)
+ deserialize(@cache.get(key_for(key)))
+ end
+
+ def delete(key, *)
+ string_key = key_for(key)
+ value = self[key]
+ @cache.del(string_key) if value
+ value
+ end
+
+ def store(key, value, *)
+ @cache.set(key_for(key), serialize(value))
+ end
+
+ def clear(*)
+ @cache.flushdb
+ end
end
end
end
View
41 lib/moneta/adapters/rufus.rb
@@ -1,41 +0,0 @@
-begin
- require "rufus/tokyo"
-rescue LoadError
- puts "You need the rufus gem to use the Rufus moneta store"
- exit
-end
-
-module Moneta
- class BasicRufus < ::Rufus::Tokyo::Cabinet
- include Defaults
-
- def initialize(options = {})
- file = options[:file]
- super("#{file}.tch")
- end
-
- def key?(key)
- !!self[key]
- end
-
- def [](key)
- if val = super
- Marshal.load(val.unpack("m")[0])
- end
- end
-
- def []=(key, value)
- super(key, [Marshal.dump(value)].pack("m"))
- end
- end
-
- class Rufus < BasicRufus
- include Expires
-
- def initialize(options = {})
- file = options[:file]
- @expiration = BasicRufus.new(:file => "#{file}_expires")
- super
- end
- end
-end
View
164 lib/moneta/adapters/s3.rb
@@ -1,162 +1,14 @@
-begin
- require "right_aws"
-rescue LoadError
- puts "You need the RightScale AWS gem to use the S3 moneta store"
- exit
-end
+require "moneta/adapters/fog"
module Moneta
- # An S3 implementation of Moneta
- #
- # Example usage:
- #
- # require 'rubygems'
- # require 'moneta'
- # require 'moneta/s3'
- #
- # store = Moneta::S3.new(
- # :access_key_id => 'ACCESS_KEY_ID',
- # :secret_access_key => 'SECRET_ACCESS_KEY',
- # :bucket => 'a_bucket'
- # )
- # store['somefile']
- class S3
- # Initialize the Moneta::S3 store.
- #
- # Required values passed in the options hash:
- # * <tt>:access_key_id</tt>: The access id key
- # * <tt>:secret_access_key</tt>: The secret key
- # * <tt>:bucket</tt>: The name of bucket. Will be created if it doesn't
- # exist.
- # * <tt>:multi_thread</tt>: Set to true if using threading
- def initialize(options = {})
- validate_options(options)
- s3 = RightAws::S3.new(
- options[:access_key_id],
- options[:secret_access_key],
- {
- :logger => logger,
- :multi_thread => options.delete(:multi_thread) || false
- }
- )
- @bucket = s3.bucket(options.delete(:bucket), true)
- end
-
- def key?(key)
- !s3_key(key).nil?
- end
-
- alias has_key? key?
-
- def [](key)
- get(key)
- end
-
- def []=(key, value)
- store(key, value)
- end
-
- def delete(key)
- k = s3_key(key)
- if k
- value = k.get
- k.delete
- value
- end
- end
-
- # Store the key/value pair.
- #
- # Options:
- # *<tt>:meta_headers</tt>: Meta headers passed to S3
- # *<tt>:perms</tt>: Permissions passed to S3
- # *<tt>:headers</tt>: Headers sent as part of the PUT request
- # *<tt>:expires_in</tt>: Number of seconds until expiration
- def store(key, value, options = {})
- debug "store(key=#{key}, value=#{value}, options=#{options.inspect})"
- meta_headers = meta_headers_from_options(options)
- perms = options[:perms]
- headers = options[:headers] || {}
-
- case value
- when IO
- @bucket.put(key, value.read, meta_headers, perms, headers)
- else
- @bucket.put(key, value, meta_headers, perms, headers)
- end
- end
-
- def update_key(key, options = {})
- debug "update_key(key=#{key}, options=#{options.inspect})"
- k = s3_key(key, false)
- k.save_meta(meta_headers_from_options(options)) unless k.nil?
- end
-
- def clear
- @bucket.clear
- end
-
- protected
- def logger
- @logger ||= begin
- logger = Logger.new(STDOUT)
- logger.level = Logger::FATAL
- logger
- end
- end
-
- private
- def validate_options(options)
- unless options[:access_key_id]
- raise RuntimeError, ":access_key_id is required in options"
- end
- unless options[:secret_access_key]
- raise RuntimeError, ":secret_access_key is required in options"
- end
- unless options[:bucket]
- raise RuntimeError, ":bucket is required in options"
- end
- end
-
- def get(key)
- k = s3_key(key)
- k.nil? ? nil : k.get
- end
-
- def s3_key(key, nil_if_expired=true)
- begin
- s3_key = @bucket.key(key, true)
- if s3_key.exists?
- logger.debug "[Moneta::S3] key exists: #{key}"
- if s3_key.meta_headers.has_key?('expires-at')
- expires_at = Time.parse(s3_key.meta_headers['expires-at'])
- if Time.now > expires_at && nil_if_expired
- # TODO delete the object?
- debug "key expired: #{key} (@#{s3_key.meta_headers['expires-at']})"
- return nil
- end
- end
- return s3_key
- else
- debug "key does not exist: #{key}"
- end
- rescue RightAws::AwsError => e
- debug "key does not exist: #{key}"
- end
- nil
- end
-
- def meta_headers_from_options(options={})
- meta_headers = options[:meta_headers] || {}
- if options[:expires_in]
- meta_headers['expires-at'] = (Time.now + options[:expires_in]).rfc2822
+ module Adapters
+ class S3 < Fog
+
+ def initialize(options)
+ options[:cloud] = ::Fog::AWS::S3
+ super
end
- debug "setting expires-at: #{meta_headers['expires-at']}"
- meta_headers
- end
-
- def debug(message)
- logger.debug "[Moneta::S3] #{message}"
+
end
end
end
View
43 lib/moneta/adapters/sdbm.rb
@@ -1,33 +1,34 @@
require "sdbm"
module Moneta
- class BasicSDBM < ::SDBM
- include Defaults
+ module Adapters
+ class SDBM < ::SDBM
+ include Moneta::Defaults
- def [](key)
- if val = super
- Marshal.load(val)
+ def initialize(options = {})
+ raise "No :file options specified" unless file = options[:file]
+ super(file)
end
- end
- def []=(key, value)
- super(key, Marshal.dump(value))
- end
+ def [](key)
+ if val = super(key_for(key))
+ deserialize(val)
+ end
+ end
- def delete(key)
- if val = super
- Marshal.load(val)
+ def store(key, value, *)
+ super(key_for(key), serialize(value))
end
- end
- end
- class SDBM < BasicSDBM
- include Expires
+ def key?(key, *)
+ super(key_for(key))
+ end
- def initialize(options = {})
- raise "No :file option specified" unless file = options[:file]
- @expiration = BasicSDBM.new("#{file}_expires")
- super(file)
+ def delete(key, *)
+ if val = super(key_for(key))
+ deserialize(val)
+ end
+ end
end
end
-end
+end
View
51 lib/moneta/adapters/tokyo_cabinet.rb
@@ -0,0 +1,51 @@
+begin
+ require "tokyocabinet"
+rescue LoadError
+ puts "You need the tokyocabinet gem to use the tokyo cabinet store"
+ exit
+end
+
+module Moneta
+ module Adapters
+ class TokyoCabinet
+ include Defaults
+
+ def initialize(options = {})
+ file = options[:file]
+ @cache = ::TokyoCabinet::HDB.new
+ unless @cache.open(file, ::TokyoCabinet::HDB::OWRITER | ::TokyoCabinet::HDB::OCREAT)
+ puts @cache.errmsg(@cache.ecode)
+ end
+ end
+
+ def [](key)
+ deserialize(@cache[key_for(key)])
+ end
+
+ def store(key, value, *)
+ @cache[key_for(key)] = serialize(value)
+ end
+
+ def key?(key, *)
+ !!self[key]
+ end
+
+ def delete(key, *)
+ value = self[key]
+
+ if value
+ @cache.delete(key_for(key))
+ value
+ end
+ end
+
+ def clear(*)
+ @cache.clear
+ end
+
+ def close
+ @cache.close
+ end
+ end
+ end
+end
View
73 lib/moneta/adapters/tyrant.rb
@@ -1,58 +1,45 @@
begin
- require "rufus/tokyo/tyrant"
+ require "tokyotyrant"
rescue LoadError
- puts "You need the rufus gem to use the Tyrant moneta store"
+ puts "You need the tokyotyrant gem to use the Tyrant moneta store"
exit
end
module Moneta
- class Tyrant < ::Rufus::Tokyo::Tyrant
- include Defaults
-
- module Implementation
+ module Adapters
+ class Tyrant
+ include Defaults
+
def initialize(options = {})
- host = options[:host]
- port = options[:port]
- super(host, port)
- end
-
- def key?(key)
+ @hash = ::TokyoTyrant::RDB.new
+
+ if !@hash.open(options[:host], options[:port])
+ puts @hash.errmsg(@hash.ecode)
+ end
+ end
+
+ def key?(key, *)
!!self[key]
end
-
+
def [](key)
- if val = super
- Marshal.load(val.unpack("m")[0])
- end
+ deserialize(@hash[key_for(key)])
end
-
- def []=(key, value)
- super(key, [Marshal.dump(value)].pack("m"))
- end
- end
-
- include Implementation
- include Expires
-
- def initialize(options = {})
- super
- @expiration = Expiration.new(options)
- end
-
- class Expiration < ::Rufus::Tokyo::Tyrant
- include Implementation
-
- def [](key)
- super("#{key}__expiration")
+
+ def store(key, value, *)
+ @hash.put(key_for(key), serialize(value))
end
-
- def []=(key, value)
- super("#{key}__expiration", value)
+
+ def clear(*)
+ @hash.clear
end
-
- def delete(key)
- super("#{key}__expiration")
+
+ def delete(key, *)
+ if value = self[key]
+ @hash.delete(key_for(key))
+ value
+ end
end
end
- end
-end
+ end
+end
View
10 lib/moneta/adapters/xattr.rb
@@ -18,7 +18,7 @@ def initialize(options = {})
FileUtils.touch(file)
end
- def key?(key)
+ def key?(key, *)
@hash.list.include?(key_for(key))
end
@@ -28,18 +28,18 @@ def [](key)
Marshal.load(@hash.get(string_key))
end
- def []=(key, value)
+ def store(key, value, *)
@hash.set(key_for(key), Marshal.dump(value))
end
- def delete(key)
+ def delete(key, *)
return nil unless key?(key)
value = self[key]
@hash.remove(key_for(key))
value
end
- def clear
+ def clear(*)
@hash.list.each do |item|
@hash.remove(item)
end
@@ -47,4 +47,4 @@ def clear
end
end
-end
+end
View
8 lib/moneta/adapters/yaml.rb
@@ -13,7 +13,7 @@ def initialize(options = {})
end
end
- def key?(key)
+ def key?(key, *)
yaml.has_key?(key_for(key))
end
@@ -22,13 +22,13 @@ def [](key)
yaml[string_key]['value'] if yaml.key?(string_key)
end
- def []=(key, value)
+ def store(key, value, *)
hash = yaml
(hash[key_for(key)] ||= {})['value'] = value
save(hash)
end
- def delete(key)
+ def delete(key, *)
hash = yaml
value = self[key_for(key)]
hash.delete(key_for(key))
@@ -36,7 +36,7 @@ def delete(key)
value
end
- def clear
+ def clear(*)
save
end
View
6 lib/moneta/builder.rb
@@ -41,6 +41,10 @@ def key?(key, *args)
def clear(*args)
@adapter.clear(*args)
end
+
+ def close
+ @adapter.close
+ end
end
class Builder
@@ -65,4 +69,4 @@ def run(adapter, *args, &block)
end
end
-end
+end
View
26 spec/moneta_berkeley_spec.rb
@@ -1,26 +0,0 @@
-require 'spec_helper'
-
-begin
- require 'moneta/adapters/berkeley'
- require 'bdb/environment'
-
- describe "Moneta::Adapters::Berkeley" do
- path = File.expand_path("../berkeley_test.db", __FILE__)
-
- before(:each) do
- @cache = Moneta::Builder.build do
- run Moneta::Adapters::Berkeley, :file => path
- end
- @cache.clear
- end
-
- after(:all) do
- Moneta::Adapters::Berkeley.close_all
- File.delete(path)
- end
-
- it_should_behave_like "a read/write Moneta cache"
- end
-
-rescue SystemExit
-end
View
6 spec/moneta_couch_spec.rb
@@ -11,11 +11,7 @@
@cache.clear
end
- after(:all) do
- @cache.delete_store
- end
-
it_should_behave_like "a read/write Moneta cache"
end
rescue SystemExit
-end
+end
View
46 spec/moneta_datamapper_spec.rb
@@ -1,18 +1,27 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
+require 'fileutils'
begin
- require "moneta/datamapper"
-
+ require "moneta/adapters/datamapper"
+
+ DataMapper.setup(:default, :adapter => :in_memory)
+ path = File.expand_path("../datamapper_spec.db", __FILE__)
+ FileUtils.rm(path) if File.exist?(path)
+
describe "Moneta::DataMapper" do
- describe "default repository" do
+ before(:each) do
+ DataMapper.repository(:default).adapter.reset
+ end
+
+ describe "with the default repository" do
before(:each) do
- @cache = Moneta::DataMapper.new(:setup => "sqlite3::memory:")
+ @cache = Moneta::Adapters::DataMapper.new(:setup => "sqlite3://#{path}")
@cache.clear
end
-
+
after(:all) do
- repository(:moneta) { MonetaHash.auto_migrate! }
+ MonetaHash.auto_migrate!(:moneta)
end
it_should_behave_like "a read/write Moneta cache"
@@ -20,23 +29,23 @@
describe "when :repository specified" do
before(:each) do
- @cache = Moneta::DataMapper.new(:repository => :sample, :setup => "sqlite3::memory:")
+ @cache = Moneta::Adapters::DataMapper.new(:repository => :sample, :setup => "sqlite3://#{path}")
@cache.clear
end
after(:all) do
- repository(:sample) { MonetaHash.auto_migrate! }
+ MonetaHash.auto_migrate!(:sample)
end
it_should_behave_like "a read/write Moneta cache"
end
- describe "with multple stores" do
+ describe "with multiple stores" do
before(:each) do
- @default_cache = Moneta::DataMapper.new(:setup => "sqlite3:moneta.db")
+ @default_cache = Moneta::Adapters::DataMapper.new(:setup => "sqlite3:moneta.db")
@default_cache.clear
- @sample_cache = Moneta::DataMapper.new(:repository => :sample, :setup => "sqlite3:sample.db")
+ @sample_cache = Moneta::Adapters::DataMapper.new(:repository => :sample, :setup => "sqlite3:sample.db")
@sample_cache.clear
end
@@ -62,18 +71,7 @@
@default_cache.key?("key").should be_false
@sample_cache["key"].should == "value2"
end
-
- it "does not cross contaminate when expiring" do
- @default_cache.store("key", "value", :expires_in => 2)
- @sample_cache["key"] = "value2"
-
- time = Time.now
- Time.stub!(:now).and_return { time + 2 }
-
- @default_cache["key"].should == nil
- @sample_cache["key"].should == "value2"
- end
end
end
rescue SystemExit
-end
+end
View
9 spec/moneta_mongodb_spec.rb
@@ -1,16 +1,15 @@
require File.dirname(__FILE__) + '/spec_helper'
begin
- require "moneta/mongodb"
+ require "moneta/adapters/mongodb"
- describe "Moneta::MongoDB" do
+ describe "Moneta::Adapters::MongoDB" do
before(:each) do
- @native_expires = true
- @cache = Moneta::MongoDB.new
+ @cache = Moneta::Adapters::MongoDB.new
@cache.clear
end
it_should_behave_like "a read/write Moneta cache"
end
rescue SystemExit
-end
+end
View
21 spec/moneta_rackspace_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+begin
+ require 'moneta/adapters/rackspace'
+
+ describe "Moneta::Adapters::Rackspace" do
+ before(:each) do
+ #Fog.mock! unless ENV["RACKSPACE_USERNAME"]
+
+ @cache = Moneta::Adapters::Rackspace.new(
+ :rackspace_username => ENV["RACKSPACE_USERNAME"] || "mocked",
+ :rackspace_api_key => ENV["RACKSPACE_APIKEY"] || "mocked",
+ :namespace => "TESTING"
+ )
+ @cache.clear
+ end
+
+ it_should_behave_like "a read/write Moneta cache"
+ end
+rescue SystemExit
+end
View
13 spec/moneta_redis_spec.rb
@@ -1,16 +1,15 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
begin
- require "moneta/redis"
+ require "moneta/adapters/redis"
- describe "Moneta::Redis" do
+ describe "Moneta::Adapters::Redis" do
before(:each) do
- @native_expires = true
- @cache = Moneta::Redis.new
+ @cache = Moneta::Adapters::Redis.new
@cache.clear
end
-
+
it_should_behave_like "a read/write Moneta cache"
end
rescue SystemExit
-end
+end
View
15 spec/moneta_rufus_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-begin
- require "moneta/adapters/rufus"
-
- describe "Moneta::Adapters::Rufus" do
- before(:each) do
- @cache = Moneta::Adapters::Rufus.new(:file => "cache")
- @cache.clear
- end
-
- it_should_behave_like "a read/write Moneta cache"
- end
-rescue SystemExit
-end
View
20 spec/moneta_s3_spec.rb
@@ -1,19 +1,21 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
begin
- require 'moneta/s3'
+ require 'moneta/adapters/s3'
- describe "Moneta::S3" do
+ describe "Moneta::Adapters::S3" do
before(:each) do
- @cache = Moneta::S3.new(
- :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
- :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
- :bucket => 'moneta_test'
+ Fog.mock! unless ENV["S3_KEY"]
+
+ @cache = Moneta::Adapters::S3.new(
+ :aws_access_key_id => ENV["S3_KEY"] || "mocked",
+ :aws_secret_access_key => ENV["S3_SECRET"] || "mocked",
+ :namespace => "TESTING"
)
@cache.clear
end
-
+
it_should_behave_like "a read/write Moneta cache"
end
rescue SystemExit
-end
+end
View
14 spec/moneta_sdbm_spec.rb
@@ -1,16 +1,18 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
begin
- require "moneta/sdbm"
+ require "moneta/adapters/sdbm"
- describe "Moneta::SDBM" do
+ path = File.expand_path("../sdbm_cache", __FILE__)
+
+ describe "Moneta::Adapters::SDBM" do
before(:each) do
- @cache = Moneta::SDBM.new(:file => File.join(File.dirname(__FILE__), "sdbm_cache"))
+ @cache = Moneta::Adapters::SDBM.new(:file => path)
@cache.clear
end
after(:all) do
- FileUtils.rm_rf(Dir.glob(File.join(File.dirname(__FILE__), "sdbm_cache*")))
+ FileUtils.rm_rf(Dir["#{path}*"])
end
if ENV['MONETA_TEST'].nil? || ENV['MONETA_TEST'] == 'sdbm'
@@ -18,4 +20,4 @@
end
end
rescue SystemExit
-end
+end
View
19 spec/moneta_tokyo_cabinet_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+begin
+ require "moneta/adapters/tokyo_cabinet"
+
+ describe "Moneta::Adapters::TokyoCabinet" do
+ before(:each) do
+ @cache = Moneta::Adapters::TokyoCabinet.new(:file => File.expand_path("../cache", __FILE__))
+ @cache.clear
+ end
+
+ after(:each) do
+ @cache.close
+ end
+
+ it_should_behave_like "a read/write Moneta cache"
+ end
+rescue SystemExit
+end
View
12 spec/moneta_tyrant_spec.rb
@@ -1,15 +1,15 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
begin
- require "moneta/tyrant"
+ require "moneta/adapters/tyrant"
- describe "Moneta::Tyrant" do
+ describe "Moneta::Adapters::Tyrant" do
before(:each) do
- @cache = Moneta::Tyrant.new(:host => "localhost", :port => 1978)
+ @cache = Moneta::Adapters::Tyrant.new(:host => "127.0.0.1", :port => 1978)
@cache.clear
end
-
+
it_should_behave_like "a read/write Moneta cache"
end
rescue SystemExit
-end
+end
View
21 spec/shared.rb
@@ -9,11 +9,28 @@
@cache[key].should == nil
end
- it "writes to keys that are #{type}s like a Hash" do
+ it "writes String values to keys that are #{type}s like a Hash" do
@cache[key] = "value"
@cache[key].should == "value"
end
+ it "guarantees that a different String value is retrieved from the #{type} key" do
+ value = "value"
+ @cache[key] = value
+ @cache[key].should_not be_equal(value)
+ end
+
+ it "writes Object values to keys that are #{type}s like a Hash" do
+ @cache[key] = {:foo => :bar}
+ @cache[key].should == {:foo => :bar}
+ end
+
+ it "guarantees that a different Object value is retrieved from the #{type} key" do
+ value = {:foo => :bar}
+ @cache[key] = value
+ @cache[key].should_not be_equal(:foo => :bar)
+ end
+
it "returns false from key? if a #{type} key is not available" do
@cache.key?(key).should be_false
end
@@ -106,4 +123,4 @@
it "specifies that it is writable via frozen?" do
@cache.should_not be_frozen
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.