使用外部API时加速页面加载

时间:2022-08-24 16:58:58

I'm building a site with django that lets users move content around between a bunch of photo services. As you can imagine the application does a lot of api hits.

我正在使用django构建一个网站,允许用户在一堆照片服务之间移动内容。你可以想象这个应用程序做了很多api命中。

for example: user connects picasa, flickr, photobucket, and facebook to their account. Now we need to pull content from 4 different apis to keep this users data up to date.

例如:用户将picasa,flickr,photobucket和facebook连接到他们的帐户。现在我们需要从4个不同的api中提取内容,以使这些用户数据保持最新状态。

right now I have a a function that updates each api and I run them all simultaneously via threading. (all the api's that are not enabled return false on the second line, no it's not much overhead to run them all).

现在我有一个更新每个api的函数,我通过线程同时运行它们。 (所有未启用的api都会在第二行返回false,不是没有太大的开销来运行它们)。

Here is my question:

What is the best strategy for keeping content up to date using these APIs?

使用这些API保持内容最新的最佳策略是什么?

I have two ideas that might work:

我有两个可能有用的想法:

  1. Update the apis periodically (like a cron job) and whatever we have at the time is what the user gets.

    定期更新api(如cron作业),当时我们拥有的是用户获得的内容。

    benefits:

    • It's easy and simple to implement.
    • 实施简单易行。

    • We'll always have pretty good data when a user loads their first page.
    • 当用户加载他们的第一页时,我们总是会有非常好的数据。

    pitfalls:

    • we have to do api hits all the time for users that are not active, which wastes a lot of bandwidth
    • 我们必须一直为那些不活跃的用户做api命中,这会浪费很多带宽

    • It will probably make the api providers unhappy
    • 它可能会使api提供商不高兴

  2. Trigger the updates when the user logs in (on a pageload)

    用户登录时触发更新(在页面加载上)

    benefits:

    • we save a bunch of bandwidth and run less risk of pissing off the api providers
    • 我们节省了大量带宽,降低了api提供商的压力

    • doesn't require NEARLY the amount of resources on our servers
    • 我们的服务器上几乎不需要大量的资源

    pitfalls:

    • we either have to do the update asynchronously (and won't have anything on first login) or...
    • 我们要么必须异步进行更新(并且首次登录时不会有任何内容)或者......

    • the first page will take a very long time to load because we're getting all the api data (I've measured 26 seconds this way)
    • 第一页需要很长时间才能加载,因为我们正在获取所有api数据(我用这种方式测量了26秒)

edit: the design is very light, the design has only two images, an external css file, and two external javascript files.

编辑:设计很轻,设计只有两个图像,一个外部css文件和两个外部javascript文件。

Also, the 26 seconds number comes from the firebug network monitor running on a machine which was on the same LAN as the server

此外,26秒的数字来自在与服务器位于同一LAN上的计算机上运行的firebug网络监视器

1 个解决方案

#1


Personally, I would opt for the second method you mention. The first time you log in, you can query each of the services asynchronously, showing the user some kind of activity/status bar while the processes are running. You can then populate the page as you get the results back from each of the services.

就个人而言,我会选择你提到的第二种方法。第一次登录时,您可以异步查询每个服务,在进程运行时向用户显示某种活动/状态栏。然后,您可以在从每个服务返回结果时填充页面。

You can then cache the results of those calls per user so that you don't have to call the apis each time.

然后,您可以缓存每个用户的这些调用的结果,这样您就不必每次都调用api。

That lightens the load on your servers, loads your page fast, and provides the user with some indication of activity (along with incrimental updates to the page as their content loads). I think those add up to the best User Experience you can provide.

这样可以减轻服务器的负担,快速加载页面,并为用户提供一些活动指示(以及在内容加载时对页面进行实际更新)。我认为这些可以提供您可以提供的最佳用户体验。

#1


Personally, I would opt for the second method you mention. The first time you log in, you can query each of the services asynchronously, showing the user some kind of activity/status bar while the processes are running. You can then populate the page as you get the results back from each of the services.

就个人而言,我会选择你提到的第二种方法。第一次登录时,您可以异步查询每个服务,在进程运行时向用户显示某种活动/状态栏。然后,您可以在从每个服务返回结果时填充页面。

You can then cache the results of those calls per user so that you don't have to call the apis each time.

然后,您可以缓存每个用户的这些调用的结果,这样您就不必每次都调用api。

That lightens the load on your servers, loads your page fast, and provides the user with some indication of activity (along with incrimental updates to the page as their content loads). I think those add up to the best User Experience you can provide.

这样可以减轻服务器的负担,快速加载页面,并为用户提供一些活动指示(以及在内容加载时对页面进行实际更新)。我认为这些可以提供您可以提供的最佳用户体验。