Module: ArCache::Marshal

Included in:
Table
Defined in:
lib/ar_cache/marshal.rb

Instance Method Summary collapse

Instance Method Details

#delete(*ids) ⇒ Object



8
9
10
11
12
# File 'lib/ar_cache/marshal.rb', line 8

def delete(*ids)
  return -1 if disabled?

  ArCache.delete_multi(ids.map { |id| primary_cache_key(id) })
end

#read(where_clause, select_values = nil, &block) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ar_cache/marshal.rb', line 36

def read(where_clause, select_values = nil, &block) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
  entries_hash = ArCache.read_multi(*where_clause.cache_hash.keys, raw: true)
  where_clause.cache_hash.each_key do |k|
    v = entries_hash[k]

    case v
    when nil
      where_clause.add_missed_values(k)
    when ArCache::PLACEHOLDER
      where_clause.add_missed_values(k)
      where_clause.add_blank_primary_cache_key(k)
      entries_hash.delete(k)
    else
      entries_hash[k] = load_attributes(v)
    end
  end

  records = []

  entries_hash.each do |k, entry|
    wrong_key = detect_wrong_column(entry, where_clause.to_h)

    if wrong_key
      where_clause.add_missed_values(k)
      where_clause.add_invalid_second_cache_key(k) if column_indexes.include?(wrong_key)
    else
      entry = entry.slice(*select_values) if select_values
      records << instantiate(where_clause.klass, entry, &block)
    end
  end

  where_clause.delete_invalid_keys
  records
end

#write(records) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ar_cache/marshal.rb', line 14

def write(records) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  return -1 if disabled?

  records.each do |attributes|
    key = primary_cache_key(attributes[primary_key])
    stringify_attributes = dump_attributes(attributes)
    bool = ArCache.write(key, stringify_attributes, unless_exist: cache_lock?, raw: true, expires_in: expires_in)
    if cache_lock? && !bool
      value = ArCache.read(key, raw: true)
      next if value == ArCache::PLACEHOLDER
      next ArCache.lock_key(key) if value != stringify_attributes
    end

    unique_indexes.each_with_index do |index, i|
      # The first index is primary key, should skip it.
      ArCache.write(cache_key(attributes, index), key, raw: true, expires_in: expires_in) unless i.zero?
    end
  end
rescue Encoding::UndefinedConversionError
  0
end