Google App Engine - 缓存生成的HTML [英] Google App Engine - Caching generated HTML

查看:104
本文介绍了Google App Engine - 缓存生成的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Google App Engine应用程序,它可以编程生成一堆HTML代码,这些代码对于登录到我的系统的每个用户来说都是相同的输出,并且我知道代码运行时效率会很低投入生产。所以,我试图找出缓存生成页面的最佳方法。

最可能的选择是生成页面并将它们写入数据库,然后检查给定页面的数据库put操作的时间与代码最近更新了。然后,如果代码比最后一次放入数据库(对于特定的HTML请求)更新,则会生成并提供新的HTML并缓存到数据库。如果代码比最后一次放入数据库的时间长,那么我只需从数据库获取HTML直接并提供它(因此避免了生成HTML的所有CPU浪费)。我不仅希望尽量减少加载时间,而且要尽量减少CPU使用。



然而,我遇到的一个问题是我无法弄清楚如何编程检查上传到应用引擎的代码版本是否已更新。



我对此方法的任何建议以及用于缓存生成的html的其他方法持开放态度。



请注意,而memcache可以在这种情况下提供帮助,我相信这不是最终的解决方案,因为当代码更新时(而不是每次memcache过期),我实际上只需要重新生成html。

解决方案

按照速度排序:

  • 在数据存储区中缓存HTML
  • 整页生成

  • 您缓存解决方案应该考虑到这一点。从本质上讲,我可能会推荐使用memcache。在大多数情况下,它将比访问数据存储更快,并且当您生成大块HTML时,缓存的主要优点之一是您可能不必承担访问数据的I / O惩罚商店。如果您使用数据存储进行缓存,则仍有I / O损失。除非您有一个非常复杂的页面,否则重新生成所有内容和从数据存储区中缓存html中提取数据之间的差异可能相当小。从memcache中获得一堆速度非常快的缓存命中可能会更好,并且每隔一段时间就会完成一次完整的再生,而不是每次都向数据存储发出调用。当你更新时,没有什么能够阻止你在memcache中使缓存的HTML无效,并且如果你的流量足够高以保证它的话,你总是可以做一个多级缓存系统。



    <但是,我主要担心的是,这是过早的优化。如果您还没有流量,请将缓存保持在最低限度。 App Engine提供了一套非常方便的性能分析工具,您应该使用这些工具来确定您的流量至少有几个QPS后的瓶颈。


    随时你正在做性能优化,首先测量!很多性能优化结果要么比原来的要慢,要么完全一样,要么它们具有负面的用户体验特征(如陈旧的数据)。不要优化,直到你确定你必须。


    I have written a Google App Engine application that programatically generates a bunch of HTML code that is really the same output for each user who logs into my system, and I know that this is going to be in-efficient when the code goes into production. So, I am trying to figure out the best way to cache the generated pages.

    The most probable option is to generate the pages and write them into the database, and then check the time of the database put operation for a given page against the time that the code was last updated. Then, if the code is newer than the last put to the database (for a particular HTML request), new HTML will be generated and served, and cached to the database. If the code is older than the last put to the database, then I will just get the HTML direct from the database and serve it (therefore avoiding all the CPU wastage of generating the HTML). I am not only looking to minimize load times, but to minimize CPU usage.

    However, one issue that I am having is that I can't figure out how to programatically check when the version of code uploaded to the app engine was updated.

    I am open to any suggestions on this approach, or other approaches for caching generated html.

    Note that while memcache could help in this situation, I believe that it is not the final solution since I really only need to re-generate html when the code is updated (as opposed to every time the memcache expires).

    解决方案

    In order of speed:

    1. memcache
    2. cached HTML in data store
    3. full page generation

    Your caching solution should take this into account. Essentially, I would probably recommend using memcache anyways. It will be faster than accessing the data store in most cases and when you're generating a large block of HTML, one of the main benefits of caching is that you potentially didn't have to incur the I/O penalty of accessing the data store. If you cache using the data store, you still have the I/O penalty. The difference between regenerating everything and pulling from cached html in the data store is likely to be fairly small unless you have a very complex page. It's probably better to get a bunch of very fast cache hits off memcache and do a full regenerate every once in a while than to make a call out to the data store every time. There's nothing stopping you from invalidating the cached HTML in memcache when you update, and if your traffic is high enough to warrant it, you can always do a multi-level caching system.

    However, my main concern is that this is premature optimization. If you don't have the traffic yet, keep caching to a minimum. App Engine provides a set of really convenient performance analysis tools, and you should be using those to identify bottlenecks after you've got at least a few QPS of traffic.

    Anytime you're doing performance optimization, measure first! A lot of performance "optimizations" turn out to either be slower than the original, exactly the same, or they have negative user experience characteristics (like stale data). Don't optimize until you're certain you have to.

    这篇关于Google App Engine - 缓存生成的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆