Class: SolidCache::Record

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/solid_cache/record.rb

Direct Known Subclasses

Entry

Constant Summary collapse

NULL_INSTRUMENTER =
ActiveSupport::Notifications::Instrumenter.new(ActiveSupport::Notifications::Fanout.new)

Class Method Summary collapse

Class Method Details

.disable_instrumentation(&block) ⇒ Object



12
13
14
# File 'app/models/solid_cache/record.rb', line 12

def disable_instrumentation(&block)
  with_instrumenter(NULL_INSTRUMENTER, &block)
end

.each_shard(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/solid_cache/record.rb', line 39

def each_shard(&block)
  return to_enum(:each_shard) unless block_given?

  if SolidCache.configuration.sharded?
    SolidCache.configuration.shard_keys.each do |shard|
      Record.with_shard(shard, &block)
    end
  else
    yield
  end
end

.with_instrumenter(instrumenter, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/solid_cache/record.rb', line 16

def with_instrumenter(instrumenter, &block)
  with_connection do |connection|
    if connection.respond_to?(:with_instrumenter)
      connection.with_instrumenter(instrumenter, &block)
    else
      begin
        old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter
        block.call
      ensure
        ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter
      end
    end
  end
end

.with_shard(shard, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'app/models/solid_cache/record.rb', line 31

def with_shard(shard, &block)
  if shard && SolidCache.configuration.sharded?
    connected_to(shard: shard, role: default_role, prevent_writes: false, &block)
  else
    block.call
  end
end