gotime-moneta 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +56 -0
- data/Rakefile +60 -0
- data/TODO +4 -0
- data/lib/moneta.rb +76 -0
- data/lib/moneta/basic_file.rb +113 -0
- data/lib/moneta/berkeley.rb +53 -0
- data/lib/moneta/couch.rb +63 -0
- data/lib/moneta/datamapper.rb +117 -0
- data/lib/moneta/file.rb +91 -0
- data/lib/moneta/lmc.rb +52 -0
- data/lib/moneta/memcache.rb +53 -0
- data/lib/moneta/memory.rb +11 -0
- data/lib/moneta/mongodb.rb +58 -0
- data/lib/moneta/redis.rb +67 -0
- data/lib/moneta/rufus.rb +41 -0
- data/lib/moneta/s3.rb +162 -0
- data/lib/moneta/sdbm.rb +33 -0
- data/lib/moneta/tyrant.rb +58 -0
- data/lib/moneta/xattr.rb +58 -0
- data/lib/moneta/yaml.rb +92 -0
- metadata +89 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Yehuda Katz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Moneta: A unified interface for key/value stores
|
2
|
+
================================================
|
3
|
+
|
4
|
+
Moneta provides a standard interface for interacting with various kinds of key/value stores.
|
5
|
+
|
6
|
+
Out of the box, it supports:
|
7
|
+
|
8
|
+
* File store for xattr
|
9
|
+
* Basic File Store
|
10
|
+
* Memcache store
|
11
|
+
* In-memory store
|
12
|
+
* The xattrs in a file system
|
13
|
+
* DataMapper
|
14
|
+
* S3
|
15
|
+
* Berkeley DB
|
16
|
+
* Redis
|
17
|
+
* SDBM
|
18
|
+
* Tokyo
|
19
|
+
* CouchDB
|
20
|
+
|
21
|
+
All stores support key expiration, but only memcache and Redis support it natively. All other stores
|
22
|
+
emulate expiration.
|
23
|
+
|
24
|
+
The Moneta API is purposely extremely similar to the Hash API. In order so support an
|
25
|
+
identical API across stores, it does not support iteration or partial matches, but that
|
26
|
+
might come in a future release.
|
27
|
+
|
28
|
+
The API:
|
29
|
+
|
30
|
+
#initialize(options):: options differs per-store, and is used to set up the store
|
31
|
+
|
32
|
+
#[](key):: retrieve a key. if the key is not available, return nil
|
33
|
+
|
34
|
+
#fetch(key, &block):: retrieve a key. if the key is not available, execute the
|
35
|
+
block and return its return value.
|
36
|
+
|
37
|
+
#fetch(key, value) retrieve a key. if the key is not available, return the value
|
38
|
+
|
39
|
+
#[]=(key, value):: set a value for a key. if the key is already used, clobber it.
|
40
|
+
keys set using []= will never expire
|
41
|
+
|
42
|
+
#delete(key):: delete the key from the store and return the current value
|
43
|
+
|
44
|
+
#key?(key):: true if the key exists, false if it does not
|
45
|
+
|
46
|
+
#has_key?(key):: alias for key?
|
47
|
+
|
48
|
+
#store(key, value, options):: same as []=, but you can supply an :expires_in option,
|
49
|
+
which will specify a number of seconds before the key
|
50
|
+
should expire. In order to support the same features
|
51
|
+
across all stores, only full seconds are supported
|
52
|
+
|
53
|
+
#update_key(key, options):: updates an existing key with a new :expires_in option.
|
54
|
+
if the key has already expired, it will not be updated.
|
55
|
+
|
56
|
+
#clear:: clear all keys in this store
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
GEM = "gotime-moneta"
|
8
|
+
GEM_VERSION = "0.6.2"
|
9
|
+
AUTHOR = "Yehuda Katz"
|
10
|
+
EMAIL = "wycats@gmail.com"
|
11
|
+
HOMEPAGE = "http://www.yehudakatz.com"
|
12
|
+
SUMMARY = "A unified interface to key/value stores (with redis updates)"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = GEM
|
16
|
+
s.version = GEM_VERSION
|
17
|
+
s.platform = Gem::Platform::RUBY
|
18
|
+
s.has_rdoc = true
|
19
|
+
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
20
|
+
s.summary = SUMMARY
|
21
|
+
s.description = s.summary
|
22
|
+
s.author = AUTHOR
|
23
|
+
s.email = EMAIL
|
24
|
+
s.homepage = HOMEPAGE
|
25
|
+
|
26
|
+
# Uncomment this to add a dependency
|
27
|
+
# s.add_dependency "foo"
|
28
|
+
|
29
|
+
s.require_path = 'lib'
|
30
|
+
s.autorequire = GEM
|
31
|
+
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
|
32
|
+
end
|
33
|
+
|
34
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
35
|
+
pkg.gem_spec = spec
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "install the gem locally"
|
39
|
+
task :install => [:package] do
|
40
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
41
|
+
end
|
42
|
+
|
43
|
+
desc "create a gemspec file"
|
44
|
+
task :make_spec do
|
45
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
46
|
+
file.puts spec.to_ruby
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "Run all examples (or a specific spec with TASK=xxxx)"
|
51
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
52
|
+
t.spec_opts = ["-cfs"]
|
53
|
+
t.spec_files = begin
|
54
|
+
if ENV["TASK"]
|
55
|
+
ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
|
56
|
+
else
|
57
|
+
FileList['spec/**/*_spec.rb']
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/TODO
ADDED
data/lib/moneta.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module Moneta
|
2
|
+
module Expires
|
3
|
+
def check_expired(key)
|
4
|
+
if @expiration[key] && Time.now > @expiration[key]
|
5
|
+
@expiration.delete(key)
|
6
|
+
self.delete(key)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def key?(key)
|
11
|
+
check_expired(key)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
check_expired(key)
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def fetch(key, default = nil, &blk)
|
21
|
+
check_expired(key)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete(key)
|
26
|
+
check_expired(key)
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_key(key, options)
|
31
|
+
update_options(key, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def store(key, value, options = {})
|
35
|
+
ret = super(key, value)
|
36
|
+
update_options(key, options)
|
37
|
+
ret
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def update_options(key, options)
|
42
|
+
if options[:expires_in]
|
43
|
+
@expiration[key] = (Time.now + options[:expires_in])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module StringExpires
|
49
|
+
include Expires
|
50
|
+
|
51
|
+
def check_expired(key)
|
52
|
+
if @expiration[key] && Time.now > Time.at(@expiration[key].to_i)
|
53
|
+
@expiration.delete(key)
|
54
|
+
delete(key)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def update_options(key, options)
|
60
|
+
if options[:expires_in]
|
61
|
+
@expiration[key] = (Time.now + options[:expires_in]).to_i.to_s
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
module Defaults
|
67
|
+
def fetch(key, value = nil)
|
68
|
+
value ||= block_given? ? yield(key) : default
|
69
|
+
self[key] || value
|
70
|
+
end
|
71
|
+
|
72
|
+
def store(key, value, options = {})
|
73
|
+
self[key] = value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
#
|
2
|
+
# Basic File Store
|
3
|
+
# by Hampton Catlin
|
4
|
+
#
|
5
|
+
# This cache simply uses a directory that it creates
|
6
|
+
# and manages to keep your file stores.
|
7
|
+
#
|
8
|
+
# Specify :skip_expires => true if you aren't using
|
9
|
+
# expiration as this will slightly decrease your file size
|
10
|
+
# and memory footprint of the library
|
11
|
+
#
|
12
|
+
# You can optionally also specify a :namespace
|
13
|
+
# option that will create a subfolder.
|
14
|
+
#
|
15
|
+
|
16
|
+
|
17
|
+
require 'fileutils'
|
18
|
+
require File.join(File.dirname(__FILE__), "..", "moneta.rb")
|
19
|
+
|
20
|
+
module Moneta
|
21
|
+
class BasicFile
|
22
|
+
include Defaults
|
23
|
+
|
24
|
+
def initialize(options = {})
|
25
|
+
@namespace = options[:namespace]
|
26
|
+
@directory = ::File.join(options[:path], @namespace.to_s)
|
27
|
+
|
28
|
+
@expires = !options[:skip_expires]
|
29
|
+
|
30
|
+
ensure_directory_created(@directory)
|
31
|
+
end
|
32
|
+
|
33
|
+
def key?(key)
|
34
|
+
!self[key].nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
alias has_key? key?
|
38
|
+
|
39
|
+
def [](key)
|
40
|
+
if ::File.exist?(path(key))
|
41
|
+
data = raw_get(key)
|
42
|
+
if @expires
|
43
|
+
if data[:expires_at].nil? || data[:expires_at] > Time.now
|
44
|
+
data[:value]
|
45
|
+
else
|
46
|
+
delete!(key)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
data
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def raw_get(key)
|
55
|
+
Marshal.load(::File.read(path(key)))
|
56
|
+
end
|
57
|
+
|
58
|
+
def []=(key, value)
|
59
|
+
store(key, value)
|
60
|
+
end
|
61
|
+
|
62
|
+
def store(key, value, options = {})
|
63
|
+
ensure_directory_created(::File.dirname(path(key)))
|
64
|
+
::File.open(path(key), "w") do |file|
|
65
|
+
if @expires
|
66
|
+
data = {:value => value}
|
67
|
+
if options[:expires_in]
|
68
|
+
data[:expires_at] = Time.now + options[:expires_in]
|
69
|
+
end
|
70
|
+
contents = Marshal.dump(data)
|
71
|
+
else
|
72
|
+
contents = Marshal.dump(value)
|
73
|
+
end
|
74
|
+
file.puts(contents)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def update_key(key, options)
|
79
|
+
store(key, self[key], options)
|
80
|
+
end
|
81
|
+
|
82
|
+
def delete!(key)
|
83
|
+
FileUtils.rm(path(key))
|
84
|
+
nil
|
85
|
+
rescue Errno::ENOENT
|
86
|
+
end
|
87
|
+
|
88
|
+
def delete(key)
|
89
|
+
value = self[key]
|
90
|
+
delete!(key)
|
91
|
+
value
|
92
|
+
end
|
93
|
+
|
94
|
+
def clear
|
95
|
+
FileUtils.rm_rf(@directory)
|
96
|
+
FileUtils.mkdir(@directory)
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
def path(key)
|
101
|
+
::File.join(@directory, key.to_s)
|
102
|
+
end
|
103
|
+
|
104
|
+
def ensure_directory_created(directory_path)
|
105
|
+
if ::File.file?(directory_path)
|
106
|
+
raise StandardError, "The path you supplied #{directory_path} is a file"
|
107
|
+
elsif !::File.exists?(directory_path)
|
108
|
+
FileUtils.mkdir_p(directory_path)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
begin
|
2
|
+
require 'bdb'
|
3
|
+
rescue LoadError
|
4
|
+
puts "You need bdb gem to use Bdb moneta store"
|
5
|
+
exit
|
6
|
+
end
|
7
|
+
|
8
|
+
module Moneta
|
9
|
+
|
10
|
+
class Berkeley
|
11
|
+
include Defaults
|
12
|
+
|
13
|
+
def initialize(options={})
|
14
|
+
file = @file = options[:file]
|
15
|
+
@db = Bdb::Db.new()
|
16
|
+
@db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
|
17
|
+
unless options[:skip_expires]
|
18
|
+
@expiration = Moneta::Berkeley.new(:file => "#{file}_expiration", :skip_expires => true )
|
19
|
+
self.extend(StringExpires)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module Implementation
|
24
|
+
def key?(key)
|
25
|
+
nil | self[key]
|
26
|
+
end
|
27
|
+
|
28
|
+
alias has_key? key?
|
29
|
+
|
30
|
+
def []=(key,value)
|
31
|
+
@db[key] = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](key)
|
35
|
+
@db[key]
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete(key)
|
39
|
+
value = self[key]
|
40
|
+
@db.del(nil,key,0) if value
|
41
|
+
value
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear
|
45
|
+
@db.truncate(nil)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
include Implementation
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/moneta/couch.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
begin
|
2
|
+
require "couchrest"
|
3
|
+
rescue LoadError
|
4
|
+
puts "You need the couchrest gem to use the CouchDB store"
|
5
|
+
exit
|
6
|
+
end
|
7
|
+
|
8
|
+
module Moneta
|
9
|
+
class Couch
|
10
|
+
include Defaults
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
@db = ::CouchRest.database!(options[:db])
|
14
|
+
unless options[:skip_expires]
|
15
|
+
@expiration = Moneta::Couch.new(:db => "#{options[:db]}_expiration", :skip_expires => true)
|
16
|
+
self.extend(StringExpires)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def key?(key)
|
21
|
+
!self[key].nil?
|
22
|
+
rescue RestClient::ResourceNotFound
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
alias has_key? key?
|
27
|
+
|
28
|
+
def [](key)
|
29
|
+
@db.get(key)["data"]
|
30
|
+
rescue RestClient::ResourceNotFound
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def []=(key, value)
|
35
|
+
@db.save_doc("_id" => key, :data => value)
|
36
|
+
rescue RestClient::RequestFailed
|
37
|
+
self[key]
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete(key)
|
41
|
+
value = @db.get(key)
|
42
|
+
@db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
|
43
|
+
value["data"]
|
44
|
+
rescue RestClient::ResourceNotFound
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def update_key(key, options = {})
|
49
|
+
val = self[key]
|
50
|
+
self.store(key, val, options)
|
51
|
+
rescue RestClient::ResourceNotFound
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def clear
|
56
|
+
@db.recreate!
|
57
|
+
end
|
58
|
+
|
59
|
+
def delete_store
|
60
|
+
@db.delete!
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|