Module: ActiveRecordCached

Extended by:
ActiveRecordCached
Includes:
BaseExtension, MassOperationWrapper
Included in:
ActiveRecordCached
Defined in:
lib/activerecord_cached/activerecord_extensions.rb,
lib/activerecord_cached.rb,
lib/activerecord_cached/cache.rb,
lib/activerecord_cached/railtie.rb,
lib/activerecord_cached/version.rb,
lib/activerecord_cached/redis_mutex.rb,
lib/activerecord_cached/limit_checks.rb,
lib/activerecord_cached/configuration.rb

Overview

define cached_* methods, e.g. cached_pluck

Defined Under Namespace

Modules: BaseExtension, MassOperationWrapper, RelationExtension Classes: Railtie, RedisMutex

Constant Summary collapse

VERSION =
"1.1.0"

Instance Method Summary collapse

Methods included from BaseExtension

#clear_cached_values

Instance Method Details

#cache_storeObject



6
7
8
9
10
# File 'lib/activerecord_cached/configuration.rb', line 6

def cache_store
  @cache_store ||= store_with_limit_warning(
    ActiveSupport::Cache::MemoryStore.new({ coder: nil, size: max_total_bytes })
  )
end

#cache_store=(val) ⇒ Object



12
13
14
15
16
17
# File 'lib/activerecord_cached/configuration.rb', line 12

def cache_store=(val)
  val.is_a?(ActiveSupport::Cache::MemoryStore) ||
    val.is_a?(ActiveSupport::Cache::RedisCacheStore) ||
    raise(ArgumentError, 'pass a MemoryStore or RedisCacheStore')
  @cache_store = store_with_limit_warning(val)
end

#clear_allObject



24
25
26
27
28
29
30
# File 'lib/activerecord_cached/cache.rb', line 24

def clear_all
  synchronize do
    all_keys = fetch_cache_keys_per_model.values.flat_map(&:keys)
    write_cache_keys_per_model({})
    cache_store.delete_multi(all_keys)
  end
end

#clear_for_model(model) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/activerecord_cached/cache.rb', line 12

def clear_for_model(model)
  synchronize do
    hash = fetch_cache_keys_per_model
    return unless model_keys = hash.delete(model)&.keys

    # delete from the key list first so that if a fetch comes in right after,
    # it can write the key to the list again
    write_cache_keys_per_model(hash)
    cache_store.delete_multi(model_keys)
  end
end

#configure(&block) ⇒ Object



2
3
4
# File 'lib/activerecord_cached/configuration.rb', line 2

def configure(&block)
  tap(&block)
end

#fetch(relation, method, args) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/activerecord_cached/cache.rb', line 2

def fetch(relation, method, args)
  key = ['ActiveRecordCached', relation.to_sql, method, args.sort].join(':')
  cache_store.fetch(key, expires_in: 1.day + rand(300).seconds, race_condition_ttl: 10.seconds) do
    # The gem keeps track of all cache keys used for each model for easy clearing.
    # Using #delete_matched would be too slow on large redis instances.
    log_model_cache_key(relation.klass, key)
    query_db(relation, method, args)
  end
end

#max_bytes=(val) ⇒ Object



32
33
34
35
# File 'lib/activerecord_cached/configuration.rb', line 32

def max_bytes=(val)
  val.is_a?(Integer) && val > 0 || val.nil? || raise(ArgumentError, 'pass an int > 0 or nil')
  super
end

#max_count=(val) ⇒ Object



26
27
28
29
# File 'lib/activerecord_cached/configuration.rb', line 26

def max_count=(val)
  val.is_a?(Integer) && val > 0 || val.nil? || raise(ArgumentError, 'pass an int > 0 or nil')
  super
end

#max_total_bytes=(val) ⇒ Object



20
21
22
23
# File 'lib/activerecord_cached/configuration.rb', line 20

def max_total_bytes=(val)
  val.is_a?(Integer) && val > 0 || raise(ArgumentError, 'pass an int > 0')
  super
end

#on_limit_reached=(val) ⇒ Object



38
39
40
41
# File 'lib/activerecord_cached/configuration.rb', line 38

def on_limit_reached=(val)
  val.is_a?(Proc) && val.arity == 1 || raise(ArgumentError, 'pass a proc with arity 1')
  super
end