Class: ArCache::Record

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ar_cache/record.rb

Overview

rubocop:disable Rails/ApplicationRecord

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(table_name) ⇒ Object



7
8
9
# File 'lib/ar_cache/record.rb', line 7

def self.get(table_name)
  find_by(table_name: table_name)
end

.store(table) ⇒ Object



23
24
25
26
27
# File 'lib/ar_cache/record.rb', line 23

def self.store(table)
  record = get(table.name) || new(table_name: table.name)
  record.store(table)
  record
end

.update_version(table) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ar_cache/record.rb', line 15

def self.update_version(table)
  record = get(table.name)
  return store(table).version unless record

  record.update_version
  record.version
end

.version(table) ⇒ Object



11
12
13
# File 'lib/ar_cache/record.rb', line 11

def self.version(table)
  (get(table.name) || store(table)).version
end

Instance Method Details

#store(table) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ar_cache/record.rb', line 29

def store(table)
  with_optimistic_retry do
    self.version += 1 unless table_md5 == table.md5
    self.table_md5 = table.md5

    save! if changed?
  end
end

#update_versionObject



38
39
40
41
42
43
# File 'lib/ar_cache/record.rb', line 38

def update_version
  with_optimistic_retry do
    self.version += 1
    save!
  end
end