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



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

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



38
39
40
41
42
# File 'lib/second_level_cache/active_record/fetch_by_uniq_key.rb', line 38

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
24
25
26
# 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
    record = begin
      find(obj_id)
    rescue
      nil
    end
  end
  return record if record_attributes_equal_where_values?(record, where_values)
  record = where(where_values).first
  if record
    SecondLevelCache.cache_store.write(cache_key, record.id)
    record
  else
    SecondLevelCache.cache_store.delete(cache_key)
    nil
  end
end

#fetch_by_uniq_keys!(where_values) ⇒ Object



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

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