elephas 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -6
- data/doc/Elephas/Cache.html +304 -73
- data/doc/Elephas/Entry.html +153 -35
- data/doc/Elephas/Providers/Base.html +26 -19
- data/doc/Elephas/Providers/Hash.html +19 -12
- data/doc/Elephas/Providers/RubyOnRails.html +12 -7
- data/doc/Elephas/Providers.html +1 -1
- data/doc/Elephas/Version.html +3 -3
- data/doc/Elephas.html +1 -1
- data/doc/_index.html +1 -1
- data/doc/file.README.html +35 -9
- data/doc/index.html +35 -9
- data/doc/method_list.html +56 -32
- data/doc/top-level-namespace.html +1 -1
- data/lib/elephas/cache.rb +67 -24
- data/lib/elephas/entry.rb +14 -4
- data/lib/elephas/provider.rb +7 -6
- data/lib/elephas/providers/hash.rb +3 -2
- data/lib/elephas/providers/ruby_on_rails.rb +4 -4
- data/lib/elephas/version.rb +2 -2
- data/spec/elephas/cache_spec.rb +77 -10
- data/spec/elephas/entry_spec.rb +90 -0
- metadata +4 -4
@@ -21,15 +21,15 @@ module Elephas
|
|
21
21
|
# Writes a value to the cache.
|
22
22
|
#
|
23
23
|
# @param key [String] The key to associate the value with.
|
24
|
-
# @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
|
25
|
-
# @param options [Hash] A list of options for writing.
|
24
|
+
# @param value [Object] The value to write. **Setting a value to `nil` **doesn't** mean *deleting* the value.
|
25
|
+
# @param options [Hash] A list of options for writing.
|
26
|
+
# @see Elephas::Cache.setup_options
|
26
27
|
# @return [Object] The value itself.
|
27
28
|
def write(key, value, options = {})
|
28
|
-
ttl = [options[:ttl].to_integer, 0].max
|
29
29
|
fvalue = ::Elephas::Entry.ensure(value, key, options)
|
30
30
|
fvalue.refresh
|
31
31
|
|
32
|
-
Rails.cache.write(key, value, :expires_in => ttl)
|
32
|
+
Rails.cache.write(key, value, :expires_in => fvalue.ttl)
|
33
33
|
value
|
34
34
|
end
|
35
35
|
|
data/lib/elephas/version.rb
CHANGED
data/spec/elephas/cache_spec.rb
CHANGED
@@ -6,36 +6,103 @@
|
|
6
6
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
|
-
describe Elephas::Cache do
|
10
|
-
|
9
|
+
describe ::Elephas::Cache do
|
10
|
+
let(:entry) { ::Elephas::Entry.ensure("VALUE", ::Elephas::Cache.default_prefix + "[KEY]", {:ttl => 3600}) }
|
11
11
|
|
12
|
+
describe ".use" do
|
13
|
+
before(:each) do
|
14
|
+
::Elephas::Cache.provider = Elephas::Providers::Hash.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should use the provider for reading the value" do
|
18
|
+
::Elephas::Cache.provider.should_receive(:read)
|
19
|
+
::Elephas::Cache.use("KEY") do "VALUE" end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should skip the provider if requested to" do
|
23
|
+
::Elephas::Cache.use("KEY", {:ttl => 0}) do "VALUE" end
|
24
|
+
::Elephas::Cache.provider.should_not_receive(:read)
|
25
|
+
::Elephas::Cache.use("KEY", {:force => true}) do "VALUE" end
|
26
|
+
::Elephas::Cache.provider.should_not_receive(:read)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should use the block for value computation" do
|
30
|
+
expect{ ::Elephas::Cache.use("KEY") do raise ArgumentError end }.to raise_error(ArgumentError)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should not use the block if the value is valid" do
|
34
|
+
::Elephas::Cache.use("KEY") do entry end
|
35
|
+
expect{ ::Elephas::Cache.use("KEY") do raise ArgumentError end }.not_to raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should store the value in the cache" do
|
39
|
+
::Elephas::Cache.use("KEY") do entry end
|
40
|
+
expect(::Elephas::Cache.provider.read(entry.hash)).to eq(entry)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return the entire entry or only the value" do
|
44
|
+
::Elephas::Cache.use("KEY") do "VALUE" end
|
45
|
+
|
46
|
+
expect(::Elephas::Cache.use("KEY")).to eq("VALUE")
|
47
|
+
value = ::Elephas::Cache.use("KEY", {:as_entry => true})
|
48
|
+
expect(value).to be_a(::Elephas::Entry)
|
49
|
+
expect(value.value).to eq("VALUE")
|
50
|
+
end
|
12
51
|
end
|
13
52
|
|
14
53
|
describe ".read" do
|
15
54
|
it "should be forwarded to the provider" do
|
16
|
-
Elephas::Cache.provider.should_receive(:read)
|
17
|
-
Elephas::Cache.read("KEY")
|
55
|
+
::Elephas::Cache.provider.should_receive(:read)
|
56
|
+
::Elephas::Cache.read("KEY")
|
18
57
|
end
|
19
58
|
end
|
20
59
|
|
21
60
|
describe ".write" do
|
22
61
|
it "should be forwarded to the provider" do
|
23
|
-
Elephas::Cache.provider.should_receive(:write)
|
24
|
-
Elephas::Cache.write("KEY", "VALUE")
|
62
|
+
::Elephas::Cache.provider.should_receive(:write)
|
63
|
+
::Elephas::Cache.write("KEY", "VALUE")
|
25
64
|
end
|
26
65
|
end
|
27
66
|
|
28
67
|
describe ".delete" do
|
29
68
|
it "should be forwarded to the provider" do
|
30
|
-
Elephas::Cache.provider.should_receive(:delete)
|
31
|
-
Elephas::Cache.delete("KEY")
|
69
|
+
::Elephas::Cache.provider.should_receive(:delete)
|
70
|
+
::Elephas::Cache.delete("KEY")
|
32
71
|
end
|
33
72
|
end
|
34
73
|
|
35
74
|
describe ".exists?" do
|
36
75
|
it "should be forwarded to the provider" do
|
37
|
-
Elephas::Cache.provider.should_receive(:exists?)
|
38
|
-
Elephas::Cache.exists?("KEY")
|
76
|
+
::Elephas::Cache.provider.should_receive(:exists?)
|
77
|
+
::Elephas::Cache.exists?("KEY")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "setup_options" do
|
82
|
+
it "should set good defaults for options" do
|
83
|
+
options_hashes = [
|
84
|
+
nil,
|
85
|
+
"A",
|
86
|
+
{:ttl => 2.hour},
|
87
|
+
{:force => true},
|
88
|
+
{:as_entry => true},
|
89
|
+
{:prefix => "prefix", :hash => "1f3caa3bd0b7ba49dc25f9445abdd4fed6fe65236681b392f0da251673885ea9"},
|
90
|
+
{:hash => "hash"}
|
91
|
+
]
|
92
|
+
|
93
|
+
reference_hashes = [
|
94
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => false, :as_entry => false, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "8a7675d5f1d9b163bb7bb4329c1a59ff18870fad4ed35a3a2e8d195a6f3c0332"},
|
95
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => false, :as_entry => false, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "8a7675d5f1d9b163bb7bb4329c1a59ff18870fad4ed35a3a2e8d195a6f3c0332"},
|
96
|
+
{:key => "KEY", :ttl => 2.hour, :force => false, :as_entry => false, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "8a7675d5f1d9b163bb7bb4329c1a59ff18870fad4ed35a3a2e8d195a6f3c0332"},
|
97
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => true, :as_entry => false, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "8a7675d5f1d9b163bb7bb4329c1a59ff18870fad4ed35a3a2e8d195a6f3c0332"},
|
98
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => false, :as_entry => true, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "8a7675d5f1d9b163bb7bb4329c1a59ff18870fad4ed35a3a2e8d195a6f3c0332"},
|
99
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => false, :as_entry => false, :prefix => "prefix", :complete_key => "prefix[KEY]", :hash => "1f3caa3bd0b7ba49dc25f9445abdd4fed6fe65236681b392f0da251673885ea9"},
|
100
|
+
{:key => "KEY", :ttl => 1.hour * 1000, :force => false, :as_entry => false, :prefix => ::Elephas::Cache.default_prefix, :complete_key => "#{::Elephas::Cache.default_prefix}[KEY]", :hash => "hash"}
|
101
|
+
]
|
102
|
+
|
103
|
+
options_hashes.each_with_index do |options, i|
|
104
|
+
expect(::Elephas::Cache.setup_options(options, "KEY")).to eq(reference_hashes[i])
|
105
|
+
end
|
39
106
|
end
|
40
107
|
end
|
41
108
|
end
|
data/spec/elephas/entry_spec.rb
CHANGED
@@ -7,5 +7,95 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Elephas::Entry do
|
10
|
+
subject { ::Elephas::Entry.new("KEY", "VALUE") }
|
10
11
|
|
12
|
+
describe "#initialize" do
|
13
|
+
it "should initialize with good defaults" do
|
14
|
+
::Time.stub(:now).and_return(123.456)
|
15
|
+
expect(subject.key).to eq("KEY")
|
16
|
+
expect(subject.value).to eq("VALUE")
|
17
|
+
expect(subject.hash).to eq("5ca24005b740717ba4f3f6bc48a230700e68c2a4b11ecedb96f169f4efaf1f21")
|
18
|
+
expect(subject.ttl).to eq(360000)
|
19
|
+
expect(subject.updated_at).to eq(123.456)
|
20
|
+
|
21
|
+
::Time.stub(:now).and_return(123.789)
|
22
|
+
other = ::Elephas::Entry.new("KEY 1", "VALUE 1", "HASH", 7200)
|
23
|
+
expect(other.key).to eq("KEY 1")
|
24
|
+
expect(other.value).to eq("VALUE 1")
|
25
|
+
expect(other.hash).to eq("HASH")
|
26
|
+
expect(other.ttl).to eq(7200)
|
27
|
+
expect(other.updated_at).to eq(123.789)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#refresh" do
|
32
|
+
before(:each) do
|
33
|
+
::Time.stub(:now).and_return(123.123)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should update the updated_at field" do
|
37
|
+
expect(subject.updated_at).to eq(123.123)
|
38
|
+
::Time.stub(:now).and_return(456.456)
|
39
|
+
subject.refresh
|
40
|
+
expect(subject.updated_at).to eq(456.456)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should save to the cache" do
|
44
|
+
expect(::Elephas::Cache.provider.read(subject.hash)).not_to eq(subject)
|
45
|
+
subject.refresh(true)
|
46
|
+
expect(::Elephas::Cache.provider.read(subject.hash)).to eq(subject)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#valid?" do
|
51
|
+
before(:each) do
|
52
|
+
::Time.stub(:now).and_return(100)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return true if the ttl is still valid" do
|
56
|
+
::Time.stub(:now).and_return(1000)
|
57
|
+
expect(subject.valid?).to be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return true if the ttl has expired" do
|
61
|
+
::Time.stub(:now).and_return(10000)
|
62
|
+
subject.updated_at = 1000
|
63
|
+
expect(subject.valid?).to be_false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "==" do
|
68
|
+
it "should correctly compare with other entries" do
|
69
|
+
expect(subject == subject).to be_true
|
70
|
+
expect(subject == ::Elephas::Entry.new("KEY", "VALUE")).to be_true
|
71
|
+
expect(subject == ::Elephas::Entry.new("KEY", "VALUE 1")).to be_false
|
72
|
+
expect(subject == ::Elephas::Entry.new("KEY 1", "VALUE")).to be_false
|
73
|
+
expect(subject == ::Elephas::Entry.new("KEY", "VALUE", "HASH")).to be_false
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return false for other type" do
|
77
|
+
expect(subject == nil).to be_false
|
78
|
+
expect(subject == "A").to be_false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe ".hashify_key" do
|
83
|
+
it "should compute a good hash" do
|
84
|
+
expect(::Elephas::Entry.hashify_key("HASH 1")).to eq("88e1f3572122e2605c1fab09efa8d4e99f5a064ae0230ca0aeced839796aba35")
|
85
|
+
expect(::Elephas::Entry.hashify_key("HASH 2")).to eq("38589cee32e00f700cf958dfe98f17d6da231700c41586e3c32b00314bb3cb58")
|
86
|
+
expect(::Elephas::Entry.hashify_key(nil)).to eq("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe ".ensure" do
|
91
|
+
it "should wrap the value" do
|
92
|
+
expect(::Elephas::Entry.ensure(nil, "KEY 1")).to eq(::Elephas::Entry.new("KEY 1", nil))
|
93
|
+
expect(::Elephas::Entry.ensure("A", "KEY 2")).to eq(::Elephas::Entry.new("KEY 2", "A"))
|
94
|
+
expect(::Elephas::Entry.ensure([], "KEY 3")).to eq(::Elephas::Entry.new("KEY 3", []))
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should not alter Entry objects" do
|
98
|
+
expect(::Elephas::Entry.ensure(subject, "ANOTHER KEY")).to eq(subject)
|
99
|
+
end
|
100
|
+
end
|
11
101
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elephas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cowtech-extensions
|
@@ -204,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
204
204
|
version: '0'
|
205
205
|
segments:
|
206
206
|
- 0
|
207
|
-
hash:
|
207
|
+
hash: 3704477900054561540
|
208
208
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
209
|
none: false
|
210
210
|
requirements:
|
@@ -213,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
213
213
|
version: '0'
|
214
214
|
segments:
|
215
215
|
- 0
|
216
|
-
hash:
|
216
|
+
hash: 3704477900054561540
|
217
217
|
requirements: []
|
218
218
|
rubyforge_project: elephas
|
219
219
|
rubygems_version: 1.8.24
|