wycats-moneta 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +6 -0
- data/lib/moneta.rb +42 -16
- data/lib/moneta/datamapper.rb +51 -41
- data/lib/moneta/file.rb +6 -15
- data/lib/moneta/memcache.rb +16 -16
- data/lib/moneta/xattr.rb +2 -11
- metadata +1 -1
data/README
CHANGED
@@ -10,6 +10,12 @@ Out of the box, it supports:
|
|
10
10
|
* In-memory store
|
11
11
|
* The xattrs in a file system
|
12
12
|
* DataMapper
|
13
|
+
* S3
|
14
|
+
* Berkeley DB
|
15
|
+
* Redis
|
16
|
+
* SDBM
|
17
|
+
* Tokyo
|
18
|
+
* CouchDB
|
13
19
|
|
14
20
|
All stores support key expiration, but only memcache supports it natively. All other stores
|
15
21
|
emulate expiration.
|
data/lib/moneta.rb
CHANGED
@@ -6,40 +6,37 @@ module Moneta
|
|
6
6
|
self.delete(key)
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
10
|
-
def
|
9
|
+
|
10
|
+
def key?(key)
|
11
11
|
check_expired(key)
|
12
12
|
super
|
13
13
|
end
|
14
|
-
|
15
|
-
def key?(key)
|
16
|
-
has_key?(key)
|
17
|
-
end
|
18
|
-
|
14
|
+
|
19
15
|
def [](key)
|
20
16
|
check_expired(key)
|
21
17
|
super
|
22
18
|
end
|
23
|
-
|
24
|
-
def fetch(key, default)
|
19
|
+
|
20
|
+
def fetch(key, default = nil, &blk)
|
25
21
|
check_expired(key)
|
26
22
|
super
|
27
23
|
end
|
28
|
-
|
24
|
+
|
29
25
|
def delete(key)
|
30
26
|
check_expired(key)
|
31
27
|
super
|
32
28
|
end
|
33
|
-
|
29
|
+
|
34
30
|
def update_key(key, options)
|
35
|
-
update_options(key, options)
|
31
|
+
update_options(key, options)
|
36
32
|
end
|
37
|
-
|
33
|
+
|
38
34
|
def store(key, value, options = {})
|
39
|
-
super(key, value)
|
35
|
+
ret = super(key, value)
|
40
36
|
update_options(key, options)
|
37
|
+
ret
|
41
38
|
end
|
42
|
-
|
39
|
+
|
43
40
|
private
|
44
41
|
def update_options(key, options)
|
45
42
|
if options[:expires_in]
|
@@ -47,4 +44,33 @@ module Moneta
|
|
47
44
|
end
|
48
45
|
end
|
49
46
|
end
|
50
|
-
|
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
|
data/lib/moneta/datamapper.rb
CHANGED
@@ -8,15 +8,11 @@ end
|
|
8
8
|
|
9
9
|
class MonetaHash
|
10
10
|
include DataMapper::Resource
|
11
|
-
|
12
|
-
def self.default_repository_name
|
13
|
-
:moneta
|
14
|
-
end
|
15
|
-
|
11
|
+
|
16
12
|
property :the_key, String, :key => true
|
17
13
|
property :value, Object, :lazy => false
|
18
14
|
property :expires, Time
|
19
|
-
|
15
|
+
|
20
16
|
def self.value(key)
|
21
17
|
obj = self.get(key)
|
22
18
|
obj && obj.value
|
@@ -26,79 +22,93 @@ end
|
|
26
22
|
module Moneta
|
27
23
|
class DataMapper
|
28
24
|
class Expiration
|
29
|
-
def initialize(klass)
|
25
|
+
def initialize(klass, repository)
|
30
26
|
@klass = klass
|
27
|
+
@repository = repository
|
31
28
|
end
|
32
|
-
|
29
|
+
|
33
30
|
def [](key)
|
34
31
|
if obj = get(key)
|
35
32
|
obj.expires
|
36
33
|
end
|
37
34
|
end
|
38
|
-
|
35
|
+
|
39
36
|
def []=(key, value)
|
40
37
|
obj = get(key)
|
41
38
|
obj.expires = value
|
42
|
-
obj.save
|
39
|
+
obj.save(@repository)
|
43
40
|
end
|
44
|
-
|
41
|
+
|
45
42
|
def delete(key)
|
46
43
|
obj = get(key)
|
47
44
|
obj.expires = nil
|
48
|
-
obj.save
|
45
|
+
obj.save(@repository)
|
49
46
|
end
|
50
|
-
|
47
|
+
|
51
48
|
private
|
52
49
|
def get(key)
|
53
|
-
@klass.get(key)
|
50
|
+
repository(@repository) { @klass.get(key) }
|
54
51
|
end
|
55
52
|
end
|
56
|
-
|
53
|
+
|
57
54
|
def initialize(options = {})
|
58
|
-
|
59
|
-
|
55
|
+
@repository = options.delete(:repository) || :moneta
|
56
|
+
::DataMapper.setup(@repository, options[:setup])
|
57
|
+
repository_context { MonetaHash.auto_upgrade! }
|
60
58
|
@hash = MonetaHash
|
61
|
-
@expiration = Expiration.new(MonetaHash)
|
59
|
+
@expiration = Expiration.new(MonetaHash, @repository)
|
62
60
|
end
|
63
|
-
|
61
|
+
|
64
62
|
module Implementation
|
65
63
|
def key?(key)
|
66
|
-
!!@hash.get(key)
|
64
|
+
repository_context { !!@hash.get(key) }
|
67
65
|
end
|
68
|
-
|
66
|
+
|
69
67
|
def has_key?(key)
|
70
|
-
!!@hash.get(key)
|
68
|
+
repository_context { !!@hash.get(key) }
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
def [](key)
|
74
|
-
@hash.value(key)
|
72
|
+
repository_context { @hash.value(key) }
|
75
73
|
end
|
76
74
|
|
77
75
|
def []=(key, value)
|
78
|
-
|
79
|
-
|
80
|
-
obj
|
81
|
-
|
82
|
-
|
83
|
-
|
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
84
|
end
|
85
|
-
|
86
|
-
def fetch(key,
|
87
|
-
|
85
|
+
|
86
|
+
def fetch(key, value = nil)
|
87
|
+
repository_context {
|
88
|
+
value ||= block_given? ? yield(key) : default
|
89
|
+
self[key] || value
|
90
|
+
}
|
88
91
|
end
|
89
|
-
|
92
|
+
|
90
93
|
def delete(key)
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
+
repository_context {
|
95
|
+
value = self[key]
|
96
|
+
@hash.all(:the_key => key).destroy!
|
97
|
+
value
|
98
|
+
}
|
94
99
|
end
|
95
|
-
|
100
|
+
|
96
101
|
def store(key, value, options = {})
|
97
|
-
self[key] = value
|
102
|
+
repository_context { self[key] = value }
|
98
103
|
end
|
99
|
-
|
104
|
+
|
100
105
|
def clear
|
101
|
-
@hash.all.destroy!
|
106
|
+
repository_context { @hash.all.destroy! }
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
def repository_context
|
111
|
+
repository(@repository) { yield }
|
102
112
|
end
|
103
113
|
end
|
104
114
|
include Implementation
|
data/lib/moneta/file.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
begin
|
2
|
-
require "fileutils"
|
3
2
|
require "xattr"
|
4
3
|
rescue LoadError
|
5
4
|
puts "You need the xattr gem to use the File moneta store"
|
6
5
|
exit
|
7
6
|
end
|
7
|
+
require "fileutils"
|
8
8
|
|
9
9
|
module Moneta
|
10
10
|
class File
|
@@ -40,7 +40,7 @@ module Moneta
|
|
40
40
|
if ::File.file?(@directory)
|
41
41
|
raise StandardError, "The path you supplied #{@directory} is a file"
|
42
42
|
elsif !::File.exists?(@directory)
|
43
|
-
FileUtils.
|
43
|
+
FileUtils.mkdir_p(@directory)
|
44
44
|
end
|
45
45
|
|
46
46
|
@expiration = Expiration.new(@directory)
|
@@ -51,9 +51,7 @@ module Moneta
|
|
51
51
|
::File.exist?(path(key))
|
52
52
|
end
|
53
53
|
|
54
|
-
|
55
|
-
::File.exist?(path(key))
|
56
|
-
end
|
54
|
+
alias has_key? key?
|
57
55
|
|
58
56
|
def [](key)
|
59
57
|
if ::File.exist?(path(key))
|
@@ -67,22 +65,14 @@ module Moneta
|
|
67
65
|
file.puts(contents)
|
68
66
|
end
|
69
67
|
end
|
70
|
-
|
71
|
-
def fetch(key, default)
|
72
|
-
self[key] || default
|
73
|
-
end
|
74
|
-
|
68
|
+
|
75
69
|
def delete(key)
|
76
70
|
value = self[key]
|
77
71
|
FileUtils.rm(path(key))
|
78
72
|
value
|
79
73
|
rescue Errno::ENOENT
|
80
74
|
end
|
81
|
-
|
82
|
-
def store(key, value, options = {})
|
83
|
-
self[key] = value
|
84
|
-
end
|
85
|
-
|
75
|
+
|
86
76
|
def clear
|
87
77
|
FileUtils.rm_rf(@directory)
|
88
78
|
FileUtils.mkdir(@directory)
|
@@ -94,6 +84,7 @@ module Moneta
|
|
94
84
|
end
|
95
85
|
end
|
96
86
|
include Implementation
|
87
|
+
include Defaults
|
97
88
|
include Expires
|
98
89
|
|
99
90
|
end
|
data/lib/moneta/memcache.rb
CHANGED
@@ -1,52 +1,52 @@
|
|
1
1
|
begin
|
2
|
+
require "memcached"
|
3
|
+
rescue LoadError
|
2
4
|
require "memcache"
|
3
5
|
rescue LoadError
|
4
6
|
puts "You need the memcache gem to use the Memcache moneta store"
|
5
|
-
exit
|
7
|
+
exit
|
6
8
|
end
|
7
9
|
|
8
|
-
module Moneta
|
10
|
+
module Moneta
|
9
11
|
class Memcache
|
12
|
+
include Defaults
|
13
|
+
|
10
14
|
def initialize(options = {})
|
11
15
|
@cache = MemCache.new(options.delete(:server), options)
|
12
16
|
end
|
13
|
-
|
17
|
+
|
14
18
|
def key?(key)
|
15
|
-
!self[
|
19
|
+
!self[key].nil?
|
16
20
|
end
|
17
|
-
|
21
|
+
|
18
22
|
alias has_key? key?
|
19
|
-
|
23
|
+
|
20
24
|
def [](key)
|
21
25
|
@cache.get(key)
|
22
26
|
end
|
23
|
-
|
27
|
+
|
24
28
|
def []=(key, value)
|
25
29
|
store(key, value)
|
26
30
|
end
|
27
|
-
|
28
|
-
def fetch(key, default)
|
29
|
-
self[key] || default
|
30
|
-
end
|
31
|
-
|
31
|
+
|
32
32
|
def delete(key)
|
33
33
|
value = self[key]
|
34
34
|
@cache.delete(key) if value
|
35
35
|
value
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def store(key, value, options = {})
|
39
39
|
args = [key, value, options[:expires_in]].compact
|
40
40
|
@cache.set(*args)
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
def update_key(key, options = {})
|
44
44
|
val = self[key]
|
45
45
|
self.store(key, val, options)
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def clear
|
49
49
|
@cache.flush_all
|
50
50
|
end
|
51
51
|
end
|
52
|
-
end
|
52
|
+
end
|
data/lib/moneta/xattr.rb
CHANGED
@@ -8,6 +8,7 @@ require "fileutils"
|
|
8
8
|
|
9
9
|
module Moneta
|
10
10
|
class Xattr
|
11
|
+
include Defaults
|
11
12
|
|
12
13
|
def initialize(options = {})
|
13
14
|
file = options[:file]
|
@@ -26,9 +27,7 @@ module Moneta
|
|
26
27
|
@hash.list.include?(key)
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
@hash.list.include?(key)
|
31
|
-
end
|
30
|
+
alias has_key? key?
|
32
31
|
|
33
32
|
def [](key)
|
34
33
|
return nil unless key?(key)
|
@@ -39,10 +38,6 @@ module Moneta
|
|
39
38
|
@hash.set(key, Marshal.dump(value))
|
40
39
|
end
|
41
40
|
|
42
|
-
def fetch(key, value)
|
43
|
-
self[key] || value
|
44
|
-
end
|
45
|
-
|
46
41
|
def delete(key)
|
47
42
|
return nil unless key?(key)
|
48
43
|
value = self[key]
|
@@ -50,10 +45,6 @@ module Moneta
|
|
50
45
|
value
|
51
46
|
end
|
52
47
|
|
53
|
-
def store(key, value, options = {})
|
54
|
-
self[key] = value
|
55
|
-
end
|
56
|
-
|
57
48
|
def clear
|
58
49
|
@hash.list.each do |item|
|
59
50
|
@hash.remove(item)
|