1.0.0 • Published 5 months ago

express-cache-file v1.0.0

Weekly downloads
6
License
BSD-2-Clause
Repository
github
Last release
5 months ago

express-cache-file

Express middleware to cache static files in server memory.

npm Package Version

This is a simple package without external dependencies. (e.g. it doesn't require redis)

Features

  • express middleware
  • cache files in memory
  • customizable cache policy
  • customizable cache size

Installation

You can install this package with npm, pnpm, yarn or slnpm

npm install express-cache-file

Usage Example

import cacheFile from 'express-cache-file'
import express from 'express'

let app = express()

app.use(
  cacheFile('public', {
    cacheSize: '50mb',
    update: {
      expire: '5 seconds',
      mode: 'cache_first',
    },
  }),
)

app.listen(8100, () => {
  console.log('listening on http://localhost:8100')
})

Typescript Signature

export function cacheFile(
  root: string,
  options?: CacheFileOptions,
): (req, res, next) => void

export default cacheFile

export type CacheFileOptions = {
  readMode?: ReadMode
  update?: false | UpdateOptions
  cacheSize?: CacheSize
  redirect?: boolean // redirect '/' to 'index.html', default true
}

export type UpdateOptions = {
  expire?: UpdateInterval
  mode?: UpdateMode
}

export type UpdateMode =
  | 'cache_first' // default
  | 'wait'

export type ReadMode =
  | undefined
  | 'async' // default
  | 'sync'

export type UpdateInterval =
  | undefined
  | false // equivalent to to 'never'
  | 'never' // default
  | 'immediate'
  | number // ms, zero is equivalent to 'immediate'
  | string // duration format, e.g. '5 minutes'

export type CacheSize =
  | undefined
  | 'unlimited' // default
  | number // bytes
  | string // size format, e.g. '5mb'

Duration Format

Format: value

Unit PrefixMeaning
msmillisecond
ssecond
mminute
hhour
dday

Remarks:

  • The unit is case-insensitive.
  • The space between value and unit is optional.
  • When the unit is not specified, the value is treated as second if smaller than 1000, otherwise treated as ms.

Examples:

  • 10ms
  • 5 seconds

Size Format

Format: value

Unit PrefixMeaning
Bbyte
KiB1024^1 byte
MiB1024^2 byte
GiB1024^3 byte
TiB1024^4 byte
PiB1024^5 byte
KB1000^1 byte
MB1000^2 byte
GB1000^3 byte
TB1000^4 byte
PB1000^5 byte

Remarks:

  • The unit is case-insensitive.
  • The space between value and unit is optional.
  • When the unit is not specified, the value is treated as MB if smaller than 1000, otherwise treated as byte.

License

This is a Free and Open Source Software (FOSS) licensed under BSD-2-Clause