Module: RecordCache::ClassMethods
- Defined in:
- lib/record_cache.rb
Instance Method Summary collapse
- #add_cached_index(index) ⇒ Object
- #cached_index(name) ⇒ Object
- #cached_index_names ⇒ Object
- #cached_indexes ⇒ Object
- #delete_all_with_invalidate(conditions = nil) ⇒ Object
- #each_cached_index ⇒ Object
- #find_with_caching(*args, &block) ⇒ Object
- #id_column ⇒ Object
- #id_field ⇒ Object
- #invalidate_from_conditions(conditions, flag = nil) ⇒ Object
- #record_cache_class ⇒ Object
- #record_cache_config(opts = nil) ⇒ Object
- #update_all_with_invalidate(updates, conditions = nil) ⇒ Object
Instance Method Details
#add_cached_index(index) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/record_cache.rb', line 172 def add_cached_index(index) name = index.name count = nil # Make sure the key is unique. while cached_indexes["#{name}#{count}"] count ||= 0 count += 1 end cached_indexes["#{name}#{count}"] = index end |
#cached_index(name) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/record_cache.rb', line 165 def cached_index(name) name = name.to_s index = cached_indexes[name] index ||= base_class.cached_index(name) if base_class != self and base_class.respond_to?(:cached_index) index end |
#cached_index_names ⇒ Object
189 190 191 192 193 |
# File 'lib/record_cache.rb', line 189 def cached_index_names names = cached_indexes.keys names.concat(base_class.cached_index_names) if base_class != self and base_class.respond_to?(:cached_index_names) names.uniq end |
#cached_indexes ⇒ Object
161 162 163 |
# File 'lib/record_cache.rb', line 161 def cached_indexes @cached_indexes ||= {} end |
#delete_all_with_invalidate(conditions = nil) ⇒ Object
108 109 110 111 112 |
# File 'lib/record_cache.rb', line 108 def delete_all_with_invalidate(conditions = nil) invalidate_from_conditions(conditions) do |conditions| delete_all_without_invalidate(conditions) end end |
#each_cached_index ⇒ Object
183 184 185 186 187 |
# File 'lib/record_cache.rb', line 183 def each_cached_index cached_index_names.each do |index_name| yield cached_index(index_name) end end |
#find_with_caching(*args, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/record_cache.rb', line 65 def find_with_caching(*args, &block) if args.last.is_a?(Hash) args.last.delete_if {|k,v| v.nil?} args.pop if args.last.empty? end if [:all, :first, :last].include?(args.first) opts = args.last if opts.is_a?(Hash) and opts.keys == [:conditions] # Try to match the SQL. if opts[:conditions].kind_of?(Hash) field = nil value = nil if opts[:conditions].keys.size == 1 opts[:conditions].each {|f,v| field, value = f,v} end elsif opts[:conditions] =~ /^(?:"?#{table_name}"?\.)?"?(\w+)"? = (?:(\d+)|'(\w+)')$/i field, value = $1, ($3 || $2) elsif opts[:conditions] =~ /^(?:"?#{table_name}"?\.)?"?(\w+)"? IN \(([\d,]*)\)$/i field, value = $1, $2 value = value.split(',') end if field and value index = cached_index("by_#{field}") return index.find_by_field([value].flatten, self, args.first) if index end end elsif not args.last.is_a?(Hash) # This is a find with just ids. index = cached_index('by_id') return index.find_by_ids(args, self) if index end find_without_caching(*args, &block) end |
#id_column ⇒ Object
118 119 120 |
# File 'lib/record_cache.rb', line 118 def id_column columns_hash[primary_key] end |
#id_field ⇒ Object
114 115 116 |
# File 'lib/record_cache.rb', line 114 def id_field connection.quote_column_name(primary_key) end |
#invalidate_from_conditions(conditions, flag = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/record_cache.rb', line 122 def invalidate_from_conditions(conditions, flag = nil) if conditions.nil? # Just invalidate all indexes. result = yield(nil) self.increment_version return result end # Freeze ids to avoid race conditions. sql = "SELECT #{id_field} FROM #{table_name} " self.send(:add_conditions!, sql, conditions, self.send(:scope, :find)) ids = RecordCache.db(self).select_values(sql) return if ids.empty? conditions = "#{id_field} IN (#{ids.collect {|id| quote_value(id, id_column)}.join(',')})" if block_given? # Capture the ids to invalidate in lambdas. lambdas = [] each_cached_index do |index| lambdas << index.invalidate_from_conditions_lambda(conditions) end result = yield(conditions) # Finish invalidating with prior attributes. lambdas.each {|l| l.call} end # Invalidate again afterwards if we are updating (or for the first time if no block was given). if flag == :update or not block_given? each_cached_index do |index| index.invalidate_from_conditions(conditions) end end result end |
#record_cache_class ⇒ Object
203 204 205 |
# File 'lib/record_cache.rb', line 203 def record_cache_class self end |
#record_cache_config(opts = nil) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/record_cache.rb', line 195 def record_cache_config(opts = nil) if opts record_cache_config.merge!(opts) else @record_cache_config ||= RecordCache.config.clone end end |
#update_all_with_invalidate(updates, conditions = nil) ⇒ Object
102 103 104 105 106 |
# File 'lib/record_cache.rb', line 102 def update_all_with_invalidate(updates, conditions = nil) invalidate_from_conditions(conditions, :update) do |conditions| update_all_without_invalidate(updates, conditions) end end |