Skip to main content
Version: 6.4

元数据缓存

在 v4 及更高版本中,我们需要明确安装 @mikro-orm/reflection 才能使用 TsMorphMetadataProvider

¥In v4 and later versions, we need to explicitly install @mikro-orm/reflection to use TsMorphMetadataProvider.

MikroORM 允许使用不同的方式进行 获取实体元数据。一种方法是使用 ts-morph 读取所有实体的 TypeScript 源文件,以便能够检测所有类型。此过程可能性能高且耗时。因此,TsMorphMetadataProvider 的元数据缓存会自动启用。可以选择为其他元数据提供程序启用它,但这不是必需的。

¥MikroORM allows different ways to obtain entity metadata. One way is to use ts-morph to read TypeScript source files of all entities to be able to detect all types. This process can be performance heavy and time-consuming. For this reason, metadata cache is automatically enabled for TsMorphMetadataProvider. It can be optionally enabled for the other metadata providers, but it should not be needed.

发现过程结束后,所有元数据都将被缓存。默认情况下,FileCacheAdapter 将用于将缓存存储在 JSON 文件中的 ./temp 文件夹中。

¥After the discovery process ends, all metadata will be cached. By default, FileCacheAdapter will be used to store the cache inside ./temp folder to JSON files.

如果我们使用基于文件夹的发现,缓存将依赖于环境 - 如果我们通过 ts-node 运行,将为 TS 文件生成缓存。要生成生产缓存,我们可以使用 CLI 命令 mikro-orm cache:generate。或者,你可以生成缓存包并使用 GeneratedCacheAdapter,这允许删除对 @mikro-orm/reflection 包的生产依赖。

¥If we use folder-based discovery, cache will be dependent on environment—if we run via ts-node, the cache will be generated for TS files. To generate production cache, we can use the CLI command mikro-orm cache:generate. Alternatively, you can generate a cache bundle and use the GeneratedCacheAdapter, which allows to remove the production dependency on @mikro-orm/reflection package.

自动失效

¥Automatic Invalidation

实体元数据与源文件的修改时间一起缓存,每次请求缓存时,它首先检查缓存是否无效。这样,我们大多数时候都可以忘记缓存机制。

¥Entity metadata are cached together with modified time of the source file, and every time the cache is requested, it first checks if the cache is not invalid. This way we can forget about the caching mechanism most of the time.

一种我们最终需要手动清除缓存的情况是,当我们在实体文件夹内容不同的 git 分支中工作时。

¥One case where we can end up needing to wipe the cache manually is when we work within a git branch where contents of entities folder differs.

禁用元数据缓存

¥Disabling Metadata Cache

我们可以通过以下方式禁用元数据缓存:

¥We can disable metadata caching via:

await MikroORM.init({
metadataCache: { enabled: false },
// ...
});

漂亮打印

¥Pretty Printing

默认情况下,缓存的元数据将是一行 JSON 字符串。你可以强制漂亮地打印它:

¥By default, cached metadata will be one line JSON string. You can force pretty printing it:

await MikroORM.init({
metadataCache: { pretty: true },
// ...
});

使用不同的临时文件夹

¥Using Different temp Folder

我们可以通过以下方式设置缓存目录:

¥We can set the cache directory via:

await MikroORM.init({
// defaults to `./temp`
metadataCache: { options: { cacheDir: '...' } },
// ...
});

提供自定义缓存适配器

¥Providing Custom Cache Adapter

你还可以实现自己的缓存适配器,例如将缓存存储在 redis 中。为此,只需实现简单的 CacheAdapter 接口:

¥You can also implement your own cache adapter, for example to store the cache in redis. To do so, just implement simple CacheAdapter interface:

export interface CacheAdapter {

/**

* Gets the items under `name` key from the cache.
*/
get(name: string): Promise<any>;

/**

* Sets the item to the cache. `origin` is used for cache invalidation and should reflect the change in data.
*/
set(name: string, data: any, origin: string, expiration?: number): Promise<void>;

/**

* Clears all items stored in the cache.
*/
clear(): Promise<void>;

/**

* Called inside `MikroORM.close()` Allows graceful shutdowns (e.g. for redis).
*/
close?(): Promise<void>;

}
export class RedisCacheAdapter implements CacheAdapter { ... }

并在 cache.adapter 选项中提供实现:

¥And provide the implementation in cache.adapter option:

await MikroORM.init({
metadataCache: { adapter: RedisCacheAdapter, options: { ... } },
// ...
});