cache-machine 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  = cache-machine
2
+ http://img195.imageshack.us/img195/5371/cachemachinefinal2.png
2
3
 
3
4
  An ActiveRecord mixin that helps managing cached content in a Ruby on Rails application with complex data update dependencies.
4
5
 
@@ -17,20 +18,20 @@ Cache Machine depends only on the Rails.cache API and is thereby library agnosti
17
18
  = Usage
18
19
 
19
20
  # Fetch cache of venues collection on model_instance.
20
- @neighborhood.fetch_cache_of(:venues) { @neighborhood.venues }
21
+ @neighborhood.fetch_cache_of(:venues) { venues }
21
22
 
22
23
  # Specify format.
23
- @neighborhood.fetch_cache_of(:venues, :format => :json) { @neighborhood.venues.to_json }
24
+ @neighborhood.fetch_cache_of(:venues, :format => :json) { venues.to_json }
24
25
 
25
26
  # Paginated content.
26
- @neighborhood.fetch_cache_of(:venues, :page => 2) { @neighborhood.venues.paginate(:page => 2) }
27
+ @neighborhood.fetch_cache_of(:venues, :page => 2) { venues.paginate(:page => 2) }
27
28
 
28
29
  In you target model define <b>cache map</b>:
29
30
 
30
- acts_as_cache_machine_for :venues => [:hotspots, :today_events],
31
- :cities => [:venues],
32
- :streets => :hotspots,
33
- :users
31
+ cache_map :venues => [:hotspots, :today_events],
32
+ :cities => [:venues],
33
+ :streets => :hotspots,
34
+ :users
34
35
 
35
36
  This example shows you how changes of one collection affect on invalidation process.
36
37
  For each record of your target model:
@@ -124,6 +125,9 @@ Want to see collection timestamp?
124
125
  # For timestamps.
125
126
  @lady_gaga.reset_timestamp_of :events
126
127
 
128
+ # You can reset all associated caches using map.
129
+ @lady_gaga.delete_all_caches
130
+
127
131
  == Cache formats
128
132
  Cache Machine invalidates cache using a couple of keys with the different formats.
129
133
  Default formats are:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.1.9
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cache-machine}
8
- s.version = "0.1.8"
8
+ s.version = "0.1.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sergei Zinin", "Kevin Goslar"]
12
- s.date = %q{2011-09-11}
12
+ s.date = %q{2011-10-13}
13
13
  s.description = %q{A Ruby on Rails framework to support cache management based on explicitely modeled caching dependencies.}
14
14
  s.email = %q{kgoslar@partyearth.com}
15
15
  s.extra_rdoc_files = [
@@ -37,6 +37,7 @@ module CacheMachine
37
37
  include CacheMachine::Cache::Map
38
38
  cache_associated(associations)
39
39
  end
40
+ alias :cache_map :acts_as_cache_machine_for
40
41
 
41
42
  # Returns timestamp of class collection.
42
43
  def timestamp format = nil
@@ -38,7 +38,14 @@ module CacheMachine
38
38
  # Called only when <tt>has_many :through</tt> collection changed.
39
39
  def delete_association_cache_on record, reflection
40
40
  pk = record.class.primary_key
41
- self.joins(reflection.name).where(reflection.table_name => { pk => record.send(pk) }).each do |cache_source_record|
41
+
42
+ joining = unless reflection.options[:source_type]
43
+ reflection.through_reflection ? { reflection.through_reflection.name => reflection.source_reflection.name } : reflection.name
44
+ else
45
+ reflection.name
46
+ end
47
+
48
+ self.joins(joining).where(reflection.table_name => { pk => record.send(pk) }).find_each do |cache_source_record|
42
49
  cache_source_record.delete_cache_of reflection.name
43
50
  end
44
51
  end
@@ -131,6 +138,13 @@ module CacheMachine
131
138
  Rails.cache.fetch(cache_key, :expires_in => expires_in, &block)
132
139
  end
133
140
 
141
+ # Removes all caches using map.
142
+ def delete_all_caches
143
+ self.class.cache_map.keys.each do |cached_collection|
144
+ delete_cache_of cached_collection
145
+ end
146
+ end
147
+
134
148
  # Recursively deletes cache by map for +_member+.
135
149
  def delete_cache_of _member
136
150
  delete_cache_of_only _member
@@ -179,6 +193,7 @@ module CacheMachine
179
193
  # Deletes cache of associated collection what contains +record+.
180
194
  # Called only when <tt>has_many :through</tt> collection changed.
181
195
  def delete_association_cache_on record
196
+
182
197
  # Find all associations with +record+ by its class.
183
198
  associations = self.class.reflect_on_all_associations.find_all { |association| association.klass == record.class }
184
199
 
@@ -19,12 +19,16 @@ module CacheMachine
19
19
  # Call like this in your your code, best in development.rb: ActiveRecord::CacheMachine::Logger.level = :info
20
20
  def level= value
21
21
  @@level = LOGGING_LEVELS[value] or raise "CACHE_MACHINE: Unknown log level: '#{value}'."
22
- puts "CACHE_MACHINE: Setting log level to '#{value}'."
22
+ if @@level <= LOGGING_LEVELS[:info]
23
+ puts "CACHE_MACHINE: Setting log level to '#{value}'.\n"
24
+ end
23
25
  end
24
26
 
25
27
  # Logs the given entry with the given log level.
26
28
  def write level, text
27
- puts text if @@level <= (LOGGING_LEVELS[level] or raise "CACHE_MACHINE: Unknown log level: '#{level}'.")
29
+ if @@level <= (LOGGING_LEVELS[level] or raise "CACHE_MACHINE: Unknown log level: '#{level}'.")
30
+ puts text
31
+ end
28
32
  end
29
33
  end
30
34
  end
@@ -70,11 +70,11 @@ describe CacheMachine do
70
70
  class Cacher < ActiveRecord::Base
71
71
  set_table_name TARGET_TABLE_NAME
72
72
 
73
- acts_as_cache_machine_for :polymorphics,
74
- :child_cachers,
75
- :has_many_cacheables => :dependent_cache,
76
- :has_many_through_cacheables => :dependent_cache,
77
- :has_and_belongs_to_many_cacheables => :dependent_cache
73
+ cache_map :polymorphics,
74
+ :child_cachers,
75
+ :has_many_cacheables => :dependent_cache,
76
+ :has_many_through_cacheables => :dependent_cache,
77
+ :has_and_belongs_to_many_cacheables => :dependent_cache
78
78
 
79
79
  define_timestamp(:dynamic_timestamp) { execute_timestamp }
80
80
  define_timestamp(:static_timestamp)
@@ -125,7 +125,19 @@ describe CacheMachine do
125
125
  end
126
126
  end
127
127
 
128
- describe "deletes cache" do
128
+ context "deletes cache" do
129
+
130
+ describe "#delete_all_caches" do
131
+ it "removes all caches using map" do
132
+ subject.fetch_cache_of(:polymorphics) { 'cache' }
133
+ subject.fetch_cache_of(:dependent_cache) { 'cache' }
134
+
135
+ subject.delete_all_caches
136
+
137
+ subject.fetch_cache_of(:polymorphics) { 'new cache' }.should == 'new cache'
138
+ subject.fetch_cache_of(:dependent_cache) { 'new cache' }.should == 'new cache'
139
+ end
140
+ end
129
141
 
130
142
  context "of polymorphic associations" do
131
143
  it "works" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache-machine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 8
10
- version: 0.1.8
9
+ - 9
10
+ version: 0.1.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergei Zinin
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-11 00:00:00 Z
19
+ date: 2011-10-13 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: rails