module RecordCache::ClassMethods

Public Instance Methods

add_cached_index(index) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/record_cache.rb, line 161
def cached_indexes
  @cached_indexes ||= {}
end
delete_all_with_invalidate(conditions = nil) click to toggle source
# 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() { |cached_index(index_name)| ... } click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/record_cache.rb, line 118
def id_column
  columns_hash[primary_key]
end
id_field() click to toggle source
# File lib/record_cache.rb, line 114
def id_field
  connection.quote_column_name(primary_key)
end
invalidate_from_conditions(conditions, flag = nil) { |nil| ... } click to toggle source
# 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() click to toggle source
# File lib/record_cache.rb, line 203
def record_cache_class
  self
end
record_cache_config(opts = nil) click to toggle source
# 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) click to toggle source
# 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