Skip to content

Commit

Permalink
Allow AR instrumentation to be disabled
Browse files Browse the repository at this point in the history
We want the cache queries to be fast and instrumentation can slow things
down. Additionally you might not want the SolidCache queries showing up
in your logs.

Instrumentation is enabled by default but can we disabled with:

```ruby
  config.cache_store = [ :solid_cache_store, active_record_instrumentation: false ]
```
  • Loading branch information
djmb committed Sep 14, 2023
1 parent 56915b9 commit 3388d45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Solid cache supports these options in addition to the universal `ActiveSupport::
- `max_entries` - the maximum number of entries allowed in the cache (default: `2.weeks.to_i`)
- `cluster` - a Hash of options for the cache database cluster, e.g { shards: [:database1, :database2, :database3] }
- `clusters` - and Array of Hashes for separate cache clusters (ignored if `:cluster` is set)
- `active_record_instrumentation` - whether to instrument the cache's queries (default: `true`)

For more information on cache clusters see [Sharding the cache](#sharding-the-cache)
### Cache trimming
Expand Down
10 changes: 10 additions & 0 deletions app/models/solid_cache/record.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
module SolidCache
class Record < ActiveRecord::Base
NULL_INSTRUMENTER = ActiveSupport::Notifications::Instrumenter.new(ActiveSupport::Notifications::Fanout.new)

self.abstract_class = true

connects_to **SolidCache.connects_to if SolidCache.connects_to

class << self
def disable_instrumentation
connection.with_instrumenter(NULL_INSTRUMENTER) do
yield
end
end
end
end
end

Expand Down
24 changes: 20 additions & 4 deletions lib/solid_cache/cluster/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ def reading_shard(normalized_key:)
with_shard(shard_for_normalized_key(normalized_key)) { yield }
end

def active_record_instrumentation?
@active_record_instrumentation
end

private
attr_reader :consistent_hash

def with_shard(shard)
if shard
Record.connected_to(shard: shard) { yield }
else
yield
disable_active_record_instrumentation_if_required do
if shard
Record.connected_to(shard: shard) { yield }
else
yield
end
end
end

Expand Down Expand Up @@ -131,6 +137,16 @@ def async_if_required
yield
end
end

def disable_active_record_instrumentation_if_required
if active_record_instrumentation?
yield
else
Record.disable_instrumentation do
yield
end
end
end
end
end
end

0 comments on commit 3388d45

Please sign in to comment.