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



27
28
29
30
31
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 27

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



33
34
35
36
37
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 33

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



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

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
      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



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

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