duanhuilao0787 2017-01-25 09:09 采纳率: 100%
浏览 13

我是否应该将缓存的数据保留在Web应用程序的内存中?

I have a database where products are kept.

There is also a list of categories.

Each product belongs to a category.

When a list of products updates, I run a function updateProductsCategories() that queries database, calculates a number of products in each category and returns a list like this:

Food:    20
Drinks:  74
Jackets: 15 

I use this list in a web page sidebar to show categories as links with amount of products in it.

My assumption is that I should run updateProductsCategories() as it is when products change, though not call it each time a page loads, but put its result in an in-memory object and get data from it to show on the page.

That way I will not make unnecessary db queries every time I show the page, but will use cached data and will have it always up-to-date as it's refreshed when products change.

As the object is not large, I don't think I need any additional solutions like Redis now.

Is it a correct and common way to optimize the app? Will it add speed to the app in real world?

  • 写回答

2条回答 默认 最新

  • douwen5951 2017-01-25 09:19
    关注

    If:

    • you need the stats frequently (number of products in each category)
      and / or acquiring the stats takes relatively long time / load
    • and product update is "infrequent" (compared to how frequently you need the stats)

    then yes, it is worth keeping it in memory. Every time a product is updated, you may invalidate the cached data and let it calculated again deferred (when it is needed) or calculate the new stats right away, after the update.

    Things to keep in mind:

    • access to these stats should be synchronized as web requests are served on multiple goroutines
    • if you have multiple server instances, these stats might become inaccurate; in this case you do need a central cache like Redis
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么