cache-machine 0.1.10 → 0.2.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.
Files changed (38) hide show
  1. data/.gitignore +3 -1
  2. data/.travis.yml +6 -0
  3. data/Gemfile +0 -2
  4. data/Gemfile.lock +0 -2
  5. data/README.md +258 -0
  6. data/VERSION +1 -1
  7. data/cache-machine.gemspec +7 -22
  8. data/db/.gitignore +1 -0
  9. data/lib/cache-machine.rb +6 -2
  10. data/lib/cache_machine/adapter.rb +127 -0
  11. data/lib/cache_machine/adapters/rails.rb +58 -0
  12. data/lib/cache_machine/cache.rb +34 -85
  13. data/lib/cache_machine/cache/associations.rb +29 -0
  14. data/lib/cache_machine/cache/class_timestamp.rb +31 -0
  15. data/lib/cache_machine/cache/collection.rb +131 -0
  16. data/lib/cache_machine/cache/map.rb +97 -241
  17. data/lib/cache_machine/cache/mapper.rb +135 -0
  18. data/lib/cache_machine/cache/resource.rb +108 -0
  19. data/lib/cache_machine/cache/scope.rb +24 -0
  20. data/lib/cache_machine/cache/timestamp_builder.rb +58 -0
  21. data/lib/cache_machine/helpers/cache_helper.rb +3 -3
  22. data/lib/cache_machine/logger.rb +9 -8
  23. data/lib/cache_machine/railtie.rb +11 -0
  24. data/lib/cache_machine/tasks.rb +10 -0
  25. data/spec/fixtures.rb +11 -6
  26. data/spec/lib/cache_machine/adapters/rails_spec.rb +149 -0
  27. data/spec/lib/cache_machine/cache/associations_spec.rb +32 -0
  28. data/spec/lib/cache_machine/cache/class_timestam_spec.rb +29 -0
  29. data/spec/lib/cache_machine/cache/collection_spec.rb +106 -0
  30. data/spec/lib/cache_machine/cache/map_spec.rb +34 -0
  31. data/spec/lib/cache_machine/cache/mapper_spec.rb +61 -0
  32. data/spec/lib/cache_machine/cache/resource_spec.rb +86 -0
  33. data/spec/lib/cache_machine/cache/scope_spec.rb +50 -0
  34. data/spec/lib/cache_machine/cache/timestamp_builder_spec.rb +52 -0
  35. data/spec/spec_helper.rb +9 -3
  36. metadata +35 -61
  37. data/README.rdoc +0 -186
  38. data/spec/lib/cache_machine_spec.rb +0 -161
@@ -1,161 +0,0 @@
1
- require 'spec_helper'
2
- require 'fixtures'
3
-
4
- describe CacheMachine do
5
- subject { Cacher.create(:name => 'foo') }
6
-
7
- describe "#cache_key_of" do
8
- it "generates association cache keys" do
9
- subject.cache_key_of(:anything).should eql("Cacher/foo/anything/1")
10
- subject.cache_key_of(:anything, :format => :ehtml).should eql("Cacher/foo/anything/ehtml/1")
11
- subject.cache_key_of(:anything, :page => 2).should eql("Cacher/foo/anything/2")
12
- subject.cache_key_of(:anything, :format => :ehtml, :page => 2).should eql("Cacher/foo/anything/ehtml/2")
13
- end
14
- end
15
-
16
- describe "#fetch_cache_of" do
17
- it "stores association cache" do
18
- subject.fetch_cache_of(:has_many_cacheables) { 'cache' }
19
- subject.fetch_cache_of(:has_many_cacheables).should == 'cache'
20
- end
21
-
22
- context "timestamps" do
23
- it "works with timestamps" do
24
- subject.should_receive(:cache_key_of).with(:anything, hash_including(:timestamp => :dynamic_timestamp)).and_return "returned stamp"
25
- Rails.cache.should_receive(:fetch).with("returned stamp", :expires_in => nil).once
26
- subject.fetch_cache_of(:anything, :timestamp => :dynamic_timestamp)
27
- end
28
-
29
- it "calls for instance methods" do
30
- subject.should_receive(:execute_timestamp).once
31
- subject.fetch_cache_of(:anything, :timestamp => :dynamic_timestamp)
32
- end
33
-
34
- it "passes expires_in param" do
35
- Rails.cache.should_receive(:fetch).with(anything(), :expires_in => 10.minutes).once
36
- subject.fetch_cache_of(:anything, :expires_in => 10.minutes)
37
- end
38
-
39
- it "passes expires_at param" do
40
- Time.stub_chain("zone.now").and_return(Time.parse('01/01/01'))
41
- Rails.cache.should_receive(:fetch).with(anything(), :expires_in => 10.minutes).once
42
- subject.fetch_cache_of(:anything, :expires_at => 10.minutes.from_now)
43
- end
44
- end
45
- end
46
-
47
- context "resets cache" do
48
- describe "#delete_all_caches" do
49
- it "removes all caches using map" do
50
- subject.should_receive(:delete_cache_of).with(:polymorphics).once
51
- subject.should_receive(:delete_cache_of).with(:child_cachers).once
52
- subject.should_receive(:delete_cache_of).with(:has_many_cacheables).once
53
- subject.should_receive(:delete_cache_of).with(:dependent_cache).once
54
- subject.should_receive(:delete_cache_of).with(:has_many_through_cacheables).once
55
- subject.should_receive(:delete_cache_of).with(:has_and_belongs_to_many_cacheables).once
56
- subject.delete_all_caches
57
- end
58
- end
59
-
60
- describe "#delete_cache_of" do
61
- it "resets cache" do
62
- subject.fetch_cache_of(:anything) { 'cache' }
63
- subject.delete_cache_of :anything
64
- subject.fetch_cache_of(:anything).should be_nil
65
- end
66
-
67
- it "resets cache by map" do
68
- subject.fetch_cache_of(:dependent_cache) { 'cache' }
69
- subject.delete_cache_of :has_many_cacheables
70
- subject.fetch_cache_of(:dependent_cache).should be_nil
71
- end
72
-
73
- context "callbacks" do
74
- context "on polymorphic associations" do
75
- it "resets cache on add new item to associated collection" do
76
- subject.fetch_cache_of(:polymorphics) { 'cache' }
77
- subject.polymorphics.create
78
- subject.fetch_cache_of(:polymorphics).should be_nil
79
- end
80
- end
81
-
82
- context "on self-join associations" do
83
- it "resets cache on add new item to associated collection" do
84
- subject.fetch_cache_of(:child_cachers) { 'cache' }
85
- Cacher.create(:parent_id => subject.id)
86
- subject.fetch_cache_of(:child_cachers).should be_nil
87
- end
88
- end
89
-
90
- context "on has_many associations" do
91
- let(:new_entry) { HasManyCacheable.create }
92
-
93
- before :each do
94
- @existing_entry = subject.has_many_cacheables.create
95
- subject.fetch_cache_of(:has_many_cacheables) { 'cache' }
96
- end
97
-
98
- after :each do
99
- subject.fetch_cache_of(:has_many_cacheables).should be_nil
100
- subject.delete_cache_of(:has_many_cacheables)
101
- end
102
-
103
- it("on update entry in collection") { @existing_entry.save }
104
- it("on add new entry in collection") { subject.has_many_cacheables << new_entry }
105
- it("on destroy intem in collection") { subject.has_many_cacheables.destroy @existing_entry }
106
- it("on destroy intem") { HasManyCacheable.destroy @existing_entry }
107
- end
108
- end
109
-
110
- context "has_many :through associations" do
111
- let(:new_entry) { HasManyThroughCacheable.create }
112
-
113
- before :each do
114
- @existing_entry = subject.has_many_through_cacheables.create
115
- subject.fetch_cache_of(:has_many_through_cacheables) { 'cache' }
116
- end
117
-
118
- after :each do
119
- subject.fetch_cache_of(:has_many_through_cacheables).should be_nil
120
- subject.delete_cache_of(:has_many_through_cacheables)
121
- end
122
-
123
- it("on update entry in collection") { @existing_entry.save }
124
- it("on add new entry in collection") { subject.has_many_through_cacheables << new_entry }
125
- it("on destroy intem in collection") { subject.has_many_through_cacheables.destroy @existing_entry }
126
- it("on destroy intem") { HasManyThroughCacheable.destroy @existing_entry }
127
- end
128
-
129
- context "has_and_belongs_to_many associations" do
130
- let(:new_entry) { HasAndBelongsToManyCacheable.create }
131
- before :each do
132
- @existing_entry = subject.has_and_belongs_to_many_cacheables.create
133
- subject.fetch_cache_of(:has_and_belongs_to_many_cacheables) { 'cache' }
134
- end
135
-
136
- after :each do
137
- subject.fetch_cache_of(:has_and_belongs_to_many_cacheables).should be_nil
138
- subject.delete_cache_of(:has_and_belongs_to_many_cacheables)
139
- end
140
-
141
- it("on update entry in collection") { @existing_entry.save }
142
- it("on add new entry in collection") { subject.has_and_belongs_to_many_cacheables << new_entry }
143
- it("on destroy intem in collection") { subject.has_and_belongs_to_many_cacheables.destroy @existing_entry }
144
- it("on destroy intem") { HasAndBelongsToManyCacheable.destroy @existing_entry }
145
- end
146
-
147
- context "paginated content" do
148
- it "works" do
149
- subject.delete_cache_of :has_many_cacheables
150
- subject.fetch_cache_of(:has_many_cacheables, :page => 1) { 'page 1' }.should eql('page 1')
151
- subject.fetch_cache_of(:has_many_cacheables, :page => 2) { 'page 2' }.should eql('page 2')
152
-
153
- subject.delete_cache_of(:has_many_cacheables)
154
-
155
- subject.fetch_cache_of(:has_many_cacheables, :page => 1).should be_nil
156
- subject.fetch_cache_of(:has_many_cacheables, :page => 2).should be_nil
157
- end
158
- end
159
- end
160
- end
161
- end