public
Fork of defunkt/cache_fu
Description: Everyone's favorite memcached plugin for ActiveRecord.
Homepage: http://errtheblog.com
Clone URL: git://github.com/entombedvirus/cache_fu.git
changing before_saves to after_saves (becuase ids are not set until after 
save)
alexcharlie (author)
Tue Jul 08 23:56:55 -0700 2008
commit  27c32e77695f8a9771edf2d05ed1d2110fd82a20
tree    4c3c4472530f7aa5c8a2a98867bbe0cc019c6c74
parent  c365598e5e4cf4fc4d3978aa72c88df54901b10c
...
124
125
126
127
 
128
129
130
...
153
154
155
156
 
157
158
159
160
161
162
163
 
164
165
166
167
168
169
170
171
 
172
173
174
...
178
179
180
181
182
 
183
184
185
...
124
125
126
 
127
128
129
130
...
153
154
155
 
156
157
158
159
160
161
 
 
162
163
164
165
166
167
168
 
 
169
170
171
172
...
176
177
178
 
 
179
180
181
182
0
@@ -124,7 +124,7 @@ module ActsAsCached
0
       pkey_name = reflection.primary_key_name
0
       skey_name = :id
0
       
0
- reflection.klass.before_save do |instance|
0
+ reflection.klass.after_save do |instance|
0
         if instance.changes[pkey_name]
0
           # Need to add id to parent's cache list ONLY if parent had the id list in cache in the first place
0
           key = "#{instance.send(pkey_name)}:#{ids_reflection}"
0
@@ -153,22 +153,20 @@ module ActsAsCached
0
       skey_name = reflection.options[:through] ? reflection.source_reflection.primary_key_name : :id
0
       r = reflection.options[:through] ? reflection.through_reflection : reflection
0
       
0
- r.klass.before_save do |instance|
0
+ r.klass.after_save do |instance|
0
         # if the foreign_key on self was changed
0
         if instance.changes[pkey_name]
0
 
0
           # Need to add id to parent's cache list ONLY if parent had the id list in cache in the first place
0
           key = "#{instance.send(pkey_name)}:#{ids_reflection}"
0
- if reflection.active_record.cached?(key)
0
- assoc_ids = reflection.active_record.fetch_cache(key)
0
+ unless (assoc_ids = reflection.active_record.fetch_cache(key)).nil?
0
             assoc_ids << instance.send(skey_name)
0
             reflection.active_record.set_cache(key, assoc_ids.uniq)
0
           end
0
           
0
           # if we are doing an update of the foreign_key rather than an insert, make sure we remove ourselves from our old parent's cache
0
           key = "#{instance.changes[pkey_name].first}:#{ids_reflection}"
0
- unless instance.changes[pkey_name].first.nil? || !reflection.active_record.cached?(key)
0
- assoc_ids = reflection.active_record.fetch_cache(key)
0
+ unless instance.changes[pkey_name].first.nil? || (assoc_ids = reflection.active_record.fetch_cache(key)).nil?
0
             assoc_ids.delete(instance.send(skey_name))
0
             reflection.active_record.set_cache(key, assoc_ids.uniq)
0
           end
0
@@ -178,8 +176,7 @@ module ActsAsCached
0
       # If an obj is destroyed, update the parent's cached id list
0
       r.klass.after_destroy do |instance|
0
         key = "#{instance.send(pkey_name)}:#{ids_reflection}"
0
- if reflection.active_record.cached?(key)
0
- assoc_ids = reflection.active_record.fetch_cache(key)
0
+ unless (assoc_ids = reflection.active_record.fetch_cache(key)).nil?
0
           assoc_ids.delete(instance.send(skey_name))
0
           reflection.active_record.set_cache(key, assoc_ids.uniq)
0
         end

Comments

    No one has commented yet.