Module: FindCache::Cacheable::ClassMethods

Defined in:
lib/find_cache/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#expire_find_cache(pk_val) ⇒ Object

Expire cache for given name and pk val

Parameters

  • pk_val - pk val



140
141
142
143
144
# File 'lib/find_cache/cacheable.rb', line 140

def expire_find_cache(pk_val)
  key = KeyGen.cache_key(name, pk_val)
  Rails.cache.delete(key)
  $find_cache_store[KeyGen.global_cache_key].delete(key)
end

#find_all_cache(ids, order_by = "#{primary_key} 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")


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

def find_all_cache(ids, order_by = "#{primary_key} 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).where(primary_key => not_in_cache)
    items.each do |item|
      Rails.cache.write(KeyGen.cache_key(name, item.send(primary_key.to_sym)), 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.send(primary_key.to_sym))] = cached_item
  end
  order_attr, order_direction = order_by.split(' ')
  ordered_cache = all_new_cache.sort_by { |item| item.send(order_attr) }
  order_direction.eql?('DESC') ? ordered_cache.reverse : ordered_cache 
end

#find_cache(id) ⇒ Object

To find an Active Record object using PK (:id) based where query 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
      where(primary_key => id).limit(1).first
    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 ForeignKey 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 = where(ref_attr => ref_val).limit(1).first
    if item
      Rails.cache.write(key_ref, item.send(primary_key.to_sym))
      Rails.cache.write(KeyGen.cache_key(name, item.send(primary_key.to_sym)), 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 ForeignKey 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.send(self.class.primary_key.to_sym))
  end
end