0.9.3 • Published 7 months ago

file-computed v0.9.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

动机

有时候我们的计算在特定目标文件不变时结果是一样的,当计算特别耗时复杂时可以通过本地文件缓存的方式快速得到原先的结果,而不需要每一次都进行该计算,以此提高运算效率。

使用

安装

pnpm i file-computed

基础

import { createFsComputed } from 'file-computed'

const fsComputed = createFsComputed()

const result = await fsComputed('package.json', () => {
	/** 模拟复杂计算,只会跑一次,后边会直接获取缓存中的结果 */
	let n = 0
	let t = 10000
	while (t--) {
		n++
	}
	return n
})

result // 10000

缓存路径

import { createFsComputed } from 'file-computed'

const fsComputed = createFsComputed({
	cachePath: 'temp' // 默认为最近 node_modules 的 .cache/.file-computed
})

同步

import { createFsComputedSync } from 'file-computed'

const fsComputed = createFsComputedSync()

const result = fsComputed('package.json', () => {
	/** 模拟复杂计算,只会跑一次,后边会直接获取缓存中的结果 */
	let n = 0
	let t = 10000
	while (t--) {
		n++
	}
	return n
})

result // 10000

适合大型缓存

import { createFsComputedWithStream } from 'file-computed'

const fsComputed = createFsComputedWithStream()

const result = await fsComputed('package.json', () => {
	/** 模拟复杂计算,只会跑一次,后边会直接获取缓存中的结果 */
	let n = 0
	let t = 10000
	while (t--) {
		n++
	}
	return n
})

result // 10000

后置写

开启 post,结果将优先返回,速度更快

import { createFsComputedWithStream } from 'file-computed'

const fsComputed = createFsComputedWithStream({
	post: true
})

const result = await fsComputed('package.json', () => {
	let n = 0
	let t = 10000
	while (t--) {
		n++
	}
	return n
})

result // 写被后置了,结果 10000 优先返回

因为写被后置了,所以下次计算前,缓存与否是不确定的

License

Made with markthree

Published under MIT License.

0.9.3

7 months ago

0.9.0

1 year ago

0.7.2

1 year ago

0.8.0

1 year ago

0.7.1

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

0.7.0

1 year ago

0.3.9

1 year ago

0.3.10

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.3.6

1 year ago

0.6.2

1 year ago

0.3.5

1 year ago

0.3.8

1 year ago

0.5.0

1 year ago

0.4.1

1 year ago

0.3.2

1 year ago

0.1.4

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.2.2

1 year ago

0.6.1

1 year ago

0.5.2

1 year ago

0.3.4

1 year ago

0.6.0

1 year ago

0.5.1

1 year ago

0.3.3

1 year ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.0

2 years ago