Module: SmartCacheTenant::CacheableRelation

Defined in:
lib/smart_cache_tenant/cacheable_relation.rb

Instance Method Summary collapse

Instance Method Details

#calculate(operation, column_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 24

def calculate(operation, column_name)
  return super unless smart_cache_enabled?

  key = smart_cache_key(operation: "calculate:#{operation}:#{column_name}")
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  cached = Rails.cache.read(key)

  unless cached.nil?
    SmartCacheTenant::Logger.log_cache_hit("#{klass.name} #{operation.to_s.capitalize}", elapsed_ms(started), arel_to_sql)
    return cached
  end

  super.tap do |result|
    Rails.cache.write(key, result, expires_in: SmartCacheTenant.config.ttl)
  end
end

#exists?(conditions = :none) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 41

def exists?(conditions = :none)
  return super unless smart_cache_enabled?

  key = smart_cache_key(operation: "exists:#{conditions.inspect}")
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  cached = Rails.cache.read(key)

  unless cached.nil?
    SmartCacheTenant::Logger.log_cache_hit("#{klass.name} Exists?", elapsed_ms(started), arel_to_sql)
    return cached
  end

  super.tap do |result|
    Rails.cache.write(key, result, expires_in: SmartCacheTenant.config.ttl)
  end
end

#insert_all(attributes, returning: nil, unique_by: nil, record_timestamps: nil) ⇒ Object



64
65
66
67
68
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 64

def insert_all(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
  result = super
  bump_smart_cache_for_bulk_write!(tenant_ids: tenant_ids_from_bulk_attributes(attributes)) if attributes.present?
  result
end

#load(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 5

def load(&block)
  return super unless smart_cache_enabled?

  key = smart_cache_key(operation: :load)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  cached = Rails.cache.read(key)

  unless cached.nil?
    @records = cached
    @loaded = true
    SmartCacheTenant::Logger.log_cache_hit("#{klass.name} Load", elapsed_ms(started), arel_to_sql)
    return self
  end

  super.tap do
    Rails.cache.write(key, @records, expires_in: SmartCacheTenant.config.ttl)
  end
end

#update_all(updates) ⇒ Object



58
59
60
61
62
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 58

def update_all(updates)
  result = super
  bump_smart_cache_for_bulk_write!(affected_rows: result)
  result
end

#upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil) ⇒ Object



70
71
72
73
74
# File 'lib/smart_cache_tenant/cacheable_relation.rb', line 70

def upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
  result = super
  bump_smart_cache_for_bulk_write!(tenant_ids: tenant_ids_from_bulk_attributes(attributes)) if attributes.present?
  result
end