cache-store-api 0.1.1 → 0.1.2
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/.gitignore +6 -0
- data/.rspec +1 -0
- data/.runrc +3 -0
- data/Gemfile +3 -0
- data/Rakefile +1 -0
- data/cache-store-api.gemspec +24 -0
- data/lib/cache-store-api.rb +4 -34
- data/lib/cache-store-api/class_methods.rb +62 -0
- data/lib/cache-store-api/version.rb +1 -1
- data/spec/cache-store-api_spec.rb +201 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/spec_suite.rb +3 -0
- metadata +43 -22
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-cfs --tty
|
data/.runrc
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require 'cache-store-api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "cache-store-api"
|
8
|
+
s.version = CacheStoreApi::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["Brian Takita"]
|
11
|
+
s.email = ["brian@honk.com"]
|
12
|
+
s.homepage = "http://github.com/honkster/cache-store-api"
|
13
|
+
s.summary = "Ruby cache methods built on top of Rails and Sinatra caching."
|
14
|
+
s.description = "Ruby cache methods built on top of Rails and Sinatra caching."
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec", "~>2.6.0"
|
22
|
+
s.add_development_dependency "rr", "~>1.0.4"
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
end
|
data/lib/cache-store-api.rb
CHANGED
@@ -1,39 +1,9 @@
|
|
1
|
-
|
2
|
-
module CommonMethods
|
3
|
-
def lazy_cache(key, expiration=3600)
|
4
|
-
if block_given?
|
5
|
-
if cache.exist?(key)
|
6
|
-
cache.read(key)
|
7
|
-
else
|
8
|
-
output = yield
|
9
|
-
cache.write(key, output, :expires_in => expiration)
|
10
|
-
output
|
11
|
-
end
|
12
|
-
else
|
13
|
-
cache.read(key)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def expire(key)
|
18
|
-
cache.delete(key)
|
19
|
-
end
|
1
|
+
require 'cache-store-api/class_methods'
|
20
2
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
25
|
-
extend CommonMethods
|
26
|
-
|
27
|
-
extend(Module.new do
|
28
|
-
attr_reader :cache_lambda
|
29
|
-
def cache
|
30
|
-
cache_lambda.call
|
31
|
-
end
|
3
|
+
module CacheStoreApi
|
4
|
+
extend ClassMethods
|
32
5
|
|
33
|
-
|
34
|
-
@cache_lambda = block
|
35
|
-
end
|
36
|
-
end)
|
6
|
+
autoload :TestCache, 'cache-store-api/test_cache'
|
37
7
|
|
38
8
|
def cache
|
39
9
|
CacheStoreApi.cache
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module CacheStoreApi
|
2
|
+
module ClassMethods
|
3
|
+
attr_reader :cache_lambda
|
4
|
+
|
5
|
+
def lazy_cache(key, expiration=3600)
|
6
|
+
return cache.read(key) unless block_given?
|
7
|
+
|
8
|
+
if caching?
|
9
|
+
return cache.read(key) if cache.exist?(key)
|
10
|
+
|
11
|
+
data = yield
|
12
|
+
cache.write(key, data, :expires_in => expiration)
|
13
|
+
data
|
14
|
+
else
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def expire(key)
|
20
|
+
cache.delete(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
def data
|
24
|
+
cache.instance_variable_get(:@data)
|
25
|
+
end
|
26
|
+
|
27
|
+
def cache
|
28
|
+
cache_lambda.call
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_cache(&block)
|
32
|
+
@cache_lambda = block
|
33
|
+
end
|
34
|
+
|
35
|
+
def caching?
|
36
|
+
!!perform_caching_lambda.call
|
37
|
+
end
|
38
|
+
|
39
|
+
def set_perform_caching(&block)
|
40
|
+
@perform_caching_lambda = block
|
41
|
+
end
|
42
|
+
|
43
|
+
def enable_cache!
|
44
|
+
set_perform_caching do
|
45
|
+
true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def disable_cache!
|
50
|
+
set_perform_caching do
|
51
|
+
false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
def perform_caching_lambda
|
57
|
+
@perform_caching_lambda || lambda do
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/spec_helper"
|
2
|
+
|
3
|
+
describe CacheStoreApi do
|
4
|
+
let(:cache) do
|
5
|
+
Object.new.tap do |cache|
|
6
|
+
stub(cache).exist? { raise "You should mock me." }
|
7
|
+
stub(cache).read { raise "You should mock me." }
|
8
|
+
stub(cache).write { raise "You should mock me." }
|
9
|
+
stub(cache).delete { raise "You should mock me." }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:cache_store_api) do
|
14
|
+
Module.new do
|
15
|
+
include CacheStoreApi
|
16
|
+
extend CacheStoreApi::ClassMethods
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
before(:each) do
|
21
|
+
cache_store_api.set_cache { cache }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".disable_cache!" do
|
25
|
+
it "should set the #caching? to false" do
|
26
|
+
cache_store_api.disable_cache!
|
27
|
+
cache_store_api.caching?.should == false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".set_perform_caching" do
|
32
|
+
it "sets .caching? to true when truthy and false when falsy" do
|
33
|
+
cache_store_api.set_perform_caching {true}
|
34
|
+
cache_store_api.caching?.should == true
|
35
|
+
|
36
|
+
cache_store_api.set_perform_caching {false}
|
37
|
+
cache_store_api.caching?.should == false
|
38
|
+
|
39
|
+
cache_store_api.set_perform_caching {"hi"}
|
40
|
+
cache_store_api.caching?.should == true
|
41
|
+
|
42
|
+
cache_store_api.set_perform_caching {nil}
|
43
|
+
cache_store_api.caching?.should == false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".enable_cache!" do
|
48
|
+
it "should set the #caching? to true" do
|
49
|
+
cache_store_api.enable_cache!
|
50
|
+
cache_store_api.caching?.should == true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe ".caching?" do
|
55
|
+
it "should reflect the value of @caching_status" do
|
56
|
+
cache_store_api.caching?.should be_true
|
57
|
+
|
58
|
+
cache_store_api.disable_cache!
|
59
|
+
|
60
|
+
cache_store_api.caching?.should be_false
|
61
|
+
|
62
|
+
cache_store_api.enable_cache!
|
63
|
+
|
64
|
+
cache_store_api.caching?.should be_true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ".lazy_cache" do
|
69
|
+
context "when caching is disabled" do
|
70
|
+
context "when a block is given" do
|
71
|
+
before(:each) do
|
72
|
+
cache_store_api.disable_cache!
|
73
|
+
|
74
|
+
stub(cache).exist?("cached-key") { fail "Cache should not be read from, when disabled!" }
|
75
|
+
end
|
76
|
+
|
77
|
+
it "always yields to the given block" do
|
78
|
+
cache_store_api.lazy_cache("cached-key") { "some value" }.should == "some value"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when caching is enabled" do
|
84
|
+
context "when a block is given" do
|
85
|
+
context "when the key exists in the cache" do
|
86
|
+
it "returns the value of cache.read" do
|
87
|
+
mock(cache).exist?("the-key") {true}
|
88
|
+
mock(cache).read("the-key") {"the value"}
|
89
|
+
|
90
|
+
cache_store_api.lazy_cache("the-key") {"new value"}.should == "the value"
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
context "when the key does not exist in the cache" do
|
96
|
+
context "when an expiration is not given" do
|
97
|
+
it "returns the return value of the block and writes that value (expiring in one hour) into the cache" do
|
98
|
+
mock(cache).exist?("the-key") {false}
|
99
|
+
mock(cache).write("the-key", "new value", :expires_in => 3600)
|
100
|
+
|
101
|
+
cache_store_api.lazy_cache("the-key") {"new value"}.should == "new value"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "when an expiration is given" do
|
106
|
+
it "returns the return value of the block and writes that value (expiring in the given time) into the cache" do
|
107
|
+
mock(cache).exist?("the-key") {false}
|
108
|
+
mock(cache).write("the-key", "new value", :expires_in => 7200)
|
109
|
+
|
110
|
+
cache_store_api.lazy_cache("the-key", 7200) {"new value"}.should == "new value"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context "when a block is not given" do
|
118
|
+
it "returns the value of cache.read" do
|
119
|
+
mock(cache).read("the-key") {"the value"}
|
120
|
+
|
121
|
+
cache_store_api.lazy_cache("the-key").should == "the value"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe ".expire" do
|
128
|
+
it "deletes the key in the cache" do
|
129
|
+
mock(cache).delete("the-key")
|
130
|
+
cache_store_api.expire("the-key")
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe ".data" do
|
135
|
+
it "returns the @data variable of the cache" do
|
136
|
+
data = {}
|
137
|
+
cache.instance_variable_set(:@data, data)
|
138
|
+
cache_store_api.data.should == data
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe CacheStoreApi::TestCache do
|
143
|
+
let(:cache) { CacheStoreApi::TestCache.new }
|
144
|
+
|
145
|
+
describe ".lazy_cache" do
|
146
|
+
context "when a block is given" do
|
147
|
+
context "when the key exists in the cache" do
|
148
|
+
it "returns the value of cache.read" do
|
149
|
+
cache.write("the-key", "the value")
|
150
|
+
cache.exist?("the-key").should be_true
|
151
|
+
|
152
|
+
cache_store_api.lazy_cache("the-key") {"new value"}.should == "the value"
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
context "when the key does not exist in the cache" do
|
158
|
+
context "when an expiration is not given" do
|
159
|
+
it "returns the return value of the block and writes that value (expiring in one hour) into the cache" do
|
160
|
+
cache.exist?("the-key").should be_false
|
161
|
+
|
162
|
+
cache_store_api.lazy_cache("the-key") {"new value"}.should == "new value"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "when an expiration is given" do
|
167
|
+
it "returns the return value of the block and writes that value (expiring in the given time) into the cache" do
|
168
|
+
cache.exist?("the-key").should be_false
|
169
|
+
|
170
|
+
cache_store_api.lazy_cache("the-key", 7200) {"new value"}.should == "new value"
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "when a block is not given" do
|
178
|
+
it "returns the value of cache.read" do
|
179
|
+
cache.write("the-key", "the value")
|
180
|
+
|
181
|
+
cache_store_api.lazy_cache("the-key").should == "the value"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe ".expire" do
|
187
|
+
it "deletes the key in the cache" do
|
188
|
+
cache.write("the-key", "the value")
|
189
|
+
cache.exist?("the-key").should be_true
|
190
|
+
cache_store_api.expire("the-key")
|
191
|
+
cache.exist?("the-key").should be_false
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe ".data" do
|
196
|
+
it "returns the @data variable of the cache" do
|
197
|
+
cache_store_api.data.should == cache.data
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/spec_suite.rb
ADDED
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache-store-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Brian Takita
|
@@ -14,22 +10,42 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
13
|
+
date: 2011-09-06 00:00:00 -07:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
21
17
|
name: rspec
|
22
18
|
prerelease: false
|
23
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.6.0
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rr
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 1.0.4
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rake
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
24
42
|
none: false
|
25
43
|
requirements:
|
26
44
|
- - ">="
|
27
45
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
46
|
version: "0"
|
31
47
|
type: :development
|
32
|
-
version_requirements: *
|
48
|
+
version_requirements: *id003
|
33
49
|
description: Ruby cache methods built on top of Rails and Sinatra caching.
|
34
50
|
email:
|
35
51
|
- brian@honk.com
|
@@ -40,11 +56,22 @@ extensions: []
|
|
40
56
|
extra_rdoc_files: []
|
41
57
|
|
42
58
|
files:
|
43
|
-
-
|
44
|
-
-
|
45
|
-
-
|
59
|
+
- .gitignore
|
60
|
+
- .rspec
|
61
|
+
- .runrc
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
46
64
|
- LICENSE
|
47
65
|
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- cache-store-api.gemspec
|
68
|
+
- lib/cache-store-api.rb
|
69
|
+
- lib/cache-store-api/class_methods.rb
|
70
|
+
- lib/cache-store-api/test_cache.rb
|
71
|
+
- lib/cache-store-api/version.rb
|
72
|
+
- spec/cache-store-api_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/spec_suite.rb
|
48
75
|
has_rdoc: true
|
49
76
|
homepage: http://github.com/honkster/cache-store-api
|
50
77
|
licenses: []
|
@@ -59,23 +86,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
86
|
requirements:
|
60
87
|
- - ">="
|
61
88
|
- !ruby/object:Gem::Version
|
62
|
-
segments:
|
63
|
-
- 0
|
64
89
|
version: "0"
|
65
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
91
|
none: false
|
67
92
|
requirements:
|
68
93
|
- - ">="
|
69
94
|
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
- 1
|
72
|
-
- 3
|
73
|
-
- 6
|
74
|
-
version: 1.3.6
|
95
|
+
version: "0"
|
75
96
|
requirements: []
|
76
97
|
|
77
|
-
rubyforge_project:
|
78
|
-
rubygems_version: 1.
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.5.2
|
79
100
|
signing_key:
|
80
101
|
specification_version: 3
|
81
102
|
summary: Ruby cache methods built on top of Rails and Sinatra caching.
|