gotime-moneta 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,52 @@
1
+ begin
2
+ require "localmemcache"
3
+ rescue LoadError
4
+ puts "You need the localmemcache gem to use the LMC moneta store"
5
+ exit
6
+ end
7
+
8
+ module Moneta
9
+ class Expiration
10
+ def initialize(hash)
11
+ @hash = hash
12
+ end
13
+
14
+ def [](key) @hash["#{key}__!__expiration"] end
15
+ def []=(key, value) @hash["#{key}__!__expiration"] = value end
16
+
17
+ def delete(key)
18
+ key = "#{key}__!__expiration"
19
+ value = @hash[key]
20
+ @hash.delete(key)
21
+ value
22
+ end
23
+ end
24
+
25
+ class LMC
26
+ include Defaults
27
+
28
+ module Implementation
29
+ def initialize(options = {})
30
+ @hash = LocalMemCache.new(:filename => options[:filename])
31
+ @expiration = Expiration.new(@hash)
32
+ end
33
+
34
+ def [](key) @hash[key] end
35
+ def []=(key, value) @hash[key] = value end
36
+ def clear() @hash.clear end
37
+
38
+ def key?(key)
39
+ @hash.keys.include?(key)
40
+ end
41
+
42
+ def delete(key)
43
+ value = @hash[key]
44
+ @hash.delete(key)
45
+ value
46
+ end
47
+ end
48
+ include Implementation
49
+ include StringExpires
50
+
51
+ end
52
+ 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 "mongo"
3
+ rescue LoadError
4
+ puts "You need the mongo gem to use the MongoDB moneta store"
5
+ exit
6
+ end
7
+
8
+ module Moneta
9
+ class MongoDB
10
+ include Defaults
11
+
12
+ def initialize(options = {})
13
+ options = {
14
+ :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
15
+ :port => ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT,
16
+ :db => 'cache',
17
+ :collection => 'cache'
18
+ }.update(options)
19
+ conn = XGen::Mongo::Driver::Connection.new(options[:host], options[:port])
20
+ @cache = conn.db(options[:db]).collection(options[:collection])
21
+ end
22
+
23
+ def key?(key)
24
+ !!self[key]
25
+ end
26
+
27
+ def [](key)
28
+ res = @cache.find_first('_id' => key)
29
+ res = nil if res && res['expires'] && Time.now > res['expires']
30
+ res && res['data']
31
+ end
32
+
33
+ def []=(key, value)
34
+ store(key, value)
35
+ end
36
+
37
+ def delete(key)
38
+ value = self[key]
39
+ @cache.remove('_id' => key) if value
40
+ value
41
+ end
42
+
43
+ def store(key, value, options = {})
44
+ exp = options[:expires_in] ? (Time.now + options[:expires_in]) : nil
45
+ @cache.update({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => exp })
46
+ end
47
+
48
+ def update_key(key, options = {})
49
+ val = self[key]
50
+ self.store(key, val, options)
51
+ end
52
+
53
+ def clear
54
+ @cache.clear
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,67 @@
1
+ begin
2
+ require "redis"
3
+ rescue LoadError
4
+ puts "You need the redis gem to use the Redis store"
5
+ exit
6
+ end
7
+
8
+ module Moneta
9
+ class Redis
10
+ include Defaults
11
+
12
+ def initialize(options = {})
13
+ @cache = ::Redis.new(options)
14
+ end
15
+
16
+ def key?(key)
17
+ !@cache[key].nil?
18
+ end
19
+
20
+ alias has_key? key?
21
+
22
+ def [](key)
23
+ @cache.get(key)
24
+ end
25
+
26
+ def []=(key, value)
27
+ store(key, value)
28
+ end
29
+
30
+ def delete(key)
31
+ value = @cache[key]
32
+ if @cache.respond_to?(:delete)
33
+ @cache.delete(key) if value
34
+ else
35
+ @cache.del(key) if value
36
+ end
37
+ value
38
+ end
39
+
40
+ def store(key, value, options = {})
41
+ if options.has_key?(:expires_in)
42
+ if @cache.respond_to?(:setex)
43
+ @cache.setex(key, options[:expires_in], value)
44
+ elsif @cache.respond_to?(:set_with_expire)
45
+ @cache.set_with_expire(key, value, options[:expires_in])
46
+ else
47
+ @cache.set(key, value, options[:expires_in])
48
+ end
49
+ else
50
+ @cache.set(key, value)
51
+ end
52
+ end
53
+
54
+ def update_key(key, options = {})
55
+ val = @cache[key]
56
+ self.store(key, val, options)
57
+ end
58
+
59
+ def clear
60
+ if @cache.respond_to?(:flush_db)
61
+ @cache.flush_db
62
+ else
63
+ @cache.flushdb
64
+ end
65
+ end
66
+ end
67
+ end