Sha256: 072acd75a5025b3998d6b722f78ab9b6b8bbd09311e505af62cdcaec3397c14a
Contents?: true
Size: 1.25 KB
Versions: 6
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true module Apartment module Model extend ActiveSupport::Concern module ClassMethods # NOTE: key can either be an array of symbols or a single value. # E.g. If we run the following query: # `Setting.find_by(key: 'something', value: 'amazing')` key will have an array of symbols: `[:key, :something]` # while if we run: # `Setting.find(10)` key will have the value 'id' def cached_find_by_statement(key, &block) # Modifying the cache key to have a reference to the current tenant, # so the cached statement is referring only to the tenant in which we've # executed this cache_key = if key.is_a? String "#{Apartment::Tenant.current}_#{key}" else # NOTE: In Rails 6.0.4 we start receiving an ActiveRecord::Reflection::BelongsToReflection # as the key, which wouldn't work well with an array. [Apartment::Tenant.current] + Array.wrap(key) end cache = @find_by_statement_cache[connection.prepared_statements] cache.compute_if_absent(cache_key) { ActiveRecord::StatementCache.create(connection, &block) } end end end end
Version data entries
6 entries across 6 versions & 3 rubygems