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



19
20
21
22
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 19

def fetch_by_uniq_key(value, uniq_key_name)
  # puts "[Deprecated] will remove in the future, use fetch_by_uniq_keys method instead."
  fetch_by_uniq_keys(uniq_key_name => value)
end

#fetch_by_uniq_key!(value, uniq_key_name) ⇒ Object



24
25
26
27
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 24

def fetch_by_uniq_key!(value, uniq_key_name)
  # puts "[Deprecated] will remove in the future, use fetch_by_uniq_keys! method instead."
  fetch_by_uniq_key(value, uniq_key_name) || raise(::ActiveRecord::RecordNotFound)
end

#fetch_by_uniq_keys(where_values) ⇒ 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_keys(where_values)
  cache_key = cache_uniq_key(where_values)
  if _id = SecondLevelCache.cache_store.read(cache_key)
    self.find(_id) rescue nil
  else
    record = self.where(where_values).first
    record.tap{|record| SecondLevelCache.cache_store.write(cache_key, record.id)} if record
  end
end

#fetch_by_uniq_keys!(where_values) ⇒ Object



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

def fetch_by_uniq_keys!(where_values)
  fetch_by_uniq_keys(where_values) || raise(::ActiveRecord::RecordNotFound)
end