Module: SecondLevelCache::ActiveRecord::FetchByUniqKey

Defined in:
lib/second_level_cache/active_record/fetch_by_uniq_key.rb

Instance Method Summary collapse

Instance Method Details

#fetch_by_uniq_key(value, uniq_key_name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 5

def fetch_by_uniq_key(value, uniq_key_name)
  return self.where(uniq_key_name => value).first unless self.second_level_cache_enabled?
  if _id = SecondLevelCache.cache_store.read(cache_uniq_key(value, uniq_key_name))
    self.find(_id) rescue nil
  else
    record = self.where(uniq_key_name => value).first
    record.tap{|record| SecondLevelCache.cache_store.write(cache_uniq_key(value, uniq_key_name), record.id)} if record
  end
end

#fetch_by_uniq_key!(value, uniq_key_name) ⇒ Object



15
16
17
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 15

def fetch_by_uniq_key!(value, uniq_key_name)
  fetch_by_uniq_key(value, uniq_key_name) || raise(::ActiveRecord::RecordNotFound)
end