Module: Redrecord::Model
- Defined in:
- lib/redrecord.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_to_cache! ⇒ Object
- #attribs_with_cached_fields ⇒ Object
- #cached_fields ⇒ Object
- #cached_method(method_name) ⇒ Object
- #invalidations_for_redrecord_update_queue ⇒ Object
- #redrecord_cached_attrib_hash ⇒ Object
- #redrecord_key ⇒ Object
- #redrecord_redis_cache ⇒ Object
- #redrecord_update_queue_commit ⇒ Object
- #redrecord_update_queue_destroy ⇒ Object
- #redrecord_update_queue_rollback ⇒ Object
- #redrecord_update_queue_save ⇒ Object
- #remove_from_cache! ⇒ Object
Class Method Details
.included(mod) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/redrecord.rb', line 62 def self.included(mod) mod.extend(ClassMethods) mod.send(:after_save, :redrecord_update_queue_save) mod.send(:after_destroy, :redrecord_update_queue_destroy) mod.send(:after_commit, :redrecord_update_queue_commit) mod.send(:after_rollback, :redrecord_update_queue_rollback) end |
Instance Method Details
#add_to_cache! ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/redrecord.rb', line 114 def add_to_cache! Redrecord.redis_op(:hmset, redrecord_key, *(self.class.redrecord_cached_fields.map {|f| aliased_target, punctuation = f.to_s.sub(/([?!=])$/, ''), $1 val = send("#{aliased_target}_without_cache#{punctuation}") [f.to_s, String===val && !Redrecord.is_marshalled?(val) ? val : Marshal.dump(val)] }.flatten) ) end |
#attribs_with_cached_fields ⇒ Object
143 144 145 |
# File 'lib/redrecord.rb', line 143 def attribs_with_cached_fields attributes.merge(cached_fields) end |
#cached_fields ⇒ Object
147 148 149 |
# File 'lib/redrecord.rb', line 147 def cached_fields self.class.redrecord_cached_fields.inject({}) {|hsh,field| hsh[field] = send(field) ; hsh } end |
#cached_method(method_name) ⇒ Object
124 125 126 |
# File 'lib/redrecord.rb', line 124 def cached_method(method_name) redrecord_cached_attrib_hash[method_name.to_sym] end |
#invalidations_for_redrecord_update_queue ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/redrecord.rb', line 75 def invalidations_for_redrecord_update_queue self.class.redrecord_invalidation_fields.each do |f| if((field_value = send(f)).kind_of?(Array)) field_value.each {|item| Redrecord.update_queue << [:save, item] } else Redrecord.update_queue << [:save, field_value] if field_value end end end |
#redrecord_cached_attrib_hash ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/redrecord.rb', line 132 def redrecord_cached_attrib_hash @redrecord_cached_attrib_hash ||= Hash.new do |h,k| h[k.to_sym] = if(cached = (redrecord_redis_cache && redrecord_redis_cache[k.to_s] unless new_record?)) Redrecord.is_marshalled?(cached) ? Marshal.load(cached) : cached else aliased_target, punctuation = k.to_s.sub(/([?!=])$/, ''), $1 send("#{aliased_target}_without_cache#{punctuation}") end end end |
#redrecord_key ⇒ Object
106 107 108 |
# File 'lib/redrecord.rb', line 106 def redrecord_key "#{self.class.table_name}:#{self.id}" end |
#redrecord_redis_cache ⇒ Object
128 129 130 |
# File 'lib/redrecord.rb', line 128 def redrecord_redis_cache @redrecord_redis_cache ||= Redrecord.redis_op(:hgetall, redrecord_key) unless Redrecord.write_only end |
#redrecord_update_queue_commit ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/redrecord.rb', line 94 def redrecord_update_queue_commit Redrecord.update_queue.each do |command, record| if command == :destroy record.remove_from_cache! elsif command == :save record.add_to_cache! # possible todo: cascade invalidation (but avoid loops) end end Redrecord.update_queue.clear end |
#redrecord_update_queue_destroy ⇒ Object
85 86 87 88 |
# File 'lib/redrecord.rb', line 85 def redrecord_update_queue_destroy Redrecord.update_queue << [:destroy, self] unless self.class.redrecord_cached_fields.empty? invalidations_for_redrecord_update_queue end |
#redrecord_update_queue_rollback ⇒ Object
90 91 92 |
# File 'lib/redrecord.rb', line 90 def redrecord_update_queue_rollback Redrecord.update_queue.clear end |
#redrecord_update_queue_save ⇒ Object
70 71 72 73 |
# File 'lib/redrecord.rb', line 70 def redrecord_update_queue_save Redrecord.update_queue << [:save, self] unless self.class.redrecord_cached_fields.empty? invalidations_for_redrecord_update_queue end |