How should ActiveSupport::Cache handle nil cache key?

Hi, I notice ActiveSupport::Cache implementations behave differently when cache key is nil.

cache_key = nil
Rails.cache.write(cache_key, 'value')
Rails.cache.read(cache_key)

For example, FileStore raises a file system error “No such file or directory” when calling write() with nil cache key. This error does not communicate what actually went wrong (i.e. cache key cannot be nil/empty). It’s better if a descriptive exception is raised.

Anyway, I’m curious what should be the expected behavior when cache key is nil? And whether the expected behavior should be specified in ActiveSupport::Cache or in its implementation?

fyi, more details and failing tests are available: https://github.com/rails/rails/pull/11009

From the table in your pull request, the MemCacheStore seems to be the one I’d expect from all the options, i.e. writing or reading a nil key raises “ArgumentError: key cannot be blank”.

Considering the docs even go as far as to say MemCacheStore is “currently the most popular cache store for production websites” and its current implementation is the most “breaking”, I think it makes sense to mirror its behavior for the more dev environment oriented options.

Should we enforce the "no blank key" behavior in each implementation, or enforce it in the base class i.e. ActiveSupport::Cache?