From: hunter_spawn@... Date: 2020-12-10T18:17:05+00:00 Subject: [ruby-core:101402] [Ruby master Feature#17342] Hash#fetch_set Issue #17342 has been updated by MaxLap (Maxime Lapointe). matz (Yukihiro Matsumoto) wrote in #note-16: > Most of the case, `hash[:key] ||= init` works. The exception is that `init` value being false. But it should be rare. The problem is not only with `false`, but with `nil` too. Anytime you want to cache something and nil (or false) is a possible value (often meaning the data is missing), this problem arises, as it means these cases are recalculated each time, to again indicate that the data is missing. ```ruby # If there is no config with that internal_id, each time you try to use the # cached version, which is meant to be fast, it would actually do a query to the database def self.cached_by_internal_id(internal_id) my_cache[internal_id] ||= Config.where(internal_id: internal_id).first end ``` It is very easy to forget that nil values means the cache is not going to act as a cache. It probably happens in many places, but is not critical, just makes things a little slower from redoing work many times. --- As for the name, there are other options that were suggested: * fetch_init * cache ---------------------------------------- Feature #17342: Hash#fetch_set https://bugs.ruby-lang.org/issues/17342#change-89163 * Author: MaxLap (Maxime Lapointe) * Status: Feedback * Priority: Normal ---------------------------------------- I would like to propose adding the `fetch_set` method to `Hash`. It behaves just like `fetch`, but when using the default value (2nd argument or the block), it also sets the value in the Hash for the given key. We often use the pattern `cache[key] ||= calculation`. This pattern however has a problem when the calculation could return false or nil, as in those case, the calculation is repeated each time. I believe the best practice in that case is: ```ruby cache.fetch(key) { cache[key] = calculation } ``` With my suggestion, it would be: ```ruby cache.fetch_set(key) { calculation } ``` In these examples, each part is very short, so the `fetch` case is still clean. But as each part gets longer, the need to repeat cache[key] becomes more friction. Here is a more realistic example: ```ruby # Also using the key argument to the block to avoid repeating the # long symbol, adding some indirection RequestStore.store.fetch(:monitor_value_is_delayed?) do |key| RequestStore.store[key] = !MonitorValue.where('date >= ?', Time.now - 5.minutes).exists? end RequestStore.store.fetch_set(:monitor_value_is_delayed?) do !MonitorValue.where('date >= ?', Time.now - 5.minutes).exists? end ``` There is a precedent for such a method: Python has it, but with a quite confusing name: `setdefault(key, default_value)`. This does not set a default for the whole dictionary as the name would make you think, it really just does what is proposed here. https://docs.python.org/3/library/stdtypes.html#dict.setdefault -- https://bugs.ruby-lang.org/ Unsubscribe: