开发者

Rails DB: How to cache complex calculations? Memoizing? Separate table? Action caching?

For my site, I have models Category and Product, with hundreds of categories, each having thousands of products. For the Category views, I'd like to do something like average price of all the products included. And for the product views, I'd like the average for that product's category to appear.

So, with thousands of these calc开发者_如何转开发ulations having to be stored somewhere, where's the best place to do it? Creating a lookup table with values? Or does caching/memoizing a Category action that calculates the average work fine?


Well, you can certainly use standard rails application cache and keep it all in memory. Few thousands of numbers isn't too much.

Example from the tutorial:

Rails.cache.read("city")   # => nil
Rails.cache.write("city", "Duckburgh")
Rails.cache.read("city")   # => "Duckburgh"


Have a look at Cache Money for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜