Module: FindCache::Cacheable::ClassMethods

Defined in:
lib/find_cache/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#find_all_cache(ids, order_by = "id DESC") ⇒ Object

To find all Active Record objects using id list

Parameters

  • ids - List of ids

  • order_by - optional order direction for results

Notes

# Only valid if the Rails.cache store supports read_multi

Examples

# Find users with ids ([21, 1, 2, 19, 43]) from User model
UserDetail.find_all_cache([21, 1, 2, 19, 43])
# Find users with ids ([21, 1, 2, 19, 43]) and id ASC
UserDetail.find_all_cache(([21, 1, 2, 19, 43]), "id ASC")


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/find_cache/cacheable.rb', line 108

def find_all_cache(ids, order_by = "id DESC")
  all_new_cache = []
  not_in_cache  = []
  cache_ids = ids.map { |id| KeyGen.cache_key(name, id) }
  all_cache = Rails.cache.read_multi(cache_ids)
  cache_ids.each_with_index do |cache_id, cache_id_index|
    if all_cache[cache_id]
      all_new_cache << all_cache[cache_id]
    else
      not_in_cache << ids[cache_id_index]
    end
  end
  if not_in_cache.length > 0
    items = order(order_by).find_all_by_id(not_in_cache)
    items.each do |item|
      Rails.cache.write(KeyGen.cache_key(name, item.id), item)
    end
    all_new_cache += items
  end
  all_new_cache.each do |cached_item|
    $find_cache_store[KeyGen.global_cache_key][KeyGen.cache_key(name, cached_item.id)] = cached_item
  end
end

#find_cache(id) ⇒ Object

To find an Active Record object using find_by_id method and caching it using Rails.cache methods

Parameters

  • id - Id for the model

Examples

# Find a User object with id 1 from User model
User.find_cache(1)


56
57
58
59
60
61
62
63
# File 'lib/find_cache/cacheable.rb', line 56

def find_cache(id)
  key = KeyGen.cache_key(name, id)
  $find_cache_store[KeyGen.global_cache_key][key] ||= (
    Rails.cache.fetch(key) do
      find_by_id(id)
    end
  )
end

#find_cache_belongs_to(attribute, model, foreign_key) ⇒ Object

belongs_to relational caching with find_cache

Parameters

  • attribute - The attribute that you wish to name for cache

  • model - The Model which data will be retreived for caching

  • foreign_key - The FK attribute name of given Model for the current Model

Examples

# In UserDetail model with user_id FroeignKey of User model
find_cache_belongs_to :user, User, :user_id


39
40
41
42
43
# File 'lib/find_cache/cacheable.rb', line 39

def find_cache_belongs_to(attribute, model, foreign_key)
  send :define_method, attribute.to_sym do |*args|
    model.find_cache(self.send(foreign_key.to_sym))
  end
end

#find_cache_by_ref(ref_attr, ref_val) ⇒ Object

To find an Active Record object using attributes other than primary key (:id) and caching it using Rails.cache methods

Parameters

  • ref_attr - Reference attribute which will be looked in table

  • ref_value - Value for the given referenced attribute

Examples

# Find an UserDetail object using user_id attribute with value 1
UserDetail.find_cache_by_ref(:user_id, 1)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/find_cache/cacheable.rb', line 77

def find_cache_by_ref(ref_attr, ref_val)
  key_ref = KeyGen.cache_key_ref(name, ref_attr, ref_val)
  if (id = Rails.cache.read(key_ref))
    find_cache(id)
  else
    item = send("find_by_#{ref_attr}", "#{ref_val}")
    if item
      Rails.cache.write(key_ref, item.id)
      Rails.cache.write(KeyGen.cache_key(name, item.id), item)
    end
    item
  end
end

#find_cache_has_one(attribute, model, foreign_key) ⇒ Object

has_one relational caching with find_cache

Parameters

  • attribute - The attribute that you wish to name for cache

  • model - The Model which data will be retreived for caching

  • foreign_key - The FK attribute name in the given Model

Examples

# For User model with user_id FroeignKey in UserDetail model
find_cache_has_one :user_detail, UserDetail, :user_id


21
22
23
24
25
# File 'lib/find_cache/cacheable.rb', line 21

def find_cache_has_one(attribute, model, foreign_key)
  send :define_method, attribute.to_sym do |*args|
    model.find_cache_by_ref(foreign_key, self.id)
  end
end