Class: SolidCache::Entry

Inherits:
Record
  • Object
show all
Includes:
Expiration, Size
Defined in:
app/models/solid_cache/entry.rb,
app/models/solid_cache/entry/size.rb,
app/models/solid_cache/entry/expiration.rb,
app/models/solid_cache/entry/size/estimate.rb,
app/models/solid_cache/entry/size/moving_average_estimate.rb

Defined Under Namespace

Modules: Expiration, Size

Constant Summary collapse

ESTIMATED_ROW_OVERHEAD =

The estimated cost of an extra row in bytes, including fixed size columns, overhead, indexes and free space Based on expirimentation on SQLite, MySQL and Postgresql. A bit high for SQLite (more like 90 bytes), but about right for MySQL/Postgresql.

140
KEY_HASH_ID_RANGE =
-(2**63)..(2**63 - 1)

Constants inherited from Record

Record::NULL_INSTRUMENTER

Class Method Summary collapse

Methods inherited from Record

disable_instrumentation, each_shard, with_shard

Class Method Details

.clear_deleteObject



46
47
48
# File 'app/models/solid_cache/entry.rb', line 46

def clear_delete
  in_batches.delete_all
end

.clear_truncateObject



42
43
44
# File 'app/models/solid_cache/entry.rb', line 42

def clear_truncate
  connection.truncate(table_name)
end

.decrement(key, amount) ⇒ Object



61
62
63
# File 'app/models/solid_cache/entry.rb', line 61

def decrement(key, amount)
  increment(key, -amount)
end

.delete_by_key(key) ⇒ Object



33
34
35
# File 'app/models/solid_cache/entry.rb', line 33

def delete_by_key(key)
  delete_no_query_cache(:key_hash, key_hash_for(key))
end

.delete_multi(keys) ⇒ Object



37
38
39
40
# File 'app/models/solid_cache/entry.rb', line 37

def delete_multi(keys)
  serialized_keys = keys.map { |key| key_hash_for(key) }
  delete_no_query_cache(:key_hash, serialized_keys)
end

.id_rangeObject



65
66
67
68
69
# File 'app/models/solid_cache/entry.rb', line 65

def id_range
  uncached do
    pick(Arel.sql("max(id) - min(id) + 1")) || 0
  end
end

.increment(key, amount) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'app/models/solid_cache/entry.rb', line 50

def increment(key, amount)
  transaction do
    uncached do
      result = lock.where(key_hash: key_hash_for(key)).pick(:key, :value)
      amount += result[1].to_i if result&.first == key
      write(key, amount)
      amount
    end
  end
end

.read(key) ⇒ Object



22
23
24
25
# File 'app/models/solid_cache/entry.rb', line 22

def read(key)
  result = select_all_no_query_cache(get_sql, key_hash_for(key)).first
  result[1] if result&.first == key
end

.read_multi(keys) ⇒ Object



27
28
29
30
31
# File 'app/models/solid_cache/entry.rb', line 27

def read_multi(keys)
  key_hashes = keys.map { |key| key_hash_for(key) }
  results = select_all_no_query_cache(get_all_sql(key_hashes), key_hashes).to_h
  results.except!(results.keys - keys)
end

.write(key, value) ⇒ Object



14
15
16
# File 'app/models/solid_cache/entry.rb', line 14

def write(key, value)
  upsert_all_no_query_cache([ { key: key, value: value } ])
end

.write_multi(payloads) ⇒ Object



18
19
20
# File 'app/models/solid_cache/entry.rb', line 18

def write_multi(payloads)
  upsert_all_no_query_cache(payloads)
end