akdubya-cushion 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cushion.rb +1 -1
- data/lib/cushion/database.rb +1 -1
- data/lib/cushion/utils/moneta.rb +89 -0
- data/spec/moneta_spec.rb +138 -0
- metadata +5 -2
data/lib/cushion.rb
CHANGED
data/lib/cushion/database.rb
CHANGED
@@ -236,7 +236,7 @@ module Cushion
|
|
236
236
|
if opts.empty? && (id == dest_id)
|
237
237
|
opts = { :rev => src.headers[:etag] }
|
238
238
|
end
|
239
|
-
res = attach(dest_id, new_filename, src,
|
239
|
+
res = attach(dest_id, new_filename, src, headers.merge(opts))
|
240
240
|
if id == dest_id
|
241
241
|
res.merge("src_rev" => res["rev"])
|
242
242
|
else
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'cushion'
|
2
|
+
require 'moneta'
|
3
|
+
|
4
|
+
module Moneta
|
5
|
+
class Cushion
|
6
|
+
class Expiration
|
7
|
+
def initialize(db)
|
8
|
+
@db = db
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](key)
|
12
|
+
if obj = get(key)
|
13
|
+
Time.parse(obj[:expires]) if obj[:expires]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def []=(key, value)
|
18
|
+
obj = get(key)
|
19
|
+
obj[:expires] = value
|
20
|
+
obj.save
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(key)
|
24
|
+
obj = get(key)
|
25
|
+
obj[:expires] = nil
|
26
|
+
obj.save
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def get(key)
|
31
|
+
@db.doc(key) rescue nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(options = {})
|
36
|
+
@db = Cushion!(options.delete(:dbname), options)
|
37
|
+
@expiration = Expiration.new(@db)
|
38
|
+
end
|
39
|
+
|
40
|
+
module Implementation
|
41
|
+
def key?(key)
|
42
|
+
@db.key?(key)
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_key?(key)
|
46
|
+
@db.has_key?(key)
|
47
|
+
end
|
48
|
+
|
49
|
+
def [](key)
|
50
|
+
obj = @db.doc(key)
|
51
|
+
obj && obj[:value]
|
52
|
+
rescue
|
53
|
+
end
|
54
|
+
|
55
|
+
def []=(key, value)
|
56
|
+
obj = @db.doc(key) rescue nil
|
57
|
+
if obj
|
58
|
+
obj[:value] = value
|
59
|
+
obj.save
|
60
|
+
else
|
61
|
+
@db.store('_id' => key, 'value' => value)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def fetch(key, default)
|
66
|
+
self[key] || default
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete(key)
|
70
|
+
obj = @db.doc(key)
|
71
|
+
obj.destroy
|
72
|
+
obj[:value]
|
73
|
+
rescue
|
74
|
+
end
|
75
|
+
|
76
|
+
def store(key, value, options = {})
|
77
|
+
self[key] = value
|
78
|
+
end
|
79
|
+
|
80
|
+
def clear
|
81
|
+
docs = @db.all['rows'].map { |row| { '_id' => row['id'],
|
82
|
+
'_rev' => row['value']['rev'], '_deleted' => true } }
|
83
|
+
@db.bulk(docs, :use_uuids => false)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
include Implementation
|
87
|
+
include Expires
|
88
|
+
end
|
89
|
+
end
|
data/spec/moneta_spec.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
begin
|
2
|
+
require File.dirname(File.dirname(__FILE__)) + '/lib/cushion/utils/moneta'
|
3
|
+
MONETA_AVAILABLE = true
|
4
|
+
rescue LoadError
|
5
|
+
puts "You need the Moneta gem to run the Moneta tests."
|
6
|
+
MONETA_AVAILABLE = false
|
7
|
+
end
|
8
|
+
|
9
|
+
shared "a read/write Moneta cache" do
|
10
|
+
it "reads from keys like a Hash" do
|
11
|
+
@cache["key"].should == nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "writes to a key like a Hash" do
|
15
|
+
@cache["key"] = "value"
|
16
|
+
@cache["key"].should == "value"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "specifies that it is writable via frozen?" do
|
20
|
+
@cache.should.not.be.frozen
|
21
|
+
end
|
22
|
+
|
23
|
+
it "if a key is not available, returns false from key?" do
|
24
|
+
@cache.key?("key").should.be.false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "if a key is available, returns true from key?" do
|
28
|
+
@cache["key"] = "value"
|
29
|
+
@cache.key?("key").should.be.true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "if it exists, removes and return an element from the backing store via delete" do
|
33
|
+
@cache["key"] = "value"
|
34
|
+
@cache.delete("key").should == "value"
|
35
|
+
@cache.key?("key").should.be.false
|
36
|
+
end
|
37
|
+
|
38
|
+
it "if it does not exists, returns nil from delete" do
|
39
|
+
@cache.delete("key").should.be.nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "removes all keys from the store with clear" do
|
43
|
+
@cache["key"] = "value"
|
44
|
+
@cache["key2"] = "value2"
|
45
|
+
@cache.clear
|
46
|
+
@cache.key?("key").should.not.be.true
|
47
|
+
@cache.key?("key2").should.not.be.true
|
48
|
+
end
|
49
|
+
|
50
|
+
it "fetches a key with a default value with fetch, if the key is not available" do
|
51
|
+
@cache.fetch("key", "value").should == "value"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "fetches a key with a default value with fetch, if the key is available" do
|
55
|
+
@cache["key"] = "value2"
|
56
|
+
@cache.fetch("key", "value").should == "value2"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "stores values with #store" do
|
60
|
+
@cache.store("key", "value")
|
61
|
+
@cache["key"].should == "value"
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "when storing values with #store, and providing an expiration" do
|
65
|
+
before do
|
66
|
+
@cache.store("key", "value", :expires_in => 2)
|
67
|
+
end
|
68
|
+
|
69
|
+
shared "not expired" do
|
70
|
+
it "still has the key" do
|
71
|
+
@cache.key?("key").should.be.true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns the value when indexed" do
|
75
|
+
@cache["key"].should == "value"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns the value when fetched" do
|
79
|
+
@cache.fetch("key", "value2").should == "value"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns the value when deleted" do
|
83
|
+
@cache.delete("key").should == "value"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "when expired" do
|
88
|
+
before do
|
89
|
+
if @native_expires
|
90
|
+
sleep 2
|
91
|
+
else
|
92
|
+
time = Time.now
|
93
|
+
Time.stub!(:now).and_return { time + 2 }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it "no longer has the key" do
|
98
|
+
@cache.key?("key").should.be.false
|
99
|
+
end
|
100
|
+
|
101
|
+
it "returns nil when indexed" do
|
102
|
+
@cache["key"].should == nil
|
103
|
+
end
|
104
|
+
|
105
|
+
it "returns the default value when fetched" do
|
106
|
+
@cache.fetch("key", "value2").should == "value2"
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns nil when deleting the expired key" do
|
110
|
+
@cache.delete("key").should == nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "when not expired" do
|
115
|
+
behaves_like "not expired"
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "after updating the expiry with update_key, and waiting for the initial expiry to pass" do
|
119
|
+
before do
|
120
|
+
@cache.update_key("key", :expires_in => 2)
|
121
|
+
end
|
122
|
+
|
123
|
+
behaves_like "not expired"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
if MONETA_AVAILABLE
|
129
|
+
describe "Moneta::Cushion" do
|
130
|
+
before do
|
131
|
+
@cache = Moneta::Cushion.new(:dbname => :moneta_test)
|
132
|
+
@cache.clear
|
133
|
+
@native_expires = true # => Getting around stubbing issue
|
134
|
+
end
|
135
|
+
|
136
|
+
behaves_like "a read/write Moneta cache"
|
137
|
+
end
|
138
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: akdubya-cushion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksander Williams
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-14 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -36,6 +36,8 @@ files:
|
|
36
36
|
- LICENSE
|
37
37
|
- lib/cushion
|
38
38
|
- lib/cushion/database.rb
|
39
|
+
- lib/cushion/utils
|
40
|
+
- lib/cushion/utils/moneta.rb
|
39
41
|
- lib/cushion/design.rb
|
40
42
|
- lib/cushion/server.rb
|
41
43
|
- lib/cushion/document.rb
|
@@ -44,6 +46,7 @@ files:
|
|
44
46
|
- spec/server_spec.rb
|
45
47
|
- spec/document_spec.rb
|
46
48
|
- spec/database_spec.rb
|
49
|
+
- spec/moneta_spec.rb
|
47
50
|
- spec/helpers.rb
|
48
51
|
- spec/cushion_spec.rb
|
49
52
|
has_rdoc: true
|