When to use memoization in Ruby on Rails

前端 未结 3 1646
醉酒成梦
醉酒成梦 2020-12-07 14:57

In mid July 2008 Memoization was added to Rails core. A demonstration of the usage is here.

I have not been able to find any good examples on when methods should be

3条回答
  •  没有蜡笔的小新
    2020-12-07 15:19

    When a method fetches data from multiple tables, and perform some calculations before returning the resulting object, and this method is multiple times in requests, memoization might make sense.

    Remember that query caching is also active, so only memoize methods which perform in-Ruby calculations, not pure database fetches.

提交回复
热议问题