Microsoft Graph Toolkit缓存Microsoft Graph Toolkit caching

Microsoft Graph Toolkit支持缓存选择 Microsoft Graph API 调用。The Microsoft Graph Toolkit supports caching of select Microsoft Graph API calls. 目前,对用户、人员、联系人和照片终结点的调用默认缓存在三个 IndexedDB 存储中。Currently, calls to the users, person, contact, and photo endpoints are cached by default in three IndexedDB stores.

您可以在开发人员面板上查看缓存。You can view the cache on the developer panel. "应用程序"****选项卡上的" 存储"窗格中,转到 "IndexedDB" 选项卡。On the Application tab, in the Storage pane, go to the IndexedDB tab.

devtools indexedDB

缓存配置Cache configuration

可以通过静态类对象读取和写入缓存 CacheService.config 选项。You can read and write the cache options through the static class CacheService.config object. 它的格式如下所示。It is formatted as shown.

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
  },
};

在 config 对象中,单个缓存无效时段默认为 null defaultInvalidationPeriod 3,600,000 毫秒,默认值为 60 (60 分钟) 。Individual cache invalidation periods are defaulted to null in the config object, and default to the general defaultInvalidationPeriod value of 3,600,000 ms (60 minutes). 传入的任何值 config.x.invalidationPeriod 都将替代 defaultInvalidationPeriodAny value passed into config.x.invalidationPeriod will override defaultInvalidationPeriod.

状态存储是唯一的例外,其默认值为 300000 毫秒(即 5 分钟)。The presence store is the only exception, and has a default value of 300000 ms, or 5 minutes.

示例Examples

若要单独禁用存储,只需将存储中的配置属性的值设置为 isEnabled false:To individual disable a store simply set the value of isEnabled in that store's config properties to false:

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

CacheService.config.users.isEnabled = false;

禁用缓存不会 清除 缓存。Disabling the cache does not clear the cache.

更改验证期类似:Changing the invalditation period is similar:

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

CacheService.config.users.invalidationPeriod = 1800000;

清除缓存Clearing the cache

当用户退出时,将自动清除缓存。也可以手动清除它。The cache is automatically cleared when the user signs out. It can also be cleared manually.

清除缓存中所有存储时,类的方法将清除 CacheService 维护的所有 clearCaches() CacheService 存储。The clear all the stores in the cache, the clearCaches() method of the CacheService class will clear every store maintained by the CacheService.

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

CacheService.clearCaches();

创建自己的缓存存储Creating your own cache stores

如果要为自定义组件创建和填充自己的缓存存储,可以使用 CacheService 静态类。If you want to create and populate your own cache stores for your custom components, you can use the CacheService static class.

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

注意: 调用 storeName 中引用的项 getCache() 必须与对象中列出的存储之一 CacheSchema 匹配。Note: The storeName you reference in the call to getCache() must match one of the stores listed in your CacheSchema object.

该对象 CacheSchema 是键/值对的字典。The CacheSchema object is a dictionary with the key/value pairs.

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

以下示例演示缓存实现。The following example shows the cache implementation.

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) });
}