new(options = nil) public

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 dump and load.

The default serializer depends on the cache format version (set via config.active_support.cache_format_version when 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_pack to use a preconfigured serializer based on ActiveSupport::MessagePack. The :message_pack serializer includes the same deserialization fallback mechanism, allowing easy migration from (or to) the default serializer. The :message_pack serializer may improve performance, but it requires the msgpack gem.

:compressor

The compressor for serialized cache values. Must respond to deflate and inflate.

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 dump and load.

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 :serializer or :compressor options instead.

If the store can handle cache entries directly, you may also specify coder: nil to 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 specify coder: nil to avoid the overhead of safeguarding against mutation.

The :coder option is mutally exclusive with the :serializer and :compressor options. Specifying them together will raise an ArgumentError.

Any other specified options are treated as default options for the relevant cache operations, such as #read, #write, and #fetch.

Show source
Register or log in to add new notes.