battle_ship 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 176aaef26568ed8973ea762f53fc72ce49825e73
4
- data.tar.gz: d02c98c534224cdfb94dc769a8a0036681c91202
3
+ metadata.gz: 7ef0a6a6b19a150b4ead60d3f5a75f3ab226c9f8
4
+ data.tar.gz: 3bdcdab074f21e0e577a18e43db96e6648515016
5
5
  SHA512:
6
- metadata.gz: d6f83679b0e0a52b8a2c6c6ed846071f9d73e1cf858ae4c7de8a322d6d4eeca3dfdf6c6730a59aaaeff388c38633f980d09017899060fc321d1eaf20bad130f3
7
- data.tar.gz: 8525ef636a9fb6c271199861fb8c1f8ce69e49db3df1902c9d31bae2b4aa62f8442346f4f9baa2ea362a31c2c79826c9bfe9d825e4c94c4bfaae1e3323833387
6
+ metadata.gz: 716a2bdee834065f1868323ba2693c4a7e73a50045a367feef1ff21cb6df47adb2ca86bd1eec4ec8e8bd868f1c17cf216476dded05c9789206b98b3d714abe20
7
+ data.tar.gz: 9c935394e24b0f379a123a58c28a4c853f3f6115651f7ca24e5cb1ee7b3ed02d5f8c4cec2bf43dec3ee0c8191c97f7f461a490cdd4bf127548a9b942b4c24a0c
data/README.md CHANGED
@@ -1,28 +1,30 @@
1
1
  # BattleShip
2
2
 
3
- ### Keep track of your cache hits & misses
4
-
5
3
  #### Description
6
- If you make use of Rails.cache methods (e.g. #fetch, #read, #write), you may be
7
- curious what your success is with your caching strategies. Perhaps you cache
8
- some user objects, sessions, etc. How do you know whether it's working? What if
9
- it's mostly misses?
4
+ The BattleShip gem answers the question "What is my cache hit rate?"
10
5
 
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
6
+ It does this by adding an increment to every cache read operation,
7
+ counting each read as either a hit (returned non-nil value) or miss (returned
8
+ nil). It stores the resulting numbers of hits and misses in the cache itself.
14
9
 
15
- #### Warnings, Caveats, etc.
16
- BattleShip works by updating ActiveSupport::Cache::Store, so requires this class
17
- to exist.
10
+ BattleShip assumes you are caching objects (such as a User or Session object). On a hit, it
11
+ will see the class name and increment the hit count at
12
+ ```"#{returned_object.class}_hits"```, where returned_object is the object that
13
+ the cache call returns. On a miss, BattleShip assumes that the key represents
14
+ the class name (e.g. you called Rails.cache.read("user_1")), and increments the
15
+ miss counter accordingly (e.g. at "user_misses"). If you pass in a ```:namespace``` key in the options
16
+ hash, it will use that instead.
18
17
 
19
- This cache must also be able to complete _atomic increment_ operations with the #increment method.
18
+ ## Usage
20
19
 
21
- This is early development days for BattleShip. Please feel free to open an issue
22
- here with either questions or suggestions. And, of course, contributions are
23
- welcome.
20
+ Cache things just liek you do with Rails.cache methods already.
21
+
22
+ Access the hits by calling ```#hits``` on your particular cache implementation (e.g.
23
+ ```Rails.cache.hits```) and passing in the namespace, e.g. User (either the string
24
+ or the classname).
25
+ Same deal with misses, but call ```#misses``` instead
24
26
 
25
- ## Installation
27
+ #### Installation
26
28
 
27
29
  Add this line to your application's Gemfile:
28
30
 
@@ -36,21 +38,9 @@ Or install it yourself as:
36
38
 
37
39
  $ gem install battle_ship
38
40
 
39
- ## Usage
40
41
 
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
42
+ #### Warnings, Caveats, etc.
43
+ If you are using a cache store that does not subclass ActiveSupport::Cache::Store, please add an initializer file in ```config/initializers``` that calls ```BattleShip.include!```
54
44
 
55
45
 
56
46
  ## Contributing
@@ -64,6 +54,5 @@ or the classname).
64
54
  ## To Do
65
55
 
66
56
  1. Add performance measurements
67
- 2. Add rails example app
68
57
  3. Add config option for turning on/off
69
58
  4. Add config option for setting time-frame of hits/misses
data/Rakefile CHANGED
@@ -1,7 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new
4
+ RSpec::Core::RakeTask.new do |task|
5
+ task.pattern = 'spec/lib/*'
6
+ end
5
7
 
6
8
  task :default => :spec
7
9
  task :test => :spec
data/battle_ship.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["DavidRagone"]
10
10
  spec.email = ["dmragone@gmail.com"]
11
11
  spec.description = %q{Adds counters to cache when successful read operation completes in order to track Rails.cache hits & misses}
12
- spec.summary = %q{Wrapper for Rails.cache methods}
12
+ spec.summary = %q{Keep track of cache hits and misses}
13
13
  spec.homepage = "https://github.com/DavidRagone/BattleShip"
14
14
  spec.license = "MIT"
15
15
 
data/lib/battle_ship.rb CHANGED
@@ -18,6 +18,10 @@ module BattleShip
18
18
  entry
19
19
  end
20
20
 
21
+ def self.include!
22
+ Rails.cache.class.class_eval("prepend BattleShip")
23
+ end
24
+
21
25
  private
22
26
  def namespace(key, options)
23
27
  (options[:namespace] || key_up_to_first_underscore(key)).camelize
@@ -1,3 +1,3 @@
1
1
  module BattleShip
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -24,13 +24,13 @@ describe ActiveSupport::Cache::Something do
24
24
 
25
25
  context ".read" do
26
26
  it "increments the related counter on hits" do
27
- cache.should_receive(:increment).with("User_hits")
27
+ cache.should_receive(:increment).with("User_hits", 1)
28
28
  cache.write("user_1", user)
29
29
  cache.read("user_1").should eq user
30
30
  end
31
31
 
32
32
  it "increments the related counter on misses" do
33
- cache.should_receive(:increment).with("User_misses")
33
+ cache.should_receive(:increment).with("User_misses", 1)
34
34
  cache.read("user_1").should eq nil
35
35
  end
36
36
  end
@@ -1,19 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
- # For tests of #read_entry, see spec/lib/active_support_cache_subclass_spec.rb
4
3
  describe BattleShip do
5
4
  let(:dummy) { Class.new { prepend(BattleShip) }.new }
6
5
 
7
6
  describe "#hits" do
8
7
  it "reads, skipping increment" do
9
- dummy.should_receive(:read).with("Foo_hits", { skip_increment: true })
8
+ dummy.should_receive(:read).with("Foo_hits", { skip_increment: true, raw: true })
10
9
  dummy.hits("Foo")
11
10
  end
12
11
  end
13
12
 
14
13
  describe "#misses" do
15
14
  it "reads, skipping increment" do
16
- dummy.should_receive(:read).with("Foo_misses", { skip_increment: true })
15
+ dummy.should_receive(:read).with("Foo_misses", { skip_increment: true, raw: true })
17
16
  dummy.misses("Foo")
18
17
  end
19
18
  end
@@ -44,4 +43,16 @@ describe BattleShip do
44
43
  end
45
44
  end
46
45
  end
46
+
47
+ describe ".include!" do
48
+ subject { BattleShip.include! }
49
+ it "forcefully prepends self to Rails.cache.class" do
50
+ klass = double
51
+ cache = double
52
+ Rails.should_receive(:cache) { cache }
53
+ cache.should_receive(:class) { klass }
54
+ klass.should_receive(:class_eval).with("prepend BattleShip")
55
+ subject
56
+ end
57
+ end
47
58
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,11 @@
1
+ class Cache
2
+ end
3
+ class Rails
4
+ def self.cache
5
+ Cache
6
+ end
7
+ end
8
+
1
9
  require 'active_support'
2
10
  require 'battle_ship'
3
11
 
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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - DavidRagone
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-05 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,7 +123,7 @@ rubyforge_project:
123
123
  rubygems_version: 2.0.3
124
124
  signing_key:
125
125
  specification_version: 4
126
- summary: Wrapper for Rails.cache methods
126
+ summary: Keep track of cache hits and misses
127
127
  test_files:
128
128
  - spec/lib/active_support_cache_subclass_spec.rb
129
129
  - spec/lib/battle_ship_spec.rb