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



29
30
31
32
33
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 29

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



35
36
37
38
39
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 35

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 6

def fetch_by_uniq_keys(where_values)
  cache_key = cache_uniq_key(where_values)
  obj_id = SecondLevelCache.cache_store.read(cache_key)
  if obj_id
    begin
      return find(obj_id)
    rescue StandardError
      return nil
    end
  end

  record = where(where_values).first
  return nil unless record

  record.tap do |r|
    SecondLevelCache.cache_store.write(cache_key, r.id)
  end
end

#fetch_by_uniq_keys!(where_values) ⇒ Object



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

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