Module: CacheBack::ReadMixin

Defined in:
lib/cache_back/read_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(model_class) ⇒ Object



6
7
8
9
10
11
# File 'lib/cache_back/read_mixin.rb', line 6

def self.extended(model_class)
  class << model_class
    alias_method_chain :find_some, :cache_back unless methods.include?('find_some_without_cache_back')
    alias_method_chain :find_every, :cache_back unless methods.include?('find_every_without_cache_back')
  end
end

Instance Method Details

#find_every_with_cache_back(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cache_back/read_mixin.rb', line 21

def find_every_with_cache_back(options)
  attribute_value_pairs = cache_back_conditions_parser.attribute_value_pairs(options) if cache_safe?(options)

  limit = (options[:limit] || (scope(:find) || {})[:limit])

  if (limit.nil? || limit == 1) && attribute_value_pairs && has_cache_back_index_on?(attribute_value_pairs.map(&:first))
    collection_key = cache_back_key_for(attribute_value_pairs)
    collection_key << '/first' if limit == 1
    unless records = CacheBack.cache.read(collection_key)
      records = without_cache_back do
        find_every_without_cache_back(options)
      end

      if records.size == 1
        CacheBack.cache.write(collection_key, records[0])
      elsif records.size <= CacheBack::CONFIGURATION[:max_collection_size]
        records.each { |record| record.store_in_cache_back if record }
        CacheBack.cache.write(collection_key, records.map(&:cache_back_key))
      end
    end

    Array(records)
  else
    without_cache_back do
      find_every_without_cache_back(options)
    end
  end
end

#find_some_with_cache_back(ids, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cache_back/read_mixin.rb', line 50

def find_some_with_cache_back(ids, options)
  attribute_value_pairs = cache_back_conditions_parser.attribute_value_pairs(options) if cache_safe?(options)
  attribute_value_pairs << [:id, ids] if attribute_value_pairs

  limit = (options[:limit] || (scope(:find) || {})[:limit])

  if limit.nil? && attribute_value_pairs && has_cache_back_index_on?(attribute_value_pairs.map(&:first))
    id_to_key_map = Hash[ids.uniq.map { |id| [id, cache_back_key_for_id(id)] }]
    cached_record_map = CacheBack.cache.get_multi(id_to_key_map.values)

    missing_keys = Hash[cached_record_map.select { |key, record| record.nil? }].keys

    return cached_record_map.values if missing_keys.empty?

    missing_ids = Hash[id_to_key_map.invert.select { |key, id| missing_keys.include?(key) }].values

    db_records = without_cache_back do
      find_some_without_cache_back(missing_ids, options)
    end
    db_records.each { |record| record.store_in_cache_back if record }

    (cached_record_map.values + db_records).compact.uniq.sort { |x, y| x.id <=> y.id}
  else
    without_cache_back do
      find_some_without_cache_back(ids, options)
    end
  end
end

#without_cache_back(&block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/cache_back/read_mixin.rb', line 13

def without_cache_back(&block)
  old_value = @use_cache_back || true
  @use_cache_back = false
  yield
ensure
  @use_cache_back = old_value
end