Giter VIP home page Giter VIP logo

filru's Introduction

filru    Build Status

A pure-disk based least-recently-used (LRU) cache.

Nothing is kept in memory. This has the benefit of persisting across application restarts, and not taking up any RAM.

The cache is constrained by size (bytes), and optionally by file age.

Use Cases

filru is much slower than an in-memory cache, so use cases are:

  • a simplistic caching layer for frequent HTTP requests, since OS file system caching is much faster than doing a network request
  • caching the results of CPU-intensive operations
  • caching slow operations across multiple network requests
  • caching large items

Usage

Node.js >= version 6

npm install filru
const Filru = require('filru');
const require('request-promise-native'); // optional for loading async when not in cache
const handleErr = (err) => {
  throw err;
};

const maxBytes = 50 * 1024 * 1024; // 50 megabytes
const maxAge = 24 * 60 * 60 * 1000; // 1 day
const hashSeed = 'cache4gold'; // optional, random seed string
const pruneInterval = 1000 * 60 * 60; // optional, defaults to 1 hour
const f = new Filru({ dir: '/tmp/filru', maxBytes, maxAge, hashSeed, pruneInterval });

// optionally add a load function for when an object
// is not found in the cache
f.load = function customLoad(key) {
  // Custom async load function must return a promise.
  return request.get('https://my-site.com/' + key);
};

f.start()
  .then(() => {
    // fill the cache
    f.set('jimmy.txt', 'yo').catch(handleErr);
    f.get('jimmy.txt')
        .then((buffer) => {
          console.log('got jimmy:', buffer.toString('utf8')); // "yo"
        })
        .catch(handleErr);
    // stop cleanup job
    f.stop();
  })
  .catch(err => {
    throw err;
  });

Tests

npm test

Debugging

Run your application with DEBUG=filru*

License

MIT

See LICENSE file in the root of this directory.

filru's People

Contributors

homerjam avatar ruffrey avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

homerjam

filru's Issues

Override hash?

Hi!

This is module is super useful - I'm adding it to an asset resizing server to boost it's performance.

Would you be interested in a PR to allow specifying the hash?

In my use case I'd like to prepend an ID to the hash so that if an asset is deleted I can remove any derivatives of the asset from the cache too.

Thanks!
James

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.