battle_ship 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
- data/README.md +24 -29
- data/battle_ship.gemspec +5 -2
- data/lib/active_support/cache/store.rb +9 -0
- data/lib/battle_ship/version.rb +1 -1
- data/lib/battle_ship.rb +28 -2
- data/spec/lib/active_support_cache_subclass_spec.rb +38 -0
- data/spec/lib/battle_ship_spec.rb +28 -261
- data/spec/spec_helper.rb +1 -0
- metadata +36 -6
- data/lib/battle_ship/core.rb +0 -62
- data/lib/battle_ship/pass_through.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 122cf236b5148bbed85ef814efd2eb4552908255
|
4
|
+
data.tar.gz: 6bf5d9c28887a6e988f704d7eb24cefb79218759
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b701f36e44d2596fefc64eb6eb1708e97332acf88884d2a35f8f459c8ec32021b3b293da842446b2dc508e450acb31b6a90f5c54288a24f632ec4359ef9cdbc0
|
7
|
+
data.tar.gz: ffb5758795a6451dd628195284c720f87561dc31b92e90b0df08da1cb16f2b043b50d3282a0d11282f5440523599369677255e81f8542745afcbec062adc2244
|
data/README.md
CHANGED
@@ -8,18 +8,15 @@ curious what your success is with your caching strategies. Perhaps you cache
|
|
8
8
|
some user objects, sessions, etc. How do you know whether it's working? What if
|
9
9
|
it's mostly misses?
|
10
10
|
|
11
|
-
BattleShip
|
12
|
-
|
11
|
+
BattleShip updates ActiveSupport::Cache::Store (and all its subclasses, so
|
12
|
+
you're covered if you use RedisStore or MemcachedStore) to increment a counter
|
13
|
+
for each cache hit and miss
|
13
14
|
|
14
15
|
#### Warnings, Caveats, etc.
|
15
|
-
|
16
|
-
|
17
|
-
- fetch
|
18
|
-
- read
|
19
|
-
- write
|
20
|
-
- increment
|
16
|
+
BattleShip works by updating ActiveSupport::Cache::Store, so requires this class
|
17
|
+
to exist.
|
21
18
|
|
22
|
-
This cache must also be able to complete _atomic increment_ operations with the #increment method
|
19
|
+
This cache must also be able to complete _atomic increment_ operations with the #increment method.
|
23
20
|
|
24
21
|
This is early development days for BattleShip. Please feel free to open an issue
|
25
22
|
here with either questions or suggestions. And, of course, contributions are
|
@@ -41,24 +38,20 @@ Or install it yourself as:
|
|
41
38
|
|
42
39
|
## Usage
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
*
|
54
|
-
|
55
|
-
|
56
|
-
*
|
57
|
-
|
58
|
-
At any time, you can access the current number of hits and misses by calling
|
59
|
-
BattleShip.hits(:namespace) and BattleShip.misses(:namespace), where :namespace
|
60
|
-
is any given namespace you have used (e.g. 'user', as above). This can be passed
|
61
|
-
as a symbol or string.
|
41
|
+
Just keep on using Rails.cache methods like you do today. BattleShip makes
|
42
|
+
certain assumptions that you should be aware of:
|
43
|
+
* When there's a cache hit, it will increment the value of the returned object's
|
44
|
+
class name plus the string "\_hits" (e.g. a User object's hits will be stored
|
45
|
+
in the cache at key ```"User_hits"```)
|
46
|
+
* When there's a cache miss, BattleShip doesn't know exactly the class you were
|
47
|
+
expecting, so it assumes that you either: (1) Passed in a namespace key in
|
48
|
+
the options hash, or (2) Named your key such that the first underscore occurs
|
49
|
+
after the name of the class (e.g. "User_123").
|
50
|
+
* Access the hits by calling ```#hits``` on your particular cache implementation (e.g.
|
51
|
+
```Rails.cache.hits```) and passing in the namespace, e.g. User (either the string
|
52
|
+
or the classname).
|
53
|
+
* Same deal with misses, but call ```#misses``` instead
|
54
|
+
|
62
55
|
|
63
56
|
## Contributing
|
64
57
|
|
@@ -70,5 +63,7 @@ as a symbol or string.
|
|
70
63
|
|
71
64
|
## To Do
|
72
65
|
|
73
|
-
1. Add
|
74
|
-
2.
|
66
|
+
1. Add performance measurements
|
67
|
+
2. Add rails example app
|
68
|
+
3. Add config option for turning on/off
|
69
|
+
4. Add config option for setting time-frame of hits/misses
|
data/battle_ship.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = BattleShip::VERSION
|
9
9
|
spec.authors = ["DavidRagone"]
|
10
10
|
spec.email = ["dmragone@gmail.com"]
|
11
|
-
spec.description = %q{
|
11
|
+
spec.description = %q{Adds counters to cache when successful read operation completes in order to track Rails.cache hits & misses}
|
12
12
|
spec.summary = %q{Wrapper for Rails.cache methods}
|
13
|
-
spec.homepage = "
|
13
|
+
spec.homepage = "https://github.com/DavidRagone/BattleShip"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -21,4 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "activesupport"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "activesupport"
|
24
27
|
end
|
data/lib/battle_ship/version.rb
CHANGED
data/lib/battle_ship.rb
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
module BattleShip
|
2
|
+
def hits(key)
|
3
|
+
read(key.to_s.camelize << '_hits', skip_increment: true)
|
4
|
+
end
|
5
|
+
|
6
|
+
def misses(key)
|
7
|
+
read(key.to_s.camelize << '_misses', skip_increment: true)
|
8
|
+
end
|
9
|
+
|
10
|
+
def read_entry(key, options) # :nodoc:
|
11
|
+
entry = super(key, options)
|
12
|
+
return entry if options[:skip_increment]
|
13
|
+
if entry
|
14
|
+
increment("#{entry.value.class}_hits")
|
15
|
+
else
|
16
|
+
increment("#{namespace(key, options)}_misses")
|
17
|
+
end
|
18
|
+
entry
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def namespace(key, options)
|
23
|
+
(options[:namespace] || key_up_to_first_underscore(key)).camelize
|
24
|
+
end
|
25
|
+
|
26
|
+
def key_up_to_first_underscore(key)
|
27
|
+
key[0..(key.index('_') -1)]
|
28
|
+
end
|
2
29
|
end
|
3
30
|
|
4
31
|
require "battle_ship/version"
|
5
|
-
require "
|
6
|
-
require "battle_ship/pass_through"
|
32
|
+
require "active_support/cache/store"
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User
|
4
|
+
def initialize(options)
|
5
|
+
@name = options[:name]
|
6
|
+
@age = options[:age]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ActiveSupport::Cache::Something < ActiveSupport::Cache::Store
|
11
|
+
protected
|
12
|
+
def read_entry(key, options)
|
13
|
+
@value
|
14
|
+
end
|
15
|
+
|
16
|
+
def write_entry(key, entry, options)
|
17
|
+
@value = entry
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ActiveSupport::Cache::Something do
|
22
|
+
let(:cache) { ActiveSupport::Cache::Something.new }
|
23
|
+
let(:user) { User.new(name: 'Bob', age: 'old') }
|
24
|
+
|
25
|
+
context ".read" do
|
26
|
+
it "increments the related counter on hits" do
|
27
|
+
cache.should_receive(:increment).with("User_hits")
|
28
|
+
cache.write("user_1", user)
|
29
|
+
cache.read("user_1").should eq user
|
30
|
+
end
|
31
|
+
|
32
|
+
it "increments the related counter on misses" do
|
33
|
+
cache.should_receive(:increment).with("User_misses")
|
34
|
+
cache.read("user_1").should eq nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -1,280 +1,47 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
def self.cache
|
5
|
-
Cache
|
6
|
-
end
|
7
|
-
end
|
8
|
-
class Cache
|
9
|
-
def self.read(k)
|
10
|
-
end
|
11
|
-
def self.increment(k,a=1,o=nil)
|
12
|
-
end
|
13
|
-
def self.fetch(k,o=nil)
|
14
|
-
if block_given?
|
15
|
-
yield
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
3
|
+
# For tests of #read_entry, see spec/lib/active_support_cache_subclass_spec.rb
|
20
4
|
describe BattleShip do
|
21
|
-
|
22
|
-
shared_examples_for :read do
|
23
|
-
it "retrieves value from Rails.cache.read" do
|
24
|
-
Cache.should_receive(:read).with("namespace_#{uniqueid}")
|
25
|
-
BattleShip.read(:namespace, uniqueid)
|
26
|
-
end
|
5
|
+
let(:dummy) { Class.new { prepend(BattleShip) }.new }
|
27
6
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
it "increments namespace hit counter when value is non-nil" do
|
34
|
-
Cache.stub(:read) { 'a value' }
|
35
|
-
Cache.should_receive(:increment).with('foo_hit', 1, nil)
|
36
|
-
BattleShip.read(:foo, :bar)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "increments namespace miss counter when value is nil" do
|
40
|
-
Cache.stub(:read) { nil }
|
41
|
-
Cache.should_receive(:increment).with('foo_miss', 1, nil)
|
42
|
-
BattleShip.read(:foo, :bar)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'when uniqueid is a symbol' do
|
47
|
-
let(:uniqueid) { :foo }
|
48
|
-
it_behaves_like :read
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'when uniqueid is a string' do
|
52
|
-
let(:uniqueid) { 'foo' }
|
53
|
-
it_behaves_like :read
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'when uniqueid is a number' do
|
57
|
-
let(:uniqueid) { 4 }
|
58
|
-
it_behaves_like :read
|
7
|
+
describe "#hits" do
|
8
|
+
it "reads, skipping increment" do
|
9
|
+
dummy.should_receive(:read).with("Foo_hits", { skip_increment: true })
|
10
|
+
dummy.hits("Foo")
|
59
11
|
end
|
60
12
|
end
|
61
13
|
|
62
|
-
describe "
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
BattleShip.write(:namespace, uniqueid, value)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'when uniqueid is a symbol' do
|
71
|
-
let(:uniqueid) { :foo }
|
72
|
-
let(:value) { :bar }
|
73
|
-
it_behaves_like :write
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'when uniqueid is a string' do
|
77
|
-
let(:uniqueid) { 'foo' }
|
78
|
-
let(:value) { 'bar' }
|
79
|
-
it_behaves_like :write
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'when uniqueid is a number' do
|
83
|
-
let(:uniqueid) { 4 }
|
84
|
-
let(:value) { 5 }
|
85
|
-
it_behaves_like :write
|
14
|
+
describe "#misses" do
|
15
|
+
it "reads, skipping increment" do
|
16
|
+
dummy.should_receive(:read).with("Foo_misses", { skip_increment: true })
|
17
|
+
dummy.misses("Foo")
|
86
18
|
end
|
87
19
|
end
|
88
20
|
|
89
|
-
describe "
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
subject.should eq 'a value'
|
99
|
-
end
|
100
|
-
|
101
|
-
it "increments namespace hit counter when value is non-nil" do
|
102
|
-
Cache.stub(:fetch) { 'a value' }
|
103
|
-
Cache.stub(:read) { 'a value' }
|
104
|
-
Cache.should_receive(:increment).with('foo_hit', 1, nil)
|
105
|
-
subject
|
106
|
-
end
|
107
|
-
|
108
|
-
it "increments namespace miss counter when value is nil" do
|
109
|
-
Cache.stub(:fetch) { nil }
|
110
|
-
Cache.should_receive(:increment).with('foo_miss', 1, nil)
|
111
|
-
subject
|
21
|
+
describe "private methods" do
|
22
|
+
describe "#namespace" do
|
23
|
+
subject { dummy.send(:namespace, key, options) }
|
24
|
+
context "when options includes namespace" do
|
25
|
+
let(:key) { 'Key' }
|
26
|
+
let(:options) { { namespace: 'NameSpace' } }
|
27
|
+
it "returns the option's namespace" do
|
28
|
+
subject.should eq 'NameSpace'
|
29
|
+
end
|
112
30
|
end
|
113
|
-
end
|
114
|
-
context "when block given" do
|
115
|
-
subject { described_class.fetch(:foo, :bar) { 'a different value' } }
|
116
|
-
it_behaves_like :fetch
|
117
31
|
|
118
|
-
context "when
|
119
|
-
|
120
|
-
|
121
|
-
|
32
|
+
context "when options does not include namespace" do
|
33
|
+
let(:key) { 'Key_Thing_123' }
|
34
|
+
let(:options) { { } }
|
35
|
+
it "returns the string preceding the first underscore" do
|
36
|
+
subject.should eq 'Key'
|
122
37
|
end
|
123
38
|
end
|
124
39
|
end
|
125
40
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
describe ".cleanup" do
|
133
|
-
let(:options) { { } }
|
134
|
-
it "passes to underlying" do
|
135
|
-
Cache.should_receive(:cleanup).with(options)
|
136
|
-
BattleShip.cleanup(options)
|
137
|
-
end
|
138
|
-
|
139
|
-
it "does not require options" do
|
140
|
-
Cache.should_receive(:cleanup).with(nil)
|
141
|
-
BattleShip.cleanup
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
describe ".clear" do
|
146
|
-
let(:options) { { } }
|
147
|
-
it "passes to underlying" do
|
148
|
-
Cache.should_receive(:clear).with(options)
|
149
|
-
BattleShip.clear(options)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "does not require options" do
|
153
|
-
Cache.should_receive(:clear).with(nil)
|
154
|
-
BattleShip.clear
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
describe ".decrement" do
|
159
|
-
let(:options) { { } }
|
160
|
-
let(:namespace) { 'name' }
|
161
|
-
let(:uid) { '1' }
|
162
|
-
let(:amount) { 2 }
|
163
|
-
it "passes to underlying" do
|
164
|
-
Cache.should_receive(:decrement).with('name_1', amount, options)
|
165
|
-
BattleShip.decrement(namespace, uid, amount, options)
|
166
|
-
end
|
167
|
-
|
168
|
-
it "defaults amount to 1" do
|
169
|
-
Cache.should_receive(:decrement).with('name_1', 3, options)
|
170
|
-
BattleShip.decrement(namespace, uid, 3, options)
|
171
|
-
end
|
172
|
-
|
173
|
-
it "does not require options" do
|
174
|
-
Cache.should_receive(:decrement).with('name_1', 1, nil)
|
175
|
-
BattleShip.decrement(namespace, uid)
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe ".delete" do
|
180
|
-
let(:namespace) { 'name' }
|
181
|
-
let(:uid) { 1 }
|
182
|
-
let(:options) { { } }
|
183
|
-
it "passes to underlying" do
|
184
|
-
Cache.should_receive(:delete).with('name_1', options)
|
185
|
-
BattleShip.delete(namespace, uid, options)
|
186
|
-
end
|
187
|
-
|
188
|
-
it "does not require options" do
|
189
|
-
Cache.should_receive(:delete).with('name_1', nil)
|
190
|
-
BattleShip.delete(namespace, uid)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
describe ".delete_matched" do
|
195
|
-
let(:matcher) { 'something*' }
|
196
|
-
let(:options) { { } }
|
197
|
-
it "passes to underlying" do
|
198
|
-
Cache.should_receive(:delete_matched).with(matcher, options)
|
199
|
-
BattleShip.delete_matched(matcher, options)
|
200
|
-
end
|
201
|
-
|
202
|
-
it "does not require options" do
|
203
|
-
Cache.should_receive(:delete_matched).with(matcher, nil)
|
204
|
-
BattleShip.delete_matched(matcher)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
describe ".exist?" do
|
209
|
-
let(:namespace) { 'name' }
|
210
|
-
let(:uid) { 1 }
|
211
|
-
let(:options) { { } }
|
212
|
-
it "passes to underlying" do
|
213
|
-
Cache.should_receive(:exist?).with('name_1', options)
|
214
|
-
BattleShip.exist?(namespace, uid, options)
|
215
|
-
end
|
216
|
-
|
217
|
-
it "does not require options" do
|
218
|
-
Cache.should_receive(:exist?).with('name_1', nil)
|
219
|
-
BattleShip.exist?(namespace, uid)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
describe ".increment" do
|
224
|
-
let(:options) { { } }
|
225
|
-
let(:namespace) { 'name' }
|
226
|
-
let(:uid) { '1' }
|
227
|
-
let(:amount) { 2 }
|
228
|
-
it "passes to underlying" do
|
229
|
-
Cache.should_receive(:increment).with('name_1', amount, options)
|
230
|
-
BattleShip.increment(namespace, uid, amount, options)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "defaults amount to 1" do
|
234
|
-
Cache.should_receive(:increment).with('name_1', 3, options)
|
235
|
-
BattleShip.increment(namespace, uid, 3, options)
|
236
|
-
end
|
237
|
-
|
238
|
-
it "does not require options" do
|
239
|
-
Cache.should_receive(:increment).with('name_1', 1, nil)
|
240
|
-
BattleShip.increment(namespace, uid)
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
describe ".hits" do
|
245
|
-
it "returns the hits" do
|
246
|
-
Cache.should_receive(:read).with("foo_hit") { 4 }
|
247
|
-
BattleShip.hits(:foo).should eq 4
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
describe ".misses" do
|
252
|
-
it "returns the hits" do
|
253
|
-
Cache.should_receive(:read).with("foo_miss") { 4 }
|
254
|
-
BattleShip.misses(:foo).should eq 4
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe ".mute" do
|
259
|
-
it "passes to underlying" do
|
260
|
-
Cache.should_receive(:mute)
|
261
|
-
BattleShip.mute
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
describe ".read_multi" do
|
266
|
-
# *names - options is last
|
267
|
-
let(:names) { ['1', '2', '3'] }
|
268
|
-
it "passes to underlying" do
|
269
|
-
Cache.should_receive(:read_multi).with(names)
|
270
|
-
BattleShip.read_multi(names)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
describe ".silence!" do
|
275
|
-
it "passes to underlying" do
|
276
|
-
Cache.should_receive(:silence!)
|
277
|
-
BattleShip.silence!
|
41
|
+
describe "#key_up_to_first_underscore" do
|
42
|
+
it "returns the string up to the first underscore" do
|
43
|
+
dummy.send(:key_up_to_first_underscore, 'someclass_thing').should eq 'someclass'
|
44
|
+
end
|
278
45
|
end
|
279
46
|
end
|
280
47
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: battle_ship
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DavidRagone
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,36 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Adds counters to cache when successful read operation completes in order
|
84
|
+
to track Rails.cache hits & misses
|
56
85
|
email:
|
57
86
|
- dmragone@gmail.com
|
58
87
|
executables: []
|
@@ -65,13 +94,13 @@ files:
|
|
65
94
|
- README.md
|
66
95
|
- Rakefile
|
67
96
|
- battle_ship.gemspec
|
97
|
+
- lib/active_support/cache/store.rb
|
68
98
|
- lib/battle_ship.rb
|
69
|
-
- lib/battle_ship/core.rb
|
70
|
-
- lib/battle_ship/pass_through.rb
|
71
99
|
- lib/battle_ship/version.rb
|
100
|
+
- spec/lib/active_support_cache_subclass_spec.rb
|
72
101
|
- spec/lib/battle_ship_spec.rb
|
73
102
|
- spec/spec_helper.rb
|
74
|
-
homepage:
|
103
|
+
homepage: https://github.com/DavidRagone/BattleShip
|
75
104
|
licenses:
|
76
105
|
- MIT
|
77
106
|
metadata: {}
|
@@ -96,5 +125,6 @@ signing_key:
|
|
96
125
|
specification_version: 4
|
97
126
|
summary: Wrapper for Rails.cache methods
|
98
127
|
test_files:
|
128
|
+
- spec/lib/active_support_cache_subclass_spec.rb
|
99
129
|
- spec/lib/battle_ship_spec.rb
|
100
130
|
- spec/spec_helper.rb
|
data/lib/battle_ship/core.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
module BattleShip
|
2
|
-
class << self
|
3
|
-
def read(namespace, uid)
|
4
|
-
value = cache.read(namespaced(namespace, uid))
|
5
|
-
increment_hit_or_miss(namespace, value)
|
6
|
-
value
|
7
|
-
end
|
8
|
-
|
9
|
-
def write(namespace, uid, value, options = nil)
|
10
|
-
cache.write(namespaced(namespace, uid), value, options)
|
11
|
-
end
|
12
|
-
|
13
|
-
def fetch(namespace, uid, options = nil)
|
14
|
-
# http://mudge.name/2011/01/26/passing-blocks-in-ruby-without-block.html
|
15
|
-
namespaced = namespaced(namespace, uid)
|
16
|
-
increment_hit_or_miss(namespace, cache.read(namespaced))
|
17
|
-
if block_given?
|
18
|
-
cache.fetch(namespaced, options, &Proc.new)
|
19
|
-
else
|
20
|
-
cache.fetch(namespaced, options)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def decrement(namespace, uid, amount = 1, options = nil)
|
25
|
-
cache.decrement(namespaced(namespace, uid), amount, options)
|
26
|
-
end
|
27
|
-
|
28
|
-
def delete(namespace, uid, options = nil)
|
29
|
-
cache.delete(namespaced(namespace, uid), options)
|
30
|
-
end
|
31
|
-
|
32
|
-
def exist?(namespace, uid, options = nil)
|
33
|
-
cache.exist?(namespaced(namespace, uid), options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def increment(namespace, uid, amount = 1, options = nil)
|
37
|
-
cache.increment(namespaced(namespace, uid), amount, options)
|
38
|
-
end
|
39
|
-
|
40
|
-
def hits(namespace)
|
41
|
-
cache.read("#{namespace}_hit")
|
42
|
-
end
|
43
|
-
|
44
|
-
def misses(namespace)
|
45
|
-
cache.read("#{namespace}_miss")
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
def increment_hit_or_miss(namespace, value, amount = 1, options = nil)
|
50
|
-
hit_or_miss = value.nil? ? '_miss' : '_hit'
|
51
|
-
cache.increment(namespace.to_s << hit_or_miss, amount, options)
|
52
|
-
end
|
53
|
-
|
54
|
-
def namespaced(namespace, uid)
|
55
|
-
namespace.to_s << '_' << uid.to_s
|
56
|
-
end
|
57
|
-
|
58
|
-
def cache
|
59
|
-
Rails.cache
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module BattleShip
|
2
|
-
class << self
|
3
|
-
def cleanup(options = nil)
|
4
|
-
cache.cleanup(options)
|
5
|
-
end
|
6
|
-
|
7
|
-
def clear(options = nil)
|
8
|
-
cache.clear(options)
|
9
|
-
end
|
10
|
-
|
11
|
-
def delete_matched(matcher, options = nil)
|
12
|
-
cache.delete_matched(matcher, options)
|
13
|
-
end
|
14
|
-
|
15
|
-
def mute
|
16
|
-
cache.mute
|
17
|
-
end
|
18
|
-
|
19
|
-
def read_multi(*names)
|
20
|
-
cache.read_multi(*names)
|
21
|
-
end
|
22
|
-
|
23
|
-
def silence!
|
24
|
-
cache.silence!
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|