大网数据广告活动
Microsoft Graph 接口

Microsoft Graph Toolkit缓存

· 52CE 我爱测

解释缓存的工作原理以及如何配置提供给开发人员的选项

Microsoft Graph Toolkit支持缓存选择 Microsoft 图形 API调用。 呼叫按实体(如人员、联系人、照片)进行缓存。 这允许一个组件检索数据和其他组件以重复使用它,而无需调用 Microsoft Graph。

Tip: 有关每个组件缓存哪些实体的信息,请参阅组件的文档。

由 mgt 创建的用于缓存的数据库的前缀为 mgt-。 每个实体的数据都存储在单独的对象存储中。 若要检查缓存,请使用开发人员面板中的"应用程序"选项卡 (F12 工具) - 在 "存储" 部分下,单击"IndexedDB"选项卡。

devtools indexedDB

缓存配置

可以通过静态类对象读取和写入缓存 CacheService.config 选项。 它的格式如下所示。

let config = {
  defaultInvalidationPeriod: number,
  isEnabled: boolean,
  people: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  photos: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  users: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  presence: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  groups: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  response: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  files: {
    invalidationPeriod: number,
    isEnabled: boolean
  },
  fileLists: {
    invalidationPeriod: number,
    isEnabled: boolean
  }
};

null在 config defaultInvalidationPeriod 对象中,单个缓存无效时段默认为 ,默认为 60 分钟 60 分钟的常规值 3,600,000 ms (60 分钟) 。 传入的任何值都将 config.x.invalidationPeriod 替代 defaultInvalidationPeriod。

状态存储是唯一的例外,其默认值为 300000 毫秒(即 5 分钟)。

示例

若要单独禁用存储,只需 isEnabled 将存储的 config 属性的值设置为 false:

import { CacheService } from '@microsoft/mgt';

CacheService.config.users.isEnabled = false;

禁用缓存不会 清除 缓存。

更改失效期类似:

import { CacheService } from '@microsoft/mgt';

CacheService.config.users.invalidationPeriod = 1800000;

清除缓存

当用户退出时,将自动清除缓存。也可以手动清除它。

如果清除缓存中所有的存储,clearCaches()``CacheService类的 方法将清除 CacheService 维护的所有存储。

import { CacheService } from '@microsoft/mgt';

CacheService.clearCaches();

创建自己的缓存存储

如果要为自定义组件创建和填充自己的缓存存储,可以使用静态 CacheService 类。

CacheService.getCache(schema: CacheSchema, storeName: String);

注意: 调用 storeName 中引用的 必须与 getCache() 对象中列出的存储之一 CacheSchema 匹配。

对象 CacheSchema 是键/值对的字典。

import { CacheSchema } from '@microsoft/mgt';
const cacheSchema: CacheSchema = {
  name: string,
  stores: {
    store1: {},
    store2: {},
    ...
  },
  version: number
};

以下示例演示缓存实现。

import { CacheItem, CacheSchema, CacheService, CacheStore } from '@microsoft/mgt';

const cacheSchema: CacheSchema = {
  name: 'users',
  stores: {
    users: {},
    usersQuery: {}
  },
  version: 1
};

interface CacheUser extends CacheItem {
  user?: string;
}

// retrieves invalidation time from cache config
const getUserInvalidationTime = (): number =>
  CacheService.config.users.invalidationPeriod || CacheService.config.defaultInvalidationPeriod;

// checks for if cache is enabled
const usersCacheEnabled = (): boolean => CacheService.config.users.isEnabled && CacheService.config.isEnabled;

// declare the desired cache store
let cache: CacheStore<CacheUser>

// check if the cache is enabled
if (usersCacheEnabled()) {
  cache = CacheService.getCache<CacheUser>(cacheSchema, 'users');
  const user = await cache.getValue(query);

  // check if an item is retrieved, and if it's not expired
  if (user && getUserInvalidationTime() > Date.now() - user.timeCached) {
    return JSON.parse(user.user);
  }
}

// graph call
const graphRes = graph
  .api('me')
  .middlewareOptions(prepScopes('user.read'))
  .get();

// store graph result into the cache if cache is enabled
if (usersCacheEnabled()) {
  cache.putValue(userId, { user: JSON.stringify(graphRes) });
}

历史资料。此文对应 2023-02-03 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者(原始作者元数据 sebastienlevert)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / Microsoft Graph 中文文档历史快照
适用范围
历史中文快照 2023-02-03;详见原文产品版本;文档日期 未标明
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。