Giter Club home page Giter Club logo

file-cache's Introduction

file-cache

Read file with LRU caching, rely on thunks.

NPM version Build Status Downloads

Demo

const FileCache = require('file-cache')
const fileCache = FileCache('./')

fileCache('index.js')(function (err, file) {
  console.log(err, file)
  // { path: '/Users/zensh/git/toajs/file-cache/index.js',
  // dir: '/Users/zensh/git/toajs/file-cache',
  // name: 'index.js',
  // ext: '.js',
  // type: 'application/javascript',
  // size: 4375,
  // atime: 'Sun, 30 Nov 2014 03:38:25 GMT',
  // mtime: 'Sun, 30 Nov 2014 03:31:30 GMT',
  // ctime: 'Sun, 30 Nov 2014 03:31:30 GMT',
  // birthtime: 'Tue, 23 Sep 2014 01:13:26 GMT',
  // compress: 'origin',
  // contents: <Buffer 27 75 73 65 20 73 74 72 69 63 74 27 3b 0a 2f 2f 20 2a 2a 47 69 74 68 75 62 3a 2a 2a 20 68 74 74 70 73 3a 2f 2f 67 69 74 68 75 62 2e 63 6f 6d 2f 74 6f ... >,
  // length: 4375,
  // md5: '08f89d75eec2731ea6612ada474a1795' }

  fileCache('index.js', 'gzip')(function (err, file) {
    console.log(err, file)
    // read file from cache, compress with gzip:
    //
    // { path: '/Users/zensh/git/toajs/file-cache/index.js',
    // dir: '/Users/zensh/git/toajs/file-cache',
    // name: 'index.js',
    // ext: '.js',
    // type: 'application/javascript',
    // size: 4375,
    // atime: 'Sun, 30 Nov 2014 03:48:47 GMT',
    // mtime: 'Sun, 30 Nov 2014 03:48:35 GMT',
    // ctime: 'Sun, 30 Nov 2014 03:48:35 GMT',
    // birthtime: 'Tue, 23 Sep 2014 01:13:26 GMT',
    // compress: 'gzip',
    // contents: <Buffer 1f 8b 08 00 00 00 00 00 00 03 95 58 4b 73 db 36 10 be eb 57 30 97 90 74 64 ca e9 34 17 a9 69 a7 75 d3 36 33 c9 a4 d3 26 27 47 07 8a 04 45 24 14 c1 00 ... >,
    // length: 2576,
    // md5: '33a6f550613d64dc6b7e1e2d99d568bd' }
  })
})

Install

npm install file-cache

API

const FileCache = require('file-cache')
const fileCache = FileCache('./static')

FileCache(options)

Return a read file function with cache.

options

Optional, Type: String or Object

options.root

Optional, Type: String, Default: process.cwd().

File directory, that allow to read file.

options.extraFiles

Optional, Type: Array<String>, Default: [].

Extra files path, that allow to read.

options.compress

Optional, Type: Boolean, Default: true.

Allow to compress file with gzip or deflate

options.md5Encoding

Optional, Type: String, Default: base64.

The MD5 encoding can be 'hex', 'binary' or 'base64'.

options.maxCacheLength

Optional, Type: Number, Default: 0.

The maximum length of the files cache in bytes. if cache's size > maxCacheLength, then the least recently used file will be removed. if maxCacheLength === -1, cache will not be used. if maxCacheLength === 0, there is no limit.

options.minCompressLength

Optional, Type: Number, Default: 256.

The minimum length of the files size in bytes that could be compressed.

fileCache(path, encodings)

Return thunk function.

path

Required, Type: String

File path to read.

encodings

Optional, Type: String or Array

Compress encoding, ['gzip', 'deflate'].

file-cache's People

Contributors

zensh avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.