Git Product home page Git Product logo

timed-cache's Introduction








Cache storage

Build Status CodeFactor DeepSource

A minimalist time-based caching system.

This storage module evicts cached key/value pairs based on their time-to-live.

Current version: 2.0.0

Install

npm install --save timed-cache

Usage

Import the cache module

timed-cache is distributed as an ESM module that you can import in your implementation.

import Cache from 'timed-cache';

Creating the cache module

Basic operations you can perform on an instance of a Cache are insertion, retrieval and removal of key/value pairs.

To do so, you will need to create a new instance of the cache, by calling its constructor :

const cache = new Cache();

Note that by default, a key/value pair will be held by the cache storage for 60 seconds before being evicted.

It is however possible to specify what default value you would like the TTL to have when creating the storage :

// The TTL is always expressed in milliseconds.
// In this case it will be equal to `5` minutes.
const cache = new Cache({ defaultTtl: 300 * 1000 });

You will then be able to interact with the storage by retrieving and inserting data.

Basic insertions

You insert a key/value pair into the storage by using the .put primitive and retrieve a value given its key identifier using the .get primitive.

Here is an example of inserting values associated with a string key :

cache.put('bar', 'baz');
cache.put('foo', { foo: 'bar' });
cache.put('qux', 42);

It is then possible to retrieve the cached values using their associated keys :

cache.get('bar'); // Returns 'baz'
cache.get('foo'); // Returns the object { foo: 'bar' }

It is also possible to use an object as a key as long as it is serializable using JSON.stringify :

cache.put({ foo: 'bar' }, { bar: 'baz' });
cache.get({ foo: 'bar' }); // Returns the object { bar: 'baz' }

Note that inserting a value already associated with the inserted key will cause the previous value to be overwritten, and the TTL to be reset.

Customizing elements TTL

You can customize the time-to-live value of a key/value pair at insertion time using the third optional argument to .put :

// Example of an insertion using a TTL expressed in milliseconds.
cache.put('foo', 'bar', { ttl: 5 * 1000 });

It is also possible to define a callback for each inserted key/value pair to be informed when it is actually evicted from the storage :

cache.put('baz', 'bar', {
  ttl: 5 * 1000,
  callback: (key, value) => console.log(`${key} ${value} evicted !`)
});

Element removal

It is possible to remove a cache entry before its time-to-live is reached, by using the .remove primitive :

cache.put('foo', 'bar', {
 callback: (key, value) => console.log(`${key} ${value} removed !`)
});

cache.remove('foo');

In this case, the callback passed to a .put will be called if the user removed the inserted entry.

License

FOSSA Status

timed-cache's People

Contributors

deepsourcebot avatar dependabot[bot] avatar fossabot avatar greenkeeper[bot] avatar hqarroum avatar vasco-santos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

timed-cache's Issues

An in-range update of grunt is breaking the build 🚨

Version 1.0.3 of grunt was just published.

Branch Build failing 🚨
Dependency grunt
Current Version 1.0.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

grunt is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 10 commits.

  • 9ba3a99 1.0.3
  • eee4c33 Changelog v1.0.3
  • 46da7f2 Merge pull request #1636 from gruntjs/upt
  • 00f4d8a Drop support for Node 0.10 and 0.12
  • e852727 util update
  • 56d702e Update deps
  • 0105524 Fix race condition with file.mkdir and make it operate more similarily to mkdir -p (#1627) r=@vladikoff
  • 303d445 https links (#1629)
  • d969132 Merge pull request #1624 from gruntjs/rm-bump-deps
  • 289ff91 Remove old bump task and deps

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of load-grunt-tasks is breaking the build 🚨

The devDependency load-grunt-tasks was updated from 5.0.0 to 5.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

load-grunt-tasks is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v5.1.0
  • Print a useful error if package.json is missing (#61) 6234bc9

v5.0.0...v5.1.0

Commits

The new version differs by 2 commits.

  • fddcf81 5.1.0
  • 6234bc9 Print a useful error if package.json is missing (#61)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of grunt-contrib-uglify is breaking the build 🚨

Version 3.4.0 of grunt-contrib-uglify was just published.

Branch Build failing 🚨
Dependency grunt-contrib-uglify
Current Version 3.3.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

grunt-contrib-uglify is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 2 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Browser tests

While fixing the browser implementation on #27 I noticed that there is no browser tests.
It would be good to extend the available tests to other environments

Add support for autoload

I would like to have an option to tell Cache to load some data in case it's missing when retrieving data with get() method.

Cache.get(key, () => { return doLoading(); }

The result of doLoading() should be saved to cache by key

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.