Azure 应用服务本地缓存概述Azure App Service Local Cache overview


函数应用或 Linux 上的应用服务不支持本地缓存。Local cache is not supported in Function apps or on App Service on Linux.

Azure 应用服务内容将存储在 Azure 存储中,作为内容共享持续呈现。Azure App Service content is stored on Azure Storage and is surfaced in a durable manner as a content share. 此设计旨在兼容各种应用,具有以下特点:This design is intended to work with a variety of apps and has the following attributes:

  • 内容跨应用的多个虚拟机 (VM) 实例共享。The content is shared across multiple virtual machine (VM) instances of the app.
  • 内容是持久性的,运行应用即可对其进行修改。The content is durable and can be modified by running apps.
  • 在同一共享内容文件夹下提供日志文件和诊断数据文件。Log files and diagnostic data files are available under the same shared content folder.
  • 发布新内容会直接更新内容文件夹。Publishing new content directly updates the content folder. 可以通过 SCM 网站和运行的应用即时查看相同的内容(通常情况下,在文件发生更改时,可以通过 ASP.NET 之类的特定技术重启应用,以获取最新内容)。You can immediately view the same content through the SCM website and the running app (typically some technologies such as ASP.NET do initiate an app restart on some file changes to get the latest content).

虽然许多应用使用所有此类功能或其中一项功能,但某些应用只需要高性能的只读内容存储,此类存储可用性高且支持这些应用的运行。While many apps use one or all of these features, some apps just need a high-performance, read-only content store that they can run from with high availability. 这些应用可以充分利用特定本地缓存的 VM 实例。These apps can benefit from a VM instance of a specific local cache.

Azure 应用服务本地缓存功能允许通过 Web 角色来查看内容。The Azure App Service Local Cache feature provides a web role view of your content. 存储内容是在站点启动后异步创建的,而该内容是对存储内容进行写后即丢弃式缓存的结果。This content is a write-but-discard cache of your storage content that is created asynchronously on-site startup. 当缓存就绪时,对站点进行切换,使之在已缓存内容的基础上运行。When the cache is ready, the site is switched to run against the cached content. 在本地缓存上运行的应用具有以下优势:Apps that run on Local Cache have the following benefits:

  • 不受访问 Azure 存储上的内容时出现的延迟的影响。They are immune to latencies that occur when they access content on Azure Storage.
  • 不受计划内升级/计划外停机以及 Azure 存储出现的任何其他中断事件(发生在提供内容共享服务的服务器上)的影响。They are immune to the planned upgrades or unplanned downtimes and any other disruptions with Azure Storage that occur on servers that serve the content share.
  • 因存储共享更改而需要重新启动应用的次数较少。They have fewer app restarts due to storage share changes.

本地缓存如何改变应用服务的行为How the local cache changes the behavior of App Service

  • D:\home 指向本地缓存,它是应用启动时在 VM 实例上创建的。D:\home points to the local cache, which is created on the VM instance when the app starts up. D:\local 继续指向特定于临时 VM 的存储。D:\local continues to point to the temporary VM-specific storage.
  • 本地缓存包含共享内容存储的 /site 和 /siteextensions 文件夹的一次性副本,分别位于 D:\home\site 和 D:\home\siteextensions。 The local cache contains a one-time copy of the /site and /siteextensions folders of the shared content store, at D:\home\site and D:\home\siteextensions, respectively. 应用启动时,文件会复制到本地缓存。The files are copied to the local cache when the app starts. 默认情况下,每个应用的这两个文件夹的大小限制为 1 GB,但可增至 2 GB。The size of the two folders for each app is limited to 1 GB by default, but can be increased to 2 GB. 请注意,随着缓存大小的增加,加载缓存需要的时间也越长。Note that as the cache size increases, it will take longer to load the cache. 如果已将本地缓存限制增加到 2 GB,并且复制的文件超过了最大大小 2 GB,则应用服务会在不提示的情况下忽略本地缓存,从远程文件共享读取数据。If you've increased local cache limit to 2 GB and the copied files exceed the maximum size of 2 GB, App Service silently ignores local cache and reads from the remote file share. 如果没有定义限制,或者该限制设置为低于 2 GB 的任何值,而复制的文件超出了该限制,则部署或交换可能会失败并出现错误。If there is no limit defined or the limit is set to anything lower than 2 GB and the copied files exceeds the limit, the deployment or swap may fail with an error.
  • 本地缓存是可以读写的。The local cache is read-write. 不过,如果应用移动了虚拟机,或者系统重启了应用,则会放弃所做的任何修改。However, any modification is discarded when the app moves virtual machines or gets restarted. 如果应用在内容存储中存储了任务关键型数据,请不要使用本地缓存。Do not use the local cache for apps that store mission-critical data in the content store.
  • D:\home\LogFiles 和 D:\home\Data 包含日志文件和应用数据。 D:\home\LogFiles and D:\home\Data contain log files and app data. 两个子文件夹本地存储在 VM 实例上,并定期复制到共享内容存储。The two subfolders are stored locally on the VM instance, and are copied to the shared content store periodically. 应用可以通过将日志文件和数据写入到这些文件夹来保留它们。Apps can persist log files and data by writing them to these folders. 但是,复制到共享内容存储是最大努力,因此由于 VM 实例的突然崩溃,日志文件和数据可能会丢失。However, the copy to the shared content store is best-effort, so it is possible for log files and data to be lost due to a sudden crash of a VM instance.
  • 日志流式处理受最大努力副本的影响。Log streaming is affected by the best-effort copy. 可以在流式传输的日志中观察到最多一分钟的延迟。You could observe up to a one-minute delay in the streamed logs.
  • 在共享内容存储中,对于使用本地缓存的应用, LogFilesData 文件夹的文件夹结构会发生变化。In the shared content store, there is a change in the folder structure of the LogFiles and Data folders for apps that use the local cache. 它们之中现在出现了子文件夹,其遵循的命名模式为“唯一标识符”+ 时间戳。There are now subfolders in them that follow the naming pattern of "unique identifier" + time stamp. 每个子文件夹对应于应用正在其中运行或已运行的一个虚拟机实例。Each of the subfolders corresponds to a VM instance where the app is running or has run.
  • D:\home 中的其他文件夹保留在本地缓存中,不会复制到共享内容存储。Other folders in D:\home remain in the local cache and are not copied to the shared content store.
  • 通过任何支持的方法进行的应用部署都将直接发布到持久共享内容存储。App deployment through any supported method publishes directly to the durable shared content store. 若要刷新本地缓存中的 D:\home\site 和 D:\home\siteextensions 文件夹,需要重新启动应用。 To refresh the D:\home\site and D:\home\siteextensions folders in the local cache, the app needs to be restarted. 若要确保无缝的生命周期,请参阅本文后面提供的信息。To make the lifecycle seamless, see the information later in this article.
  • SCM 站点的默认内容视图仍是共享内容存储的视图。The default content view of the SCM site continues to be that of the shared content store.

在应用服务中启用本地缓存Enable Local Cache in App Service

组合使用保留的应用设置即可配置本地缓存。You configure Local Cache by using a combination of reserved app settings. 可以通过以下方法配置这些应用设置:You can configure these app settings by using the following methods:

通过 Azure 门户配置本地缓存Configure Local Cache by using the Azure portal

使用以下应用设置即可在每个 Web 应用的基础上启用本地缓存: WEBSITE_LOCAL_CACHE_OPTION = AlwaysYou enable Local Cache on a per-web-app basis by using this app setting: WEBSITE_LOCAL_CACHE_OPTION = Always

Azure 门户应用设置:本地缓存

使用 Azure Resource Manager 配置本地缓存Configure Local Cache by using Azure Resource Manager


    "apiVersion": "2015-08-01",
    "type": "config",
    "name": "appsettings",
    "dependsOn": [
        "[resourceId('Microsoft.Web/sites/', variables('siteName'))]"

    "properties": {


更改本地缓存中的大小设置Change the size setting in Local Cache

本地缓存大小默认为 1 GBBy default, the local cache size is 1 GB. 其中包括从内容存储复制过来的 /site 和 /siteextensions 文件夹,以及任何本地创建的日志和数据文件夹。This includes the /site and /siteextensions folders that are copied from the content store, as well as any locally created logs and data folders. 若要增加此限制,请使用应用设置 WEBSITE_LOCAL_CACHE_SIZEINMBTo increase this limit, use the app setting WEBSITE_LOCAL_CACHE_SIZEINMB. 最高可将此大小增加到每个应用 2 GB (2000 MB)。You can increase the size up to 2 GB (2000 MB) per app. 请注意,当此大小增加时,加载本地缓存需要的时间会变长。Note that it will take longer to load local cache as the size increases.

使用应用服务本地缓存的最佳实践Best practices for using App Service Local Cache

建议将本地缓存与过渡环境功能结合在一起使用。We recommend that you use Local Cache in conjunction with the Staging Environments feature.

  • 将值为 Always粘性应用设置 WEBSITE_LOCAL_CACHE_OPTION 添加到生产槽。Add the sticky app setting WEBSITE_LOCAL_CACHE_OPTION with the value Always to your Production slot. 如果使用的是 WEBSITE_LOCAL_CACHE_SIZEINMB,也可将其作为粘性设置添加到“生产”槽。If you're using WEBSITE_LOCAL_CACHE_SIZEINMB, also add it as a sticky setting to your Production slot.
  • 创建过渡槽,并发布到过渡槽。Create a Staging slot and publish to your Staging slot. 如果获得了生产槽的本地缓存优势,则要想通过无缝的“构建-部署-测试”生命周期进行过渡,通常不需要将过渡槽设置为使用本地缓存。You typically don't set the staging slot to use Local Cache to enable a seamless build-deploy-test lifecycle for staging if you get the benefits of Local Cache for the production slot.
  • 针对“过渡”槽来测试站点。Test your site against your Staging slot.
  • 准备就绪以后,在过渡槽和生产槽之间执行交换操作When you are ready, issue a swap operation between your Staging and Production slots.
  • 粘性设置包含名称,会粘到某个槽上。Sticky settings include name and sticky to a slot. 因此,将“过渡”槽交换成“生产”槽以后,该槽会继承本地缓存应用设置。So when the Staging slot gets swapped into Production, it inherits the Local Cache app settings. 新交换的“生产”槽会在几分钟后以本地缓存为基础运行,并会在交换后进行槽预热的过程中预热。The newly swapped Production slot will run against the local cache after a few minutes and will be warmed up as part of slot warmup after swap. 因此,在槽交换完成后,“生产”槽会在本地缓存的基础上运行。So when the slot swap is complete, your Production slot is running against the local cache.

常见问题 (FAQ)Frequently asked questions (FAQ)

如何确定本地缓存是否适用于应用?How can I tell if Local Cache applies to my app?

如果应用需要高性能且可靠的内容存储,在运行时不使用内容存储来写入关键数据,并且总大小不到 2 GB,则可确定本地缓存适用于应用。If your app needs a high-performance, reliable content store, does not use the content store to write critical data at runtime, and is less than 2 GB in total size, then the answer is "yes"! 可通过站点扩展“Azure Web 应用磁盘使用情况”获取 /site 和 /siteextensions 文件夹的总大小。To get the total size of your /site and /siteextensions folders, you can use the site extension "Azure Web Apps Disk Usage."

如何确定站点是否已切换为使用本地缓存?How can I tell if my site has switched to using Local Cache?

如果在过渡环境中使用本地缓存功能,则在本地缓存预热之前,交换操作不会完成。If you're using the Local Cache feature with Staging Environments, the swap operation does not complete until Local Cache is warmed up. 若要了解站点是否正在本地缓存的基础上运行,可查看工作进程环境变量 WEBSITE_LOCALCACHE_READYTo check if your site is running against Local Cache, you can check the worker process environment variable WEBSITE_LOCALCACHE_READY. 根据工作进程环境变量页上的说明,访问多个实例上的工作进程环境变量。Use the instructions on the worker process environment variable page to access the worker process environment variable on multiple instances.

刚发布的新更改内容似乎并没有在应用上显示出来,I just published new changes, but my app does not seem to have them. 为什么?Why?

如果应用使用本地缓存,则需重新启动站点才能获取最新更改。If your app uses Local Cache, then you need to restart your site to get the latest changes. 不想将更改发布到生产站点?Don’t want to publish changes to a production site? 请参阅前述最佳实践部分的槽选项。See the slot options in the previous best practices section.


从包运行部署选项与本地缓存不兼容。The run from package deployment option is not compatible with local cache.

日志在哪里?Where are my logs?

使用本地缓存时,日志和数据文件夹看起来稍有不同。With Local Cache, your logs and data folders do look a little different. 但是,子文件夹的结构始终是相同的,区别在于子文件夹嵌套在格式为“唯一 VM 标识符”+ 时间戳的子文件夹下。However, the structure of your subfolders remains the same, except that the subfolders are nestled under a subfolder with the format "unique VM identifier" + time stamp.

本地缓存已启用,但应用仍重新启动。I have Local Cache enabled, but my app still gets restarted. 为什么会这样?Why is that? 本以为本地缓存可以缓解应用频繁重启的情况。I thought Local Cache helped with frequent app restarts.

本地缓存确实可以防止与存储相关的应用重启情况。Local Cache does help prevent storage-related app restarts. 但是,在对 VM 进行计划内基础结构升级时,应用仍可能重新启动。However, your app could still undergo restarts during planned infrastructure upgrades of the VM. 启用本地缓存以后,总体说来应用重启的次数应该会少一些。The overall app restarts that you experience with Local Cache enabled should be fewer.

本地缓存是否会阻止某些目录被复制到更快的本地驱动器?Does Local Cache exclude any directories from being copied to the faster local drive?

在复制存储内容过程中,将排除任何名为存储库的文件夹。As part of the step that copies the storage content, any folder that is named repository is excluded. 如果站点内容包含应用日常操作中可能不必要的源控件存储库,则此方法非常有用。This helps with scenarios where your site content may contain a source control repository that may not be needed in day to day operation of the app.