ota-file-cache
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

功能

多端读取文件时,作为缓存使用。如A、B、C三端同时都在请求文件file.int内容, 那么file.ini只会在内存加载一次

实现

FileCache实例存在两个私有对象
private fileCache: Map<number, any>; // 文件缓存, 多端请求同一文件时,避免重复读取, 占用内存
private fileCnts: Map<number, number>; // 引用计数, 大于0时说明存在引用,直接读取缓存 fileCache保存文件buffer, fileCnts保存文件引用计数,即当前有多少人在使用文件,每次使用完,记得执行reduceCount( id:number )减少引用计数,当引用计数<=0时, 文件从缓存清除。

使用方法

import { FileCache } from "ota-file-cache";

// 创建缓存实例 let fc = new FileCache(); // 读取文件 const f = fc.getFile( 1, "./file.int" ); // 其他需要使用文件的业务, 如截取、传输等。 ··· // 文件使用完毕,减少引用计数 fc.reduceCount( 1 );

Readme

Keywords

none

Package Sidebar

Install

npm i ota-file-cache

Weekly Downloads

12

Version

1.1.4

License

ISC

Unpacked Size

7.66 kB

Total Files

6

Last publish

Collaborators

  • tmztliusf