7

Rails 7 ActiveSupport::Cache::MemCacheStore now accepts an explicit nil for addr...

 3 years ago
source link: https://blog.saeloun.com/2021/05/11/rails-7-activesupport-cache-memcachestore-now-accepts-an-explicit-nil-for-addresses
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails 7 ActiveSupport::Cache::MemCacheStore now accepts an explicit nil for addresses argument

May 11, 2021 , by Prateek Choudhary
1 minute read

For those using Dalli as cache store in Rails, this config should appear somewhat familiar:

config.cache_store = :dalli_cache, nil, { expires_in: 1.hour }

In the above code snippet, we passed nil for the addresses argument. But if we were to migrate to :mem_cache_store with the same config like below:

config.cache_store = :mem_cache_store, nil, { expires_in: 1.hour }

then the value of addresses would be [nil] instead of nil causing an exception when trying to access the cache.

> Rails.cache.fetch('user_id')
NoMethodError: undefined method `match' for nil:NilClass

Rails 7

This PR adds support for passing an explicit nil to mem_cache_store. This change makes migrating from :dalli_store to :mem_cache_store easier.

After this change, below config:

config.cache_store = :mem_cache_store, nil

would be equivalent to

config.cache_store = :mem_cache_store  # implicit nil for addresses

Share this post!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK