@fluidframework/driver-web-cache

中文文档
3.12.0发布于 2026/11/11MITTypeScriptES Module近 30 天下载 4154

Implementation of the driver caching API for a web browser

原始使用文档
文档目录16 节
  1. Using Fluid Framework libraries
  2. Installation
  3. Importing from this package
  4. API Documentation
  5. Usage
  6. Parameters
  7. Atomic updates ( update )
  8. Clearing cache entries
  9. Minimum Client Requirements
  10. Supported Runtimes
  11. Supported Tools
  12. Module Resolution
  13. Module Formats
  14. Contribution Guidelines
  15. Help
  16. Trademark

@fluidframework/driver-web-cache

This package provides an implementation of the IPersistedCache interface in the odsp-driver package. This cache enables storing of user content on the user's machine in order to provide faster boot experiences when opening the same Fluid containers more than once. This implementation has a dependency on indexeddb, so it is intended to only be used in a browser context.

Using Fluid Framework libraries

For a dependency on a Fluid Framework library's public APIs, we recommend a ^ (caret) version range. For example, use ^1.3.4.

For a dependency on an unstable API, such as a beta API, we recommend a more restrictive version range. For example, use a ~ version range.

Installation

Run this command to install the package:

npm i @fluidframework/driver-web-cache

Importing from this package

This package uses package.json exports to separate APIs by support level. For information about the support guarantees, read API Support Levels.

Import the public APIs from @fluidframework/driver-web-cache.

Import the legacy APIs from @fluidframework/driver-web-cache/legacy.

API Documentation

Read the @fluidframework/driver-web-cache API documentation at https://fluidframework.com/docs/apis/driver-web-cache.

Usage

import { FluidCache } from "@fluidframework/driver-web-cache";

new FluidCache({
    partitionKey: userId,
    logger,
    maxCacheItemAge,
});

Parameters

  • partitionKey - Used to determine what partition of the cache is being used, and can prevent multiple users on the same machine from sharing a snapshot cache. If you absolutely know that users will not share the cache, can also be set to null. Currently optional, but is proposed to be required in the next major bump. The recommendation is to use this key to differentiate users for the cache data.
  • logger - An optional implementation of the logger contract where diagnostic data can be logged.
  • maxCacheItemAge - The cache tracks a timestamp with each entry. This flag specifies the maximum age (in milliseconds) for a cache entry to be used. This flag does not control when cached content is deleted since different scenarios and applications may have different staleness thresholds for the same data.

Atomic updates (update)

FluidCache exposes an update method that performs an atomic read-modify-write. The currently-cached value is read and the updater callback decides whether — and what — to write, inside a single IndexedDB readwrite transaction. This gives consistent update semantics across consumers sharing the same underlying IndexedDB instance (e.g. multiple browser tabs racing to persist offline pending state).

// Conditional overwrite (active tab wins): only write if our revision is higher.
const wrote = await fluidCache.update(entry, (existing, set) => {
    const existingRev = (existing as { rev?: number } | undefined)?.rev ?? -1;
    if (mine.rev > existingRev) {
        set(mine);
    }
});

// Read-modify-write: increment a counter atomically.
await fluidCache.update(entry, (existing, set) => {
    const prev = (existing as { count: number } | undefined)?.count ?? 0;
    set({ count: prev + 1 });
});

The updater callback is invoked with (existing, set). existing is undefined when the cached row would be invisible to get — that is, no entry exists for the key, the existing entry belongs to a different partition, or the existing entry is older than maxCacheItemAge. To commit a write, call set(value); to leave the cache untouched, return without calling set.

Calling set(undefined) removes the row at the key (equivalent to removeEntry inside the same atomic transaction). get already collapses "no entry" and "entry stored as undefined" into the same observable result, so the delete-on-undefined semantics gives callers an atomic conditional-delete without ambiguity for any meaningful use case.

The updater itself must be synchronous and set must be called from within it. IndexedDB transactions auto-close on any non-IDB await, which would silently break the atomicity that makes the update correct. Two guards make misuse loud rather than silent: calling set after the updater has returned throws a UsageError at the call site (so deferred-set patterns like invoking it from a setTimeout fail noisily); returning a thenable — for example, declaring the updater async — is detected after the updater returns, aborts the transaction, and is logged under FluidCacheUpdateCallbackError. If the updater calls set more than once, the last value wins. If the updater throws — including after calling set — the transaction is aborted and the existing row is preserved.

When set is called, the write (or delete) atomically replaces whatever row exists at the key, including cross-partition or stale rows the updater saw as undefined (matching the unconditional overwrite behavior of put). Callers that must preserve cross-partition rows should not use update.

update returns true if set was called and the write (or delete) committed, and false if the updater returned without calling set, threw, or an IDB error occurred.

Compare-and-set callers: the false return collapses three distinct outcomes — the updater returned without calling set (a lost race), the updater threw (including the async-updater misuse case), and the IDB write itself failed. Callers that need to distinguish these must consult telemetry: updater-side failures log under FluidCacheUpdateCallbackError; IDB-write failures log under FluidCachePutError. A lost compare-and-set race is not logged.

Clearing cache entries

Whenever any Fluid content is loaded with the web cache enabled, a task is scheduled to clear out all "stale" cache entries. This task is scheduled with the setIdleCallback browser API. We define stale cache entries as any cache entries that have not been used (read or written to) within the last 4 weeks. The cache is cleared of all stale cache entries corresponding to all documents, not just the ones corresponding to the Fluid document being loaded.

The deleteFluidCacheIndexDbInstance API that an application can use to clear out the entire contents of the snapshot cache at any time. We recommend calling this API when the user explicitly signs out. Hosting applications are on point for ensuring responsible usage of the snapshot caching capability to still meet any relevant customer promises, such as clearing out storage when appropriate or disabling snapshot caching under certain circumstances, such as when it is known the user is logged in to a public computer.

import { deleteFluidCacheIndexDbInstance } from "@fluidframework/driver-web-cache";

// We put a catch here because Firefox Incognito will throw an error here. This is why we claim this method is a "best effort", since sometimes the browser won't let us access storage
deleteFluidCacheIndexDbInstance().catch(() => {});

Minimum Client Requirements

Fluid Framework client libraries support the platforms in this document. These requirements are intentionally restrictive. Within a major version series, we can relax these requirements, but we cannot make them stricter. For a Long Term Support (LTS) version, we might need to support these platforms for several years.

Other configurations can work, but Fluid Framework does not support them. If an unsupported configuration stops working, we do not classify this as a bug. To request support for a configuration that is not listed, file an issue. The product team will evaluate your request. In the issue, specify the current status of the configuration:

  • The configuration works but needs official support.
  • The configuration does not work and requires changes.

Supported Runtimes

  • Fluid Framework supports Node.js versions 22 and 24 while they receive upstream support.
  • Fluid Framework supports modern browsers that support the ES2022 standard library.

Supported Tools

  • TypeScript 6.0:
    • Fluid Framework supports all strict options.
    • Set the build targets (lib, target) to ES2022 or later.
    • Enable strictNullChecks.
    • Fluid Framework does not support configuration options deprecated in TypeScript 6.0.
    • Fluid Framework does not fully support exactOptionalPropertyTypes. If you enable this option, do not use in, Reflect.has, Object.hasOwn, or Object.prototype.hasOwnProperty to narrow members of Fluid Framework types. These methods can incorrectly exclude undefined from the possible values.
  • webpack 5
    • We do not require a specific bundler. Other bundlers that handle ES Modules can work, but we actively test only webpack.

Module Resolution

In TypeScript compilerOptions, use Node16, Node20, NodeNext, or Bundler module resolution. These settings follow the Node.js v12+ ESM Resolution and Loading algorithm.

Do not use Node10 module resolution.

Module Formats

  • ES Modules: Use ES Modules to consume Fluid Framework client packages, including in Node.js.
  • CommonJS: Fluid Framework does not officially support CommonJS in version 3.0 or later.

Contribution Guidelines

You can contribute to Fluid Framework in these ways:

For detailed instructions, read the repo documentation.

This project follows the Microsoft Open Source Code of Conduct. For more information, read the Code of Conduct frequently asked questions. For questions or comments, contact [email protected].

This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services. Use of these trademarks or logos must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.

Help

Read the Fluid Framework documentation for information about Fluid Framework concepts and APIs.

To request information that the documentation does not contain, create an issue.

Trademark

This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.

Use of these trademarks or logos must follow Microsoft's Trademark & Brand Guidelines.

Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.

版本与发布记录

@fluidframework/driver-web-cache 共发布 480 个版本,首次发布于 2022/03/19;过去 12 个月发布了 48 个版本。

版本发布日期距今
3.12.0latest2026/11/114 天前
3.11.02026/11/0411 天前
3.10.02026/11/0412 天前
3.7.02026/10/2719 天前
3.6.02026/10/2026 天前
3.5.02026/10/131 个月前

其他发布通道:next → 2.0.0-dev-rc.1.0.0.225277,canary → 2.0.0-internal.8.0.8,dev → 2.116.0-416006,rc → 2.118.1。

关于 @fluidframework/driver-web-cache 的常见问题

@fluidframework/driver-web-cache 是什么?

Implementation of the driver caching API for a web browser

如何安装 @fluidframework/driver-web-cache?

在项目目录运行 npm install @fluidframework/driver-web-cache,或使用 pnpm add / yarn add / bun add。

@fluidframework/driver-web-cache 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluidframework/driver-web-cache 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fluidframework/driver-web-cache 可以商用吗?

@fluidframework/driver-web-cache 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fluidframework/driver-web-cache 的下载量有多少?

根据 npm 官方下载统计,@fluidframework/driver-web-cache 在 2026/10/15 – 2026/11/13 这 30 天内被下载 4,154 次,日均约 138 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluidframework/driver-web-cache 还在维护吗?

最新版本 3.12.0 发布于 2026/11/11(4 天前);至今共发布 480 个版本,其中过去 12 个月发布了 48 个;首次发布于 2022/03/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。