Module: ActiveSupport::Cache

Defined in:
lib/active_support/cache.rb,
lib/active_support/cache/coder.rb,
lib/active_support/cache/entry.rb,
lib/active_support/cache/file_store.rb,
lib/active_support/cache/null_store.rb,
lib/active_support/cache/memory_store.rb,
lib/active_support/cache/mem_cache_store.rb,
lib/active_support/cache/redis_cache_store.rb,
lib/active_support/cache/strategy/local_cache.rb,
lib/active_support/cache/serializer_with_fallback.rb,
lib/active_support/cache/strategy/local_cache_middleware.rb

Overview

See ActiveSupport::Cache::Store for documentation.

Defined Under Namespace

Modules: SerializerWithFallback, Strategy Classes: Coder, Entry, FileStore, MemCacheStore, MemoryStore, NullStore, RedisCacheStore, Store, WriteOptions

Constant Summary collapse

UNIVERSAL_OPTIONS =

These options mean something to all cache implementations. Individual cache implementations may support additional options.

[
  :coder,
  :compress,
  :compress_threshold,
  :compressor,
  :expire_in,
  :expired_in,
  :expires_in,
  :namespace,
  :race_condition_ttl,
  :serializer,
  :skip_nil,
]
OPTION_ALIASES =

Mapping of canonical option names to aliases that a store will recognize.

{
  expires_in: [:expire_in, :expired_in]
}.freeze
DEFAULT_COMPRESS_LIMIT =
1.kilobyte
DeserializationError =

Raised by coders when the cache entry can’t be deserialized. This error is treated as a cache miss.

Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.format_versionObject

Returns the value of attribute format_version.



58
59
60
# File 'lib/active_support/cache.rb', line 58

def format_version
  @format_version
end

Class Method Details

.expand_cache_key(key, namespace = nil) ⇒ Object

Expands out the key argument into a key that can be used for the cache store. Optionally accepts a namespace, and all keys will be scoped within that namespace.

If the key argument provided is an array, or responds to to_a, then each of elements in the array will be turned into parameters/keys and concatenated into a single key. For example:

ActiveSupport::Cache.expand_cache_key([:foo, :bar])               # => "foo/bar"
ActiveSupport::Cache.expand_cache_key([:foo, :bar], "namespace")  # => "namespace/foo/bar"

The key argument can also respond to cache_key or to_param.



117
118
119
120
121
122
123
124
125
126
# File 'lib/active_support/cache.rb', line 117

def expand_cache_key(key, namespace = nil)
  expanded_cache_key = namespace ? +"#{namespace}/" : +""

  if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
    expanded_cache_key << "#{prefix}/"
  end

  expanded_cache_key << retrieve_cache_key(key)
  expanded_cache_key
end

.lookup_store(store = nil, *parameters) ⇒ Object

Creates a new Store object according to the given options.

If no arguments are passed to this method, then a new ActiveSupport::Cache::MemoryStore object will be returned.

If you pass a Symbol as the first argument, then a corresponding cache store class under the ActiveSupport::Cache namespace will be created. For example:

ActiveSupport::Cache.lookup_store(:memory_store)
# => returns a new ActiveSupport::Cache::MemoryStore object

ActiveSupport::Cache.lookup_store(:mem_cache_store)
# => returns a new ActiveSupport::Cache::MemCacheStore object

Any additional arguments will be passed to the corresponding cache store class’s constructor:

ActiveSupport::Cache.lookup_store(:file_store, '/tmp/cache')
# => same as: ActiveSupport::Cache::FileStore.new('/tmp/cache')

If the first argument is not a Symbol, then it will simply be returned:

ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
# => returns MyOwnCacheStore.new


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_support/cache.rb', line 85

def lookup_store(store = nil, *parameters)
  case store
  when Symbol
    options = parameters.extract_options!
    # clean this up once Ruby 2.7 support is dropped
    # see https://github.com/rails/rails/pull/41522#discussion_r581186602
    if options.empty?
      retrieve_store_class(store).new(*parameters)
    else
      retrieve_store_class(store).new(*parameters, **options)
    end
  when Array
    lookup_store(*store)
  when nil
    ActiveSupport::Cache::MemoryStore.new
  else
    store
  end
end