benschwarz-moneta 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,51 @@
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 supports 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
+ #[]=(key, value):: set a value for a key. if the key is already used, clobber it.
35
+ keys set using []= will never expire
36
+
37
+ #delete(key):: delete the key from the store and return the current value
38
+
39
+ #key?(key):: true if the key exists, false if it does not
40
+
41
+ #has_key?(key):: alias for key?
42
+
43
+ #store(key, value, options):: same as []=, but you can supply an :expires_in option,
44
+ which will specify a number of seconds before the key
45
+ should expire. In order to support the same features
46
+ across all stores, only full seconds are supported
47
+
48
+ #update_key(key, options):: updates an existing key with a new :expires_in option.
49
+ if the key has already expired, it will not be updated.
50
+
51
+ #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 = "moneta"
8
+ GEM_VERSION = "0.5.0"
9
+ AUTHOR = "Yehuda Katz"
10
+ EMAIL = "wycats@gmail.com"
11
+ HOMEPAGE = "http://www.yehudakatz.com"
12
+ SUMMARY = "A unified interface to key/value stores"
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
@@ -0,0 +1,4 @@
1
+ TODO:
2
+ Fix LICENSE with your name
3
+ Fix Rakefile with your name and contact info
4
+ Add your code to lib/<%= name %>.rb
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,117 @@
1
+ begin
2
+ gem "dm-core", "0.9.10"
3
+ require "dm-core"
4
+ rescue LoadError
5
+ puts "You need the dm-core gem in order to use the DataMapper moneta store"
6
+ exit
7
+ end
8
+
9
+ class MonetaHash
10
+ include DataMapper::Resource
11
+
12
+ property :the_key, String, :key => true
13
+ property :value, Object, :lazy => false
14
+ property :expires, Time
15
+
16
+ def self.value(key)
17
+ obj = self.get(key)
18
+ obj && obj.value
19
+ end
20
+ end
21
+
22
+ module Moneta
23
+ class DataMapper
24
+ class Expiration
25
+ def initialize(klass, repository)
26
+ @klass = klass
27
+ @repository = repository
28
+ end
29
+
30
+ def [](key)
31
+ if obj = get(key)
32
+ obj.expires
33
+ end
34
+ end
35
+
36
+ def []=(key, value)
37
+ obj = get(key)
38
+ obj.expires = value
39
+ obj.save(@repository)
40
+ end
41
+
42
+ def delete(key)
43
+ obj = get(key)
44
+ obj.expires = nil
45
+ obj.save(@repository)
46
+ end
47
+
48
+ private
49
+ def get(key)
50
+ repository(@repository) { @klass.get(key) }
51
+ end
52
+ end
53
+
54
+ def initialize(options = {})
55
+ @repository = options.delete(:repository) || :moneta
56
+ ::DataMapper.setup(@repository, options[:setup])
57
+ repository_context { MonetaHash.auto_upgrade! }
58
+ @hash = MonetaHash
59
+ @expiration = Expiration.new(MonetaHash, @repository)
60
+ end
61
+
62
+ module Implementation
63
+ def key?(key)
64
+ repository_context { !!@hash.get(key) }
65
+ end
66
+
67
+ def has_key?(key)
68
+ repository_context { !!@hash.get(key) }
69
+ end
70
+
71
+ def [](key)
72
+ repository_context { @hash.value(key) }
73
+ end
74
+
75
+ def []=(key, value)
76
+ repository_context {
77
+ obj = @hash.get(key)
78
+ if obj
79
+ obj.update(key, value)
80
+ else
81
+ @hash.create(:the_key => key, :value => value)
82
+ end
83
+ }
84
+ end
85
+
86
+ def fetch(key, value = nil)
87
+ repository_context {
88
+ value ||= block_given? ? yield(key) : default
89
+ self[key] || value
90
+ }
91
+ end
92
+
93
+ def delete(key)
94
+ repository_context {
95
+ value = self[key]
96
+ @hash.all(:the_key => key).destroy!
97
+ value
98
+ }
99
+ end
100
+
101
+ def store(key, value, options = {})
102
+ repository_context { self[key] = value }
103
+ end
104
+
105
+ def clear
106
+ repository_context { @hash.all.destroy! }
107
+ end
108
+
109
+ private
110
+ def repository_context
111
+ repository(@repository) { yield }
112
+ end
113
+ end
114
+ include Implementation
115
+ include Expires
116
+ end
117
+ end
@@ -0,0 +1,91 @@
1
+ begin
2
+ require "xattr"
3
+ rescue LoadError
4
+ puts "You need the xattr gem to use the File moneta store"
5
+ exit
6
+ end
7
+ require "fileutils"
8
+
9
+ module Moneta
10
+ class File
11
+ class Expiration
12
+ def initialize(directory)
13
+ @directory = directory
14
+ end
15
+
16
+ def [](key)
17
+ attrs = xattr(key)
18
+ ret = Marshal.load(attrs.get("moneta_expires"))
19
+ rescue Errno::ENOENT, SystemCallError
20
+ end
21
+
22
+ def []=(key, value)
23
+ attrs = xattr(key)
24
+ attrs.set("moneta_expires", Marshal.dump(value))
25
+ end
26
+
27
+ def delete(key)
28
+ attrs = xattr(key)
29
+ attrs.remove("moneta_expires")
30
+ end
31
+
32
+ private
33
+ def xattr(key)
34
+ ::Xattr.new(::File.join(@directory, key))
35
+ end
36
+ end
37
+
38
+ def initialize(options = {})
39
+ @directory = options[:path]
40
+ if ::File.file?(@directory)
41
+ raise StandardError, "The path you supplied #{@directory} is a file"
42
+ elsif !::File.exists?(@directory)
43
+ FileUtils.mkdir_p(@directory)
44
+ end
45
+
46
+ @expiration = Expiration.new(@directory)
47
+ end
48
+
49
+ module Implementation
50
+ def key?(key)
51
+ ::File.exist?(path(key))
52
+ end
53
+
54
+ alias has_key? key?
55
+
56
+ def [](key)
57
+ if ::File.exist?(path(key))
58
+ Marshal.load(::File.read(path(key)))
59
+ end
60
+ end
61
+
62
+ def []=(key, value)
63
+ ::File.open(path(key), "w") do |file|
64
+ contents = Marshal.dump(value)
65
+ file.puts(contents)
66
+ end
67
+ end
68
+
69
+ def delete(key)
70
+ value = self[key]
71
+ FileUtils.rm(path(key))
72
+ value
73
+ rescue Errno::ENOENT
74
+ end
75
+
76
+ def clear
77
+ FileUtils.rm_rf(@directory)
78
+ FileUtils.mkdir(@directory)
79
+ end
80
+
81
+ private
82
+ def path(key)
83
+ ::File.join(@directory, key.to_s)
84
+ end
85
+ end
86
+ include Implementation
87
+ include Defaults
88
+ include Expires
89
+
90
+ end
91
+ end
@@ -0,0 +1,53 @@
1
+ begin
2
+ require "memcached"
3
+ MemCache = Memcached
4
+ rescue LoadError
5
+ require "memcache"
6
+ rescue
7
+ puts "You need either the `memcached` or `memcache-client` gem to use the Memcache moneta store"
8
+ exit
9
+ end
10
+
11
+ module Moneta
12
+ class Memcache
13
+ include Defaults
14
+
15
+ def initialize(options = {})
16
+ @cache = MemCache.new(options.delete(:server), options)
17
+ end
18
+
19
+ def key?(key)
20
+ !self[key].nil?
21
+ end
22
+
23
+ alias has_key? key?
24
+
25
+ def [](key)
26
+ @cache.get(key)
27
+ end
28
+
29
+ def []=(key, value)
30
+ store(key, value)
31
+ end
32
+
33
+ def delete(key)
34
+ value = self[key]
35
+ @cache.delete(key) if value
36
+ value
37
+ end
38
+
39
+ def store(key, value, options = {})
40
+ args = [key, value, options[:expires_in]].compact
41
+ @cache.set(*args)
42
+ end
43
+
44
+ def update_key(key, options = {})
45
+ val = self[key]
46
+ self.store(key, val, options)
47
+ end
48
+
49
+ def clear
50
+ @cache.flush_all
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ module Moneta
2
+ class Memory < Hash
3
+ include Expires
4
+
5
+ def initialize(*args)
6
+ @expiration = {}
7
+ super
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ begin
2
+ require "xattr"
3
+ rescue LoadError
4
+ puts "You need the xattr gem to use the Xattr moneta store"
5
+ exit
6
+ end
7
+ require "fileutils"
8
+
9
+ module Moneta
10
+ class Xattr
11
+ include Defaults
12
+
13
+ def initialize(options = {})
14
+ file = options[:file]
15
+ @hash = ::Xattr.new(file)
16
+ FileUtils.mkdir_p(::File.dirname(file))
17
+ FileUtils.touch(file)
18
+ unless options[:skip_expires]
19
+ @expiration = Moneta::Xattr.new(:file => "#{file}_expiration", :skip_expires => true)
20
+ self.extend(Expires)
21
+ end
22
+ end
23
+
24
+ module Implementation
25
+
26
+ def key?(key)
27
+ @hash.list.include?(key)
28
+ end
29
+
30
+ alias has_key? key?
31
+
32
+ def [](key)
33
+ return nil unless key?(key)
34
+ Marshal.load(@hash.get(key))
35
+ end
36
+
37
+ def []=(key, value)
38
+ @hash.set(key, Marshal.dump(value))
39
+ end
40
+
41
+ def delete(key)
42
+ return nil unless key?(key)
43
+ value = self[key]
44
+ @hash.remove(key)
45
+ value
46
+ end
47
+
48
+ def clear
49
+ @hash.list.each do |item|
50
+ @hash.remove(item)
51
+ end
52
+ end
53
+
54
+ end
55
+ include Implementation
56
+
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benschwarz-moneta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Yehuda Katz
8
+ autorequire: moneta
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-12 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A unified interface to key/value stores
17
+ email: wycats@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - lib/moneta
32
+ - lib/moneta/datamapper.rb
33
+ - lib/moneta/file.rb
34
+ - lib/moneta/memcache.rb
35
+ - lib/moneta/memory.rb
36
+ - lib/moneta/xattr.rb
37
+ - lib/moneta.rb
38
+ has_rdoc: true
39
+ homepage: http://www.yehudakatz.com
40
+ licenses:
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: A unified interface to key/value stores
65
+ test_files: []
66
+