npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kth/in-memory-cache

v0.1.53

Published

0.1.1 Basic in memory cache for primitives and objects.

Downloads

140

Readme

In-memory Cache Continous Integration

Npm: @kth/in-memory-cache

This is how you use the in-memory cache. It is a very basic iplementation.


const cache = require("@kth/in-memory-cache");

const user = { firstName: "John", lastname: "Doe" }

cache.add('user-id', user);

if (cache.isValid('user-id')) {
    console.log('Users lastname is ' + cache.get('user-id').lastname);
}

cache.remove('user-id');

TTL - Set the time an item is valid

If a cache item should live for a long time you can set a time to live (TTL) on it in milliseconds on the item.

const cache = require('@kth/in-memory-cache');

const aDay = 1000 * 60 * 60 * 24;
const user = { firstName: 'John', lastname: 'Doe' }

cache.add('long-caching-object', user, aDay);

Limit the number of items in the store

Sometimes you whould like to limit the number of items that are stored in the cache. This is not a FIFO, if there is space, the item will be added. Otherwise silently ignored.

const cache = require('@kth/in-memory-cache');

cache.setMaxSlots(2)

cache.add('key-1', 'value 1');
cache.add('key-2', 'value 2');
cache.add('key-3', 'value 3'); // not added, with logger.info(str) message

cache.length(); // 2

Use your own logger

Your project probably already have a logging framework, you can pass it to the cache. The default logger is console.

const cache = require('@kth/in-memory-cache');
const log = require('someLogger')

// The logger needs to implement the function info(string)
cache.setLogger(log);

Run tests

To run the tests, do npm magic or run ./build.sh. You can also view the tests at https://travis-ci.org/KTH/in-memory-cache

npm install
npm test
  Cache - Add
    ✓ 'Null' is an acceptable cache item value.
    ✓ 'Undefined' is an acceptable cache item value.
    ✓ When no TTL is passed, the default 1000ms is used as the time for an item to live.
    ✓ Use a specific TTL for an item to live.
    ✓ When a max slot is set no more items are added, until old ones are removed.

  Cache - Get
    ✓ It is possible to store and get a string.
    ✓ It is possible to store and get a number.
    ✓ It is possible to store and get an object.
    ✓ If the TTL has expired, 'undefined' will be returend for the item key.
    ✓ If there is no matching item in the store 'undefined' will be returned.

  Cache - Remove
    ✓ It is possible to remove one specific item in the cache.
    ✓ It is possible to remove all items in the cache.
    ✓ Nothing happens if you remove an item that is not in the store.

  Cache - Number of slots
    ✓ It is possible to get the numer of items in the cache.
    ✓ It is possible to set the maximum number of items in the cache.

  Cache - logger
    ✓ It is possible to set an external logger.