PHP 5 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 PHP 5 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 PHP 5 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 PHP 版本。
配置预热请求以提高性能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在将应用代码加载到新创建的实例时,您可以使用预热请求来缩短请求与响应的延迟时间。
App Engine 经常需要将应用代码加载到全新实例中。以下情况下可能会加载实例:
- 重新部署应用版本时。
- 由于请求产生的负载超过当前运行中实例集的容量而创建新实例时。
- 维护和维修底层基础架构或物理硬件时。
将应用代码加载到新实例会产生加载请求。加载请求可能会延长用户的请求延迟时间,但您可以使用预热请求来避免此延时。预热请求会在任何实际请求到达新实例之前,先将应用的代码加载到新实例中。
如果您为应用启用了预热请求,则 App Engine 会尝试检测您的应用何时需要新实例,并发出预热请求来对新实例进行初始化。但是,此类检测尝试并不适用于所有情况。因此,即使应用中启用了预热请求,您也可能会遇到加载请求。例如,如果您的应用目前没有任何流量,则对应用的第一个请求始终会是加载请求,而不是预热请求。
与任何其他发送至 App Engine 应用的请求一样,预热请求也会使用实例小时数。在大多数启用了预热请求的情况下,您不会注意到实例小时数的增加,因为您的应用只是在预热请求(而非加载请求)中进行初始化。如果您决定执行更多工作,例如在预热请求期间预先缓存,则使用的实例小时数可能会增加。如果您将 min_idle_instances
设置为大于 0
,则在这些实例首次启动时,您可能会遇到预热请求,但在此之后这些实例将保持可用。
启用预热请求
预热请求由 App Engine 调度器使用,该调度器根据用户提供的配置来控制实例的自动扩缩。启用预热请求后,App Engine 会向 /_ah/warmup
发出 GET
请求。您可以针对此请求实现处理程序,以执行预先缓存应用数据等应用特定任务。
如果调度程序判断需要更多实例,便会启动实例。由于调度器使用预热请求来启动实例,因此即使停用了预热请求,它们也可能会出现在日志中。
请注意,系统不一定会调用预热请求。在某些情况下,系统会改为发送加载请求:例如,如果实例是第一个被启动的实例,或者流量出现急剧上升。但是,如果启用了预热请求,系统将“尽力”尝试向已经预热的实例发送请求。
要启用预热请求,请在 app.yaml
文件中的 inbound_services
指令下添加 warmup
元素,例如:
inbound_services:
- warmup
注册处理程序
您可以在项目的 app.yaml
文件中注册处理预热请求的脚本。例如:
inbound_services:
- warmup
handlers:
- url: /_ah/warmup
script: warmup.php
login: admin
此示例会注册一个处理程序,以使用 warmup.php
文件侦听对 /_ah/warmup
请求路径的预热请求。
创建处理程序
创建一个处理程序,用于处理发送到 /_ah/warmup
的请求。您的处理程序应执行应用所需的任何预热逻辑。以下示例基于前一示例构建:
在 PHP 5 运行时环境中,您需要部署前端控制器才能处理所有请求路由,包括 /_ah/warmup
请求。
例如,以下手动前端控制器包含用于处理预热请求的代码:
<?php
// Handle your warmup logic for your app.
switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
case '/_ah/warmup':
echo "Warmup successful";
break;
// Other handlers
// ...
}
?>
后续步骤
您可能希望将值存储在内存里的数据存储区(例如 Memcache)中,让您的应用只需较少查询即可快速访问数据。
例如,如果您为网站构建并存储时下热门文章列表,则可以在预热中构建该列表,然后将其存储在 Memcache 中。当用户发出请求时,App Engine 不需要执行任何数据存储区查询,同时,应用可以更快地处理用户的请求。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eWarmup requests help reduce latency by loading your app's code into a new instance before live requests arrive.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine initiates warmup requests when a new instance is needed, attempting to initialize it before any live traffic is directed to it, though this is not always guaranteed.\u003c/p\u003e\n"],["\u003cp\u003eEnabling warmup requests involves adding the \u003ccode\u003ewarmup\u003c/code\u003e element under the \u003ccode\u003einbound_services\u003c/code\u003e directive in your \u003ccode\u003eapp.yaml\u003c/code\u003e file, which tells App Engine to issue requests to \u003ccode\u003e/_ah/warmup\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can register a specific script, like \u003ccode\u003ewarmup.php\u003c/code\u003e, to handle the \u003ccode\u003e/_ah/warmup\u003c/code\u003e requests and perform application-specific tasks such as pre-caching data.\u003c/p\u003e\n"],["\u003cp\u003eUtilizing warmup requests can also be coupled with in-memory datastores like Memcache to store pre-calculated data, further enhancing performance by reducing the need for datastore queries.\u003c/p\u003e\n"]]],[],null,["# Configuring Warmup Requests to Improve Performance\n\nYou can use warmup requests to reduce request and response latency during the\ntime when your app's code is being loaded to a newly created instance.\n\nApp Engine frequently needs to load your app's code into a fresh\ninstance. Loading an instance can happen in the following situations:\n\n- When you redeploy a version of your app.\n- When new instances are created due to the load from requests exceeding the capacity of the current set of running instances.\n- When maintenance and repairs of the underlying infrastructure or physical hardware occur.\n\nLoading your app's code to a new instance can result in [loading\nrequests](/appengine/docs/legacy/standard/php/how-instances-are-managed#loading_requests).\nLoading requests can result in increased request latency for your users, but you\ncan avoid this latency using *warmup requests*. Warmup requests load your\napp's code into a new instance before any live requests reach that instance.\n\nIf warmup requests are enabled for your application, App Engine attempts\nto detect when your application needs a new instance and initiates a warmup\nrequest to initialize a new instance. However, these detection attempts do not\nwork in every case. As a result, you might encounter loading requests, even if\nwarmup requests are enabled in your app. For example, if your app is serving no\ntraffic, the first request to the app will always be a loading request, not a\nwarmup request.\n\nWarmup requests use instance hours like any other request to your\nApp Engine application. In most cases where warmup requests are enabled,\nyou won't notice an increase in instance hours because your application is\nsimply initializing in a warmup request instead of a loading request. Your\ninstance hour usage can increase if you decide to do more work, such as\npre-caching during a warmup request. If you set\n[`min_idle_instances`](/appengine/docs/legacy/standard/php/config/appref#min_idle_instances)\nto greater than `0`, you might encounter warmup requests when those instances\nfirst start, but they will remain available after that time.\n\nEnabling warmup requests\n------------------------\n\nWarmup requests are used by the App Engine scheduler, which controls the\nauto scaling of instances based on user-supplied configuration. With warmup\nrequests enabled, App Engine issues `GET` requests to `/_ah/warmup`. You\ncan implement handlers for this request to perform application-specific tasks,\nsuch as pre-caching application data.\n\nThe scheduler starts up instances when it determines that more instances are\nneeded. Warmup requests may appear in\n[logs](/appengine/docs/legacy/standard/php/writing-application-logs#viewing_logs)\neven if they are disabled because the scheduler uses them to start instances.\n\nNote that warmup requests are not guaranteed to be called. In some situations\nloading requests are sent instead: for example, if the instance is the first one\nbeing started up, or if there is a steep ramp-up in traffic. However, there\nwill be a \"best effort\" attempt to send requests to already warmed-up instances\nif warmup requests are enabled.\n\nTo enable warmup requests, add the `warmup` element under the\n[`inbound_services`](/appengine/docs/legacy/standard/php/config/appref#inbound_services)\ndirective in your `app.yaml` file, for example: \n\n inbound_services:\n - warmup\n\n\u003cbr /\u003e\n\nRegistering your handler\n------------------------\n\nYou can register the script that handles warmup requests in your project's\n`app.yaml` file. For example: \n\n inbound_services:\n - warmup\n\n handlers:\n - url: /_ah/warmup\n script: warmup.php\n login: admin\n\nThis example registers a handler to listen to warmup requests to the\n`/_ah/warmup` request path with the\n`warmup.php` file.\n\nCreating your handler\n---------------------\n\nCreate a handler that will process the requests that are sent to `/_ah/warmup`.\nYour handler should perform any warmup logic that is needed by your app.\n\nThe following example builds on the previous example:\n\n\nIn the PHP 5 runtime environment, you need to\ndeploy a front controller to handle all request routing,\nincluding `/_ah/warmup` requests.\n\nFor example, the following manual front controller contains code to handle\nwarmup requests: \n\n \u003c?php\n // Handle your warmup logic for your app.\n switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {\n case '/_ah/warmup':\n echo \"Warmup successful\";\n break;\n // Other handlers\n // ...\n }\n ?\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWhat's next\n-----------\n\nYou might want to store values in an in-memory datastore such as\n[Memcache](/appengine/docs/legacy/standard/php/memcache), giving your app\nfast, query-less access to data.\n\nFor example, if you build and store a list of the current trending articles for\nyour site, you can build that list in the warmup and store it in Memcache. When\na user request comes in, App Engine doesn't need to perform any\ndatastore queries and the application can serve the user's request faster.\n\n\u003cbr /\u003e"]]