0.2.1-rc.4 • Published 7 months ago

@erkoware/foalts-cached-disk v0.2.1-rc.4

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

CachedDisk

This is a simple caching layer for FoalTS Disk. It stores files in local storage after they are first accessed. It keeps track of the cached files and the cache size in a sqlite database.

BE AWARE

If a file is modified and already cached, the cache is not updated. This means that the file will be read from the cache and not from the disk.

This also applies if a file is deleted without using the delete method of the CachedDisk.

How it works

The cache is a simple LRU cache. It stores the files in a local directory. The cache size is limited by the cacheSize option. The cache is not updated when the file is modified. One first call to read the file is copied to the cache directory and the cache size is updated. Information about the file is stored in the sqlite database. On subsequent calls the file is read from the cache directory. When the cache size is exceeded the least recently used file is removed from the cache, until the cache size is below 75% of cacheSize again.

Features

  • Cache files in local storage

Non-features

  • Update cache when files are modified
  • Cache files on upload
  • Cache lifetime
  • Doesn't track file hashes

Usage Example

We are using a rather slow S3 Service to store our files. And wrote this disk to improve the access times for frequently accessed files. While still keeping our application stateless.

Usage

Prerequisites

read the documentation from FoalTS on File Storage.

npm install --save @erkoware/cached-disk-foal

As a specific disk

export class CachedLocalDisk extends CachedDisk<LocalDisk> {
    @dependency
    disk: LocalDisk;
}
class FooController {
    @dependency
    cachedDisk: CachedLocalDisk;

    async bar() {
        const { file } = await this.disk.read('path/to/file');
        // ...
    }
}

As default disk

// driver: app/services/cached-local-disk.service.ts
export class CachedLocalDisk extends CachedDisk<LocalDisk> {
    @dependency
    disk: LocalDisk;
}

export { CachedLocalDisk as ConcreteDisk }
// config/default.js
settings: {
    disk: {
        driver: "./app/services/cached-local-disk.service",
    }
}
class FooController {
    @dependency
    disk: Disk;

    async bar() {
        const { file } = await this.disk.read('path/to/file');
        // ...
    }
}

For more information, see the documentation.

Options

OptionTypeDefaultDescription
maxSizenumber1000000000The maximum size of the cache in bytes.
reduceSizenumber0.75 * maxSizeThe size to reduce the cache to when it exceeds maxSize.
directorystring-The path to the cache directory.
dbNamestringcache.dbThe name of the sqlite database.

Roadmap

  • Warmup cache on first start
0.2.1-rc.9

7 months ago

0.2.1-rc.7

7 months ago

0.2.1-rc.6

7 months ago

0.2.1-rc.5

7 months ago

0.2.1-rc.4

7 months ago