Method: ActiveSupport::Cache::Store#initialize
- Defined in:
- lib/active_support/cache.rb
#initialize(options = nil) ⇒ Store
Creates a new cache.
Options
:namespace-
Sets the namespace for the cache. This option is especially useful if your application shares a cache with other applications.
:serializer-
The serializer for cached values. Must respond to
dumpandload.The default serializer depends on the cache format version (set via
config.active_support.cache_format_versionwhen using Rails). The default serializer for each format version includes a fallback mechanism to deserialize values from any format version. This behavior makes it easy to migrate between format versions without invalidating the entire cache.You can also specify
serializer: :message_packto use a preconfigured serializer based on ActiveSupport::MessagePack. The:message_packserializer includes the same deserialization fallback mechanism, allowing easy migration from (or to) the default serializer. The:message_packserializer may improve performance, but it requires themsgpackgem. :compressor-
The compressor for serialized cache values. Must respond to
deflateandinflate.The default compressor is
Zlib. To define a new custom compressor that also decompresses old cache entries, you can check compressed values for Zlib’s"\x78"signature:module MyCompressor def self.deflate(dumped) # compression logic... (make sure result does not start with "\x78"!) end def self.inflate(compressed) if compressed.start_with?("\x78") Zlib.inflate(compressed) else # decompression logic... end end end ActiveSupport::Cache.lookup_store(:redis_cache_store, compressor: MyCompressor) :coder-
The coder for serializing and (optionally) compressing cache entries. Must respond to
dumpandload.The default coder composes the serializer and compressor, and includes some performance optimizations. If you only need to override the serializer or compressor, you should specify the
:serializeror:compressoroptions instead.If the store can handle cache entries directly, you may also specify
coder: nilto omit the serializer, compressor, and coder. For example, if you are using ActiveSupport::Cache::MemoryStore and can guarantee that cache values will not be mutated, you can specifycoder: nilto avoid the overhead of safeguarding against mutation.The
:coderoption is mutually exclusive with the:serializerand:compressoroptions. Specifying them together will raise anArgumentError.
Any other specified options are treated as default options for the relevant cache operations, such as #read, #write, and #fetch.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/active_support/cache.rb', line 300 def initialize( = nil) = ? (()) : {} [:compress] = true unless .key?(:compress) [:compress_threshold] ||= DEFAULT_COMPRESS_LIMIT @max_key_size = .delete(:max_key_size) @max_key_size = MAX_KEY_SIZE if @max_key_size.nil? # allow 'false' as a value @coder = .delete(:coder) do legacy_serializer = Cache.format_version < 7.1 && ![:serializer] serializer = .delete(:serializer) || default_serializer serializer = Cache::SerializerWithFallback[serializer] if serializer.is_a?(Symbol) compressor = .delete(:compressor) { Zlib } Cache::Coder.new(serializer, compressor, legacy_serializer: legacy_serializer) end @coder ||= Cache::SerializerWithFallback[:passthrough] @coder_supports_compression = @coder.respond_to?(:dump_compressed) end |