cache-object 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8ed3e3acf8e4c9fe23e4a3d0f73b067e1570318
4
- data.tar.gz: d7f3afcb8be6c0d3065c97e7a1e7f027c25621bf
3
+ metadata.gz: c2a98fe0b987a204106bb9e8f273ae36b7af394e
4
+ data.tar.gz: 32890adb0048d8f08c61bad3e3041c58841b2fdd
5
5
  SHA512:
6
- metadata.gz: b77a6a97aa5e46e6d9d01e16f97fd1e9f724c7029fc21c230ffc622f7bb9b67545cb81407b7010063329f5a3f9b881d949aafc3f1aebdcb3b00ef867097fbdb1
7
- data.tar.gz: a0418a92720e49b01648b7bc6730f6d56070d8919c5845e8e32292de0d51197405521053952382d5ccde59e7925382f1a75ef45decde9e96fc4736ba14a58fa0
6
+ metadata.gz: 84721447edb9c798e46b9b8e094153c8472dfef2ad52803027cdafdefc698d2a7978c437650406f331450767dcc1dbbd1786e78e9f2dda062440ebaf3a49ed84
7
+ data.tar.gz: 4d492ccb088d4620d9167bf18a15598b10f9b4d1ab988779a0747aef8f44f9a43d27df0db195f2e7c6261a2f51c76327ec6bc3f0f00bff724f5715efc17ab5b3
data/cache-object.gemspec CHANGED
@@ -19,12 +19,15 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "activerecord", ">= 3.0"
22
+ spec.add_dependency "ruby-usdt"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "rake"
25
26
 
26
27
  spec.add_development_dependency "rspec", ">= 3.0"
27
28
  spec.add_development_dependency "rspec-collection_matchers", ">= 1.0"
29
+ spec.add_development_dependency "rspec-its", ">= 1.0"
30
+
28
31
  spec.add_development_dependency "guard"
29
32
  spec.add_development_dependency "guard-rspec"
30
33
  spec.add_development_dependency "sqlite3"
data/lib/cache/object.rb CHANGED
@@ -4,6 +4,7 @@ require 'cache/object/active_record'
4
4
  require 'cache/object/adapter'
5
5
  require 'cache/object/key_generator'
6
6
  require 'cache/object/multi_get'
7
+ require 'cache/object/dtrace_provider'
7
8
 
8
9
  module Cache
9
10
  module Object
@@ -9,31 +9,46 @@ module Cache
9
9
  end
10
10
 
11
11
  def write(decorator)
12
+ DTraceProvider.fire!(:write, decorator.instance.class.name, decorator.instance.id.to_s, ttl.to_s)
13
+
12
14
  decorator.keys.each do |key|
13
15
  store.write(key, decorator.instance, expires_in: ttl)
14
16
  end
15
17
  end
16
18
 
17
19
  def delete(decorator)
20
+ DTraceProvider.fire!(:delete, decorator.instance.class.name, decorator.instance.id.to_s)
21
+
18
22
  decorator.keys.each do |key|
19
23
  store.delete(key)
20
24
  end
21
25
  end
22
26
 
23
- def fetch(klass, id, &block)
24
- store.fetch(KeyGenerator.key_for_object(klass.name, id),
25
- expires_in: ttl,
26
- &block)
27
+ def fetch(klass, id)
28
+ DTraceProvider.fire!(:fetch, klass.name, id.to_s, ttl.to_s)
29
+
30
+ store.fetch(KeyGenerator.key_for_object(klass.name, id), expires_in: ttl) do
31
+ DTraceProvider.fire!(:fetch_miss, klass.name, id.to_s, ttl.to_s)
32
+ yield
33
+ end
27
34
  end
28
35
 
29
36
  def fetch_mapping(klass, attributes, &block)
30
- store.fetch(KeyGenerator.key_for_mapping(klass.name, attributes),
31
- expires_in: ttl,
32
- &block)
37
+ DTraceProvider.fire!(:fetch_mapping, klass.name, attributes.inspect, ttl.to_s)
38
+
39
+ store.fetch(KeyGenerator.key_for_mapping(klass.name, attributes), expires_in: ttl) do
40
+ DTraceProvider.fire!(:fetch_mapping_miss, klass.name, attributes.inspect, ttl.to_s)
41
+ yield
42
+ end
33
43
  end
34
44
 
35
45
  def read_multi(args)
36
- store.read_multi(*args)
46
+ total = args.size
47
+ result = store.read_multi(*args)
48
+ found = result.size
49
+
50
+ DTraceProvider.fire!(:read_multi, args.inspect, found, total - found)
51
+ result
37
52
  end
38
53
 
39
54
  private
@@ -0,0 +1,43 @@
1
+ require 'usdt'
2
+
3
+ module Cache
4
+ module Object
5
+ class DTraceProvider
6
+ attr_reader :provider, :probes
7
+
8
+ def initialize
9
+ @provider = USDT::Provider.create(:ruby, :cache_object)
10
+
11
+ @probes = {
12
+ # args: Class name, id, ttl
13
+ fetch: provider.probe(:adapter, :fetch, :string, :string, :string),
14
+ # args: Class name, id, ttl
15
+ fetch_miss: provider.probe(:adapter, :fetch_miss, :string, :string, :string),
16
+ # args: Class name, attributes.inspect, ttl
17
+ fetch_mapping: provider.probe(:adapter, :fetch_mapping, :string, :string, :string),
18
+ # args: Class name, attributes.inspect, ttl
19
+ fetch_mapping_miss: provider.probe(:adapter, :fetch_mapping_miss, :string, :string, :string),
20
+ # args: args.inspect, hits, misses
21
+ read_multi: provider.probe(:adapter, :read_multi, :string, :integer, :integer),
22
+ # args: class_name, id, ttl
23
+ write: provider.probe(:adapter, :write, :string, :string, :string),
24
+ # args: class_name, id
25
+ delete: provider.probe(:adapter, :delete, :string, :string, :string)
26
+ }
27
+ end
28
+
29
+ def self.provider
30
+ @provider ||= new.tap do |p|
31
+ p.provider.enable
32
+ end
33
+ end
34
+
35
+ def self.fire!(probe_name, *args)
36
+ raise "Unknown probe: #{probe_name}" unless self.provider.probes[probe_name]
37
+ probe = self.provider.probes[probe_name]
38
+ probe.fire(*args) if probe.enabled?
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -1,5 +1,5 @@
1
1
  module Cache
2
2
  module Object
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -8,7 +8,7 @@ RSpec.describe Cache::Object::Adapter do
8
8
 
9
9
  let(:cache_store) { double("CacheStore", write: true) }
10
10
  let(:adapter) { Cache::Object::Adapter.new(cache_store) }
11
- let(:instance) { double(class: double(name: "User")) }
11
+ let(:instance) { double(class: double(name: "User"), id: "1") }
12
12
 
13
13
  describe "#delete" do
14
14
  it "dispatches to delete" do
@@ -17,6 +17,17 @@ RSpec.describe Cache::Object::Adapter do
17
17
 
18
18
  adapter.delete(double(instance: instance, keys: ["User-1", "User-1-blah"]))
19
19
  end
20
+
21
+ describe "probes" do
22
+ before do
23
+ allow(cache_store).to receive(:delete)
24
+ end
25
+
26
+ it "fires the write probe" do
27
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:delete, "User", "1")
28
+ adapter.delete(double(instance: instance, keys: ["User-1", "User-1-blah"]))
29
+ end
30
+ end
20
31
  end
21
32
 
22
33
  describe "#write" do
@@ -26,6 +37,17 @@ RSpec.describe Cache::Object::Adapter do
26
37
 
27
38
  adapter.write(double(instance: instance, keys: ["User-1", "User-1-blah"]))
28
39
  end
40
+
41
+ describe "probes" do
42
+ before do
43
+ allow(cache_store).to receive(:write)
44
+ end
45
+
46
+ it "fires the write probe" do
47
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:write, "User", "1", "118")
48
+ adapter.write(double(instance: instance, keys: ["User-1", "User-1-blah"]))
49
+ end
50
+ end
29
51
  end
30
52
 
31
53
  describe "#fetch" do
@@ -35,6 +57,23 @@ RSpec.describe Cache::Object::Adapter do
35
57
  adapter.fetch(instance.class, 1, &b)
36
58
  }.to yield_control
37
59
  end
60
+
61
+ describe "probes" do
62
+ it "fires the fetch probe" do
63
+ allow(cache_store).to receive(:fetch)
64
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch, "User", "1", "118")
65
+ adapter.fetch(instance.class, 1) {}
66
+ end
67
+
68
+ describe "when fetch is a miss" do
69
+ it "fires the fetch and fetch_miss probes" do
70
+ allow(cache_store).to receive(:fetch).and_yield
71
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch, "User", "1", "118")
72
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch_miss, "User", "1", "118")
73
+ adapter.fetch(instance.class, 1) {}
74
+ end
75
+ end
76
+ end
38
77
  end
39
78
 
40
79
  describe "#fetch_mapping" do
@@ -44,5 +83,37 @@ RSpec.describe Cache::Object::Adapter do
44
83
  adapter.fetch_mapping(instance.class, { user_id: 1, name: "bob" }, &b)
45
84
  }.to yield_control
46
85
  end
86
+
87
+ describe "probes" do
88
+ it "fires the fetch probe" do
89
+ allow(cache_store).to receive(:fetch)
90
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch_mapping, "User", { user_id: 1, name: "bob" }.inspect, "118")
91
+ adapter.fetch_mapping(instance.class, { user_id: 1, name: "bob" }) {}
92
+ end
93
+
94
+ describe "when fetch is a miss" do
95
+ it "fires the fetch and fetch_miss probes" do
96
+ allow(cache_store).to receive(:fetch).and_yield
97
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch_mapping, "User", { user_id: 1, name: "bob" }.inspect, "118")
98
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:fetch_mapping_miss, "User", { user_id: 1, name: "bob" }.inspect, "118")
99
+ adapter.fetch_mapping(instance.class, { user_id: 1, name: "bob" }) {}
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#read_multi" do
106
+ it "calls read_multi on the cache_store" do
107
+ expect(cache_store).to receive(:read_multi).with("blah").and_return({})
108
+ adapter.read_multi(["blah"])
109
+ end
110
+
111
+ describe "probes" do
112
+ it "fires the read_multi probe" do
113
+ expect(Cache::Object::DTraceProvider).to receive(:fire!).with(:read_multi, ["blah", "blah2"].inspect, 1, 1)
114
+ allow(cache_store).to receive(:read_multi).and_return({"blah2" => 1})
115
+ adapter.read_multi(["blah", "blah2"])
116
+ end
117
+ end
47
118
  end
48
119
  end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Cache::Object::DTraceProvider do
4
+ describe 'initialize' do
5
+ it 'creates a new provider' do
6
+ expect(USDT::Provider).to receive(:create).with(:ruby, :cache_object).and_return(double(probe: true))
7
+ Cache::Object::DTraceProvider.new
8
+ end
9
+ end
10
+
11
+ describe 'probes' do
12
+ let(:provider) { Cache::Object::DTraceProvider.new }
13
+
14
+ describe '#fetch' do
15
+ subject(:probe) { provider.probes[:fetch] }
16
+ its(:function) { should eq :adapter }
17
+ its(:name) { should eq :fetch }
18
+ its(:arguments) { should eq [:string, :string, :string] }
19
+ end
20
+
21
+ describe '#fetch_miss' do
22
+ subject(:probe) { provider.probes[:fetch_miss] }
23
+ its(:function) { should eq :adapter }
24
+ its(:name) { should eq :fetch_miss }
25
+ its(:arguments) { should eq [:string, :string, :string] }
26
+ end
27
+
28
+
29
+ describe '#fetch_mapping' do
30
+ subject(:probe) { provider.probes[:fetch_mapping] }
31
+ its(:function) { should eq :adapter }
32
+ its(:name) { should eq :fetch_mapping }
33
+ its(:arguments) { should eq [:string, :string, :string] }
34
+ end
35
+
36
+ describe '#fetch_mapping_miss' do
37
+ subject(:probe) { provider.probes[:fetch_mapping_miss] }
38
+ its(:function) { should eq :adapter }
39
+ its(:name) { should eq :fetch_mapping_miss }
40
+ its(:arguments) { should eq [:string, :string, :string] }
41
+ end
42
+
43
+
44
+ describe '#read_multi' do
45
+ subject(:probe) { provider.probes[:read_multi] }
46
+ its(:function) { should eq :adapter }
47
+ its(:name) { should eq :read_multi }
48
+ its(:arguments) { should eq [:string, :integer, :integer] }
49
+ end
50
+
51
+ describe '#write' do
52
+ subject(:probe) { provider.probes[:write] }
53
+ its(:function) { should eq :adapter }
54
+ its(:name) { should eq :write }
55
+ its(:arguments) { should eq [:string, :string, :string] }
56
+ end
57
+
58
+ describe '#delete' do
59
+ subject(:probe) { provider.probes[:delete] }
60
+ its(:function) { should eq :adapter }
61
+ its(:name) { should eq :delete }
62
+ its(:arguments) { should eq [:string, :string, :string] }
63
+ end
64
+ end
65
+
66
+ describe ".fire!" do
67
+ it "raises if no provider" do
68
+ expect { Cache::Object::DTraceProvider.fire!(:fake) }.to raise_error
69
+ end
70
+
71
+ describe "when probe is enabled" do
72
+ it "receives event" do
73
+ probe = double(enabled?: true)
74
+ provider = double(probes: { boom: probe})
75
+ allow(Cache::Object::DTraceProvider).to receive(:provider).and_return(provider)
76
+ expect(probe).to receive(:fire).with("hai")
77
+ Cache::Object::DTraceProvider.fire!(:boom, "hai")
78
+ end
79
+ end
80
+
81
+ describe "when probe is disabled" do
82
+ it "does not receives event" do
83
+ probe = double(enabled?: false)
84
+ provider = double(probes: { boom: probe})
85
+ allow(Cache::Object::DTraceProvider).to receive(:provider).and_return(provider)
86
+ expect(probe).to receive(:fire).never
87
+ Cache::Object::DTraceProvider.fire!(:boom, "hai")
88
+ end
89
+ end
90
+ end
91
+
92
+ describe '::provider' do
93
+ it 'returns a DTraceProvider' do
94
+ provider = Cache::Object::DTraceProvider.provider
95
+ expect(provider).to be_a(Cache::Object::DTraceProvider)
96
+ end
97
+ end
98
+ end
99
+
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'cache/object'
2
2
  require 'rspec/collection_matchers'
3
+ require 'rspec/its'
3
4
  RSpec.configure do |config|
4
5
 
5
6
  config.before(:each) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache-object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Camuto
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-usdt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-its
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: guard
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +179,7 @@ files:
151
179
  - lib/cache/object/active_record.rb
152
180
  - lib/cache/object/adapter.rb
153
181
  - lib/cache/object/config.rb
182
+ - lib/cache/object/dtrace_provider.rb
154
183
  - lib/cache/object/instance_decorator.rb
155
184
  - lib/cache/object/key_generator.rb
156
185
  - lib/cache/object/multi_get.rb
@@ -158,6 +187,7 @@ files:
158
187
  - spec/cache/object/active_record_spec.rb
159
188
  - spec/cache/object/adapter_spec.rb
160
189
  - spec/cache/object/config_spec.rb
190
+ - spec/cache/object/dtrace_provider_spec.rb
161
191
  - spec/cache/object/instance_decorator_spec.rb
162
192
  - spec/cache/object/multi_get_spec.rb
163
193
  - spec/features/cache_object_spec.rb
@@ -191,8 +221,10 @@ test_files:
191
221
  - spec/cache/object/active_record_spec.rb
192
222
  - spec/cache/object/adapter_spec.rb
193
223
  - spec/cache/object/config_spec.rb
224
+ - spec/cache/object/dtrace_provider_spec.rb
194
225
  - spec/cache/object/instance_decorator_spec.rb
195
226
  - spec/cache/object/multi_get_spec.rb
196
227
  - spec/features/cache_object_spec.rb
197
228
  - spec/spec_helper.rb
198
229
  - spec/support/models.rb
230
+ has_rdoc: