@ file-cache/ core (帮助)
文件元数据或文件内容的缓存 .
安装
npm install @file-cache/core
使用量
import { createCache } from '@file-cache/core';
// use metadata starategy to detect changes
const cache = await createCache({
name: "your-tool-name",
keys: [
() => "test",
() => "custom"
],
mode: "metadata"
});
const filePath = path.resolve(__dirname, "./fixtures/f1.txt");
const result = await cache.getAndUpdateCache(filePath);
assert.deepStrictEqual(result, {
changed: true,
error: undefined
});
// write the updates to cache file
await cache.reconcile();
// re-get the file status
const result2 = await cache.getAndUpdateCache(filePath);
assert.deepStrictEqual(result2, {
changed: false,
error: undefined
});
您可以禁用缓存 noCache 选项.
import { createCache } from '@file-cache/core';
// use metadata starategy to detect changes
const cache = await createCache({
name: "your-tool-name",
keys: [
() => "test",
() => "custom"
],
mode: "metadata",
noCache: true,
});
const filePath = path.resolve(__dirname, "./fixtures/f1.txt");
const result = await cache.getAndUpdateCache(filePath);
assert.deepStrictEqual(result, {
changed: true,
error: undefined
});
await cache.reconcile();
// always, the result.changed is true
const result2 = await cache.getAndUpdateCache(filePath);
assert.deepStrictEqual(result2, {
changed: true,
error: undefined
});
选项
export type CreateCacheOptions = {
/**
* The name of your tool
*/
name: string;
/**
* - content: Using the hash value of file content
* - Slow but accurate
* - metadata: Using the metadata of file
* - Fast but not accurate
* - It can not be used in CI env
*/
mode: "content" | "metadata";
/**
* The key generators for cache file
*/
keys: CreateCacheKeyGenerator[];
/**
* Custom cache directory.
* Default: node_modules/.cache/<pkg-name>
*/
cacheDirectory?: string;
};
测试
npm test
捐款
- 叉开它!
- 创建您的特性分支 :
git checkout -b my-new-feature - 提交更改 :
git commit -am 'Add some feature' - 推到树枝上:
git push origin my-new-feature - 提交拉动请求 : D
许可证
麻省理工学院