benny_cache 0.0.1
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 +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +281 -0
- data/Rakefile +15 -0
- data/benny_cache.gemspec +23 -0
- data/lib/benny_cache.rb +7 -0
- data/lib/benny_cache/base.rb +36 -0
- data/lib/benny_cache/cache.rb +44 -0
- data/lib/benny_cache/config.rb +21 -0
- data/lib/benny_cache/model.rb +264 -0
- data/lib/benny_cache/related.rb +58 -0
- data/lib/benny_cache/version.rb +3 -0
- data/spec/models/cache_spec.rb +59 -0
- data/spec/models/config_spec.rb +15 -0
- data/spec/models/model_method_cache_spec.rb +85 -0
- data/spec/models/model_spec.rb +116 -0
- data/spec/models/related_spec.rb +63 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/test_classes.rb +73 -0
- metadata +137 -0
@@ -0,0 +1,116 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
require 'mocha_standalone'
|
4
|
+
|
5
|
+
describe BennyCache::Model do
|
6
|
+
it "should exist" do
|
7
|
+
BennyCache::Model.should be_true
|
8
|
+
end
|
9
|
+
|
10
|
+
describe " " do
|
11
|
+
before(:each) do
|
12
|
+
@model = ModelCacheFake.new
|
13
|
+
@model.id = 1
|
14
|
+
@model.other_id = 123
|
15
|
+
@model.x = 12
|
16
|
+
@model.y = 36
|
17
|
+
@store = BennyCache::Cache.new
|
18
|
+
BennyCache::Config.store=@store
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should fetch a model by id from cache" do
|
22
|
+
ModelCacheFake.expects(:find).with(1).returns(@model)
|
23
|
+
|
24
|
+
rv = ModelCacheFake.benny_model_cache(1)
|
25
|
+
rv.id.should == 1
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should fetch a model by a lookup hash from cache" do
|
29
|
+
arel= Object.new
|
30
|
+
ModelCacheFake.expects(:where).with(:other_id => 123).returns(arel)
|
31
|
+
arel.expects(:first).returns(@model)
|
32
|
+
rv = ModelCacheFake.benny_model_cache(:other_id => 123)
|
33
|
+
rv.id.should == 1
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should fire the after save callback when needed" do
|
37
|
+
ModelCacheFake.expects(:find).with(1).returns(@model)
|
38
|
+
|
39
|
+
rv = ModelCacheFake.benny_model_cache(1)
|
40
|
+
rv.id.should == 1
|
41
|
+
rv.id = 2
|
42
|
+
rv.expects(:benny_model_cache_delete)
|
43
|
+
rv.save
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should clear the cache when destroyed" do
|
48
|
+
ModelCacheFake.expects(:find).with(1).returns(@model)
|
49
|
+
|
50
|
+
rv = ModelCacheFake.benny_model_cache(1)
|
51
|
+
rv.id.should == 1
|
52
|
+
rv.other_id = 123
|
53
|
+
|
54
|
+
key = 'Benny/ModelCacheFake/1'
|
55
|
+
okey = 'Benny/ModelCacheFake/other_id/123'
|
56
|
+
fkey = 'Benny/ModelCacheFake/x/12/y/36'
|
57
|
+
|
58
|
+
@store.expects(:delete).with(key)
|
59
|
+
@store.expects(:delete).with(okey)
|
60
|
+
@store.expects(:delete).with(fkey)
|
61
|
+
rv.destroy
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should clear the cache when saved" do
|
65
|
+
|
66
|
+
ModelCacheFake.expects(:find).with(1).returns(@model)
|
67
|
+
|
68
|
+
rv = ModelCacheFake.benny_model_cache(1)
|
69
|
+
rv.id.should == 1
|
70
|
+
rv.other_id = 123
|
71
|
+
|
72
|
+
key = 'Benny/ModelCacheFake/1'
|
73
|
+
okey = 'Benny/ModelCacheFake/other_id/123'
|
74
|
+
fkey = 'Benny/ModelCacheFake/x/12/y/36'
|
75
|
+
|
76
|
+
|
77
|
+
@store.expects(:delete).with(key)
|
78
|
+
@store.expects(:delete).with(okey)
|
79
|
+
@store.expects(:delete).with(fkey)
|
80
|
+
rv.save
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should fetch a model by a multi-field index" do
|
84
|
+
arel= Object.new
|
85
|
+
ModelCacheFake.expects(:where).with(:x => 12, :y => 36).returns(arel)
|
86
|
+
arel.expects(:first).returns(@model)
|
87
|
+
ModelCacheFake.benny_model_cache(:x => 12, :y => 36)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should fetch data with a yield block" do
|
91
|
+
|
92
|
+
stuff = @model.benny_data_cache(:stuff) do
|
93
|
+
@model.stuff
|
94
|
+
end
|
95
|
+
|
96
|
+
stuff.class.should == Array
|
97
|
+
stuff[0].should == :stuff1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should supports custom namespaces" do
|
102
|
+
@model = ModelCacheFakeWithNs.new
|
103
|
+
@model.id = 1
|
104
|
+
|
105
|
+
store = BennyCache::Cache.new
|
106
|
+
BennyCache::Config.store=store
|
107
|
+
|
108
|
+
ModelCacheFakeWithNs.expects(:find).with(1).returns(@model)
|
109
|
+
|
110
|
+
rv = ModelCacheFakeWithNs.benny_model_cache(1)
|
111
|
+
rv.id.should == 1
|
112
|
+
|
113
|
+
ModelCacheFakeWithNs.get_benny_model_ns.should == "Benny/custom_ns"
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
require 'mocha_standalone'
|
4
|
+
|
5
|
+
describe BennyCache::Related do
|
6
|
+
it "should exist" do
|
7
|
+
BennyCache::Related.should be_true
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "" do
|
11
|
+
before(:each) do
|
12
|
+
BennyCache::Config.store=BennyCache::Cache.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should clear a related index when saved" do
|
16
|
+
@related = RelatedCacheFake.new
|
17
|
+
@related.model_id = '456'
|
18
|
+
@related.expects(:benny_cache_clear_related)
|
19
|
+
@related.save
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should try to clear a related index when saved" do
|
23
|
+
@related = RelatedCacheFake.new
|
24
|
+
@related.model_id = '456'
|
25
|
+
ModelCacheFake.expects(:benny_data_cache_delete).with('456', 'stuff')
|
26
|
+
BennyCache::Config.store.expects(:delete).with('Benny/ModelCacheFake/456/method/method_to_cache')
|
27
|
+
|
28
|
+
@related.save
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should clear a cache key" do
|
32
|
+
@related = RelatedCacheFake.new
|
33
|
+
@related.model_id = '456'
|
34
|
+
BennyCache::Config.store.expects(:delete).with('Benny/ModelCacheFake/456/method/method_to_cache')
|
35
|
+
BennyCache::Config.store.expects(:delete).with('Benny/ModelCacheFake/456/data/stuff')
|
36
|
+
@related.save
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should clear a related method when saved" do
|
40
|
+
|
41
|
+
@model = ModelCacheFake.new
|
42
|
+
|
43
|
+
@model.id = 456
|
44
|
+
@model.method_to_cache :foo
|
45
|
+
@model.method_to_cache :bar
|
46
|
+
|
47
|
+
@related = RelatedCacheFake.new
|
48
|
+
@related.model_id = '456'
|
49
|
+
|
50
|
+
model_base_index = "Benny/ModelCacheFake/456/method/method_to_cache"
|
51
|
+
rv = BennyCache::Config.store.read(model_base_index)
|
52
|
+
rv.class.should == Array
|
53
|
+
rv.size.should == 2
|
54
|
+
|
55
|
+
@related.save
|
56
|
+
|
57
|
+
rv = BennyCache::Config.store.read(model_base_index)
|
58
|
+
rv.should be_nil
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.adapters.define 'gem' do
|
4
|
+
add_filter '/test/'
|
5
|
+
add_filter '/features/'
|
6
|
+
add_filter '/spec/'
|
7
|
+
add_filter '/autotest/'
|
8
|
+
|
9
|
+
add_group 'Binaries', '/bin/'
|
10
|
+
add_group 'Libraries', '/lib/'
|
11
|
+
add_group 'Extensions', '/ext/'
|
12
|
+
add_group 'Vendor Libraries', '/vendor/'
|
13
|
+
end
|
14
|
+
|
15
|
+
SimpleCov.start :gem
|
16
|
+
end
|
17
|
+
|
18
|
+
require_relative '../lib/benny_cache'
|
19
|
+
require_relative "./test_classes"
|
20
|
+
|
21
|
+
#
|
22
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
23
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
24
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
25
|
+
# loaded once.
|
26
|
+
#
|
27
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
28
|
+
RSpec.configure do |config|
|
29
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
30
|
+
config.run_all_when_everything_filtered = true
|
31
|
+
config.filter_run :focus
|
32
|
+
config.mock_with :mocha
|
33
|
+
# Run specs in random order to surface order dependencies. If you find an
|
34
|
+
# order dependency and want to debug it, you can fix the order by providing
|
35
|
+
# the seed, which is printed after each run.
|
36
|
+
# --seed 1234
|
37
|
+
config.order = 'random'
|
38
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
class ARFaker
|
2
|
+
|
3
|
+
def save
|
4
|
+
self.class.class_variable_get(:@@after_save_callbacks).each do |cb|
|
5
|
+
self.send(cb)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def destroy
|
10
|
+
self.class.class_variable_get(:@@after_destroy_callbacks).each do |cb|
|
11
|
+
self.send(cb)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.after_save(*methods)
|
16
|
+
unless self.class_variable_defined?(:@@after_save_callbacks)
|
17
|
+
self.class_variable_set(:@@after_save_callbacks, [])
|
18
|
+
end
|
19
|
+
self.class_variable_get(:@@after_save_callbacks).push(*methods)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.after_destroy(*methods)
|
23
|
+
unless self.class_variable_defined?(:@@after_destroy_callbacks)
|
24
|
+
self.class_variable_set(:@@after_destroy_callbacks, [])
|
25
|
+
end
|
26
|
+
|
27
|
+
self.class_variable_get(:@@after_destroy_callbacks).push(*methods)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
class ModelCacheFake < ARFaker
|
34
|
+
include BennyCache::Model
|
35
|
+
|
36
|
+
attr_accessor :id, :other_id, :x, :y
|
37
|
+
|
38
|
+
benny_model_index :other_id, [:x, :y]
|
39
|
+
benny_data_index :stuff
|
40
|
+
|
41
|
+
def stuff
|
42
|
+
[:stuff1, :stuff2]
|
43
|
+
end
|
44
|
+
|
45
|
+
def method_to_cache_with_base_data
|
46
|
+
[:a, :b, :c]
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_to_cache(*options)
|
50
|
+
return options
|
51
|
+
end
|
52
|
+
benny_method_index :method_to_cache, :method_to_cache_with_base_data
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class ModelCacheFakeWithNs < ARFaker
|
57
|
+
include BennyCache::Model
|
58
|
+
benny_model_ns :custom_ns
|
59
|
+
|
60
|
+
attr_accessor :id, :other_id
|
61
|
+
end
|
62
|
+
|
63
|
+
class RelatedCacheFake < ARFaker
|
64
|
+
include BennyCache::Related
|
65
|
+
benny_related_index ":model_id/ModelCacheFake/stuff"
|
66
|
+
|
67
|
+
benny_related_method ":model_id/ModelCacheFake/method_to_cache"
|
68
|
+
|
69
|
+
attr_accessor :model_id
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benny_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Hilton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mocha
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: ZenTest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: simplecov
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: A model caching library with indirect cached clearing
|
79
|
+
email:
|
80
|
+
- mshiltonj@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- benny_cache.gemspec
|
92
|
+
- lib/benny_cache.rb
|
93
|
+
- lib/benny_cache/base.rb
|
94
|
+
- lib/benny_cache/cache.rb
|
95
|
+
- lib/benny_cache/config.rb
|
96
|
+
- lib/benny_cache/model.rb
|
97
|
+
- lib/benny_cache/related.rb
|
98
|
+
- lib/benny_cache/version.rb
|
99
|
+
- spec/models/cache_spec.rb
|
100
|
+
- spec/models/config_spec.rb
|
101
|
+
- spec/models/model_method_cache_spec.rb
|
102
|
+
- spec/models/model_spec.rb
|
103
|
+
- spec/models/related_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- spec/test_classes.rb
|
106
|
+
homepage: https://github.com/mshiltonj/benny_cache
|
107
|
+
licenses: []
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.24
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: A model caching library with indirect cached clearing
|
130
|
+
test_files:
|
131
|
+
- spec/models/cache_spec.rb
|
132
|
+
- spec/models/config_spec.rb
|
133
|
+
- spec/models/model_method_cache_spec.rb
|
134
|
+
- spec/models/model_spec.rb
|
135
|
+
- spec/models/related_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/test_classes.rb
|