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

ricordo

v0.2.3

Published

Micro function caching / memo library for JS

Downloads

17

Readme

RICORDO

Micro caching / memo library with ttl support.

When you need some caching, and setting up Redis is too much. Ricordo is a 700 bytes library that fully supports Node.js and the browser. With time-to-live support and full control on the number of cached keys.

Table of Contents

Installation

If using npm:

npm install ricordo

If not:

<script src="https://unpkg.com/ricordo/dist/ricordo.umd.js"></script>

Usage

Ricordo usage is pretty straightforward:

  1. Create a new Ricordo instance passing a function to cache and some optional arguments.
  2. Use the function.

Caching a function

const Ricordo = require('ricordo'); // Node.js

const demoFunc = a => `hello ${a}`;

// Cache function
const cached = new Ricordo(demoFunc);

// Use function
cached('world'); // No cache hit.
cached('world'); // cache hit.

Expesive functions

const Ricordo = require('ricordo'); // Node.js

const fibonacci = n => (n < 2) ? n : fibonacci(n - 1) + fibonacci(n - 2);

// Cache function
const cached = new Ricordo(fibonacci);

// First execution
cached(40); // 1200ms

// Cached execution
cached(40); // 0ms
cached(40); // 0ms
cached(40); // 0ms

Using TTL (time-to-live)

Ricordo supports a time-to-live prop on a newly created instance. ttl define (in ms) the lifespan of the cached key. The key will be removed from cache after ttl if there are no hits to that key.

NOTE: ttl must be above 1000ms.

const Ricordo = require('ricordo'); // Node.js

// Cache function
const cached = new Ricordo(a => `hello ${a}`, {
  ttl: 5000 * 1000, // lifespan of key.
});

// ####### 1 #######
// First execution
cached('world'); // no cache hit => insertion.

// Executing function after ttl
setTimeout(() => cached('world'), 5000 * 1001); // No cache hit => New insertion

// ####### 2 #######
// First execution
cached('world'); // no cache hit => insertion.
cached('world'); // cache hit => ttl will be renewed.

// Executing function after ttl
setTimeout(() => cached('world'), 5000 * 1001); // cache hit!!! 🎉

Setting limits on number of cached keys

You can set a limit on the number of keys that can be cached. When limit n of inserted keys is hit, the cache will return to ideal n of items. The keys that are kept when limit is hit are the ideal n of items most accessed in the ttl iterval.

NOTE: A range is used so the operations of sorting and deleting the useless keys will be less frequent.

const Ricordo = require('ricordo'); // Node.js

// Cache function
const cached = new Ricordo(a => `hello ${a}`, {
  ttl: 5000 * 1000, // lifespan of key.
  ideal: 2, // Ideal number of cached keys
  limit: 4, // Max number of cached keys
});

cached('A'); // No cache
cached('B'); // No cache
cached('C'); // No cache

// Accessing keys A, B for 5 times.
[1, 2, 3, 4, 5].forEach(e => {
  cached('A'); // From cache
  cached('B'); // From cache
});

// Accessing C key for 2 times.
[1, 2].forEach(e => {
  cached('C'); // From cache
});

// On the next insertion the `limit` (4) is hit
// => Only the `ideal` (2) items that are most accessed will be kept. => (A, B)
cached('D') // New insertion

// Then...
cached('A') // From cache
cached('B') // From cache
cached('C') // No cache

Destroy cache

Every ricordo instance has a destroy method. This method will clear every cached key.

const Ricordo = require('ricordo'); // Node.js

// Cache function
const cached = new Ricordo(a => `hello ${a}`);

cached('A'); // No cache
cached('B'); // No cache

cached('A'); // From cache
cached('B'); // From cache

// Destroy every cached key.
cache.destroy();

cached('A'); // No cache
cached('B'); // No cache

Force deletion

If force flag is enabled, cached keys will be deleted after ttl. Also if there are multiple hits to that key.

const Ricordo = require('ricordo'); // Node.js

// Specifying force flag.
const cached = new Ricordo(a => `hello ${a}`, { ttl: 1000, force: true });

cached('A'); // No cache
cached('A'); // From cache
cached('A'); // From cache

setTimeout(() => cached('A'), 1001) // No cache => lifespan not renewed.

Caching React component

import React, { Component } from 'react';
import Ricordo from 'ricordo';

const C = a => <h1>hello {a}</h1>;

const cached = new Ricordo(C);

class App extends Component {

  // Invoking cached function => now 'world' is a registered key
  // The next time that the function will be computed with 'world' as argument,
  // the previously created component will be returned.
  render = () => <div>{cached('world')}</div>;
}

API

Ricordo

| param | type | required | default | spec | | ------ | -------- | -------- | --------- | ----------------------- | | func | function | yes | | Function to cache. | | config | object | no | undefined | Defines cache behavior. |

Config

| param | type | required | default | spec | | ----- | --------------- | ---------------------------------- | --------- | --------------------------------------------------- | | ttl | number [> 1000] | no | undefined | Lifespan of cached key | | ideal | number | no [yes, if limit is specified] | undefined | Ideal number of cached keys | | limit | number | no [yes, if ideal is specified] | undefined | Max number of cached keys | | force | boolean | no | false | If set to true, cache will not be renewed after ttl |

Destroy

| param | type | required | default | spec | |-------|---------|------------------------------------|-----------|------------------------------------------------------------------------------| | key | any | no | undefined | key to delete from cache. If no key is specified, every key will be deleted. |

License

MIT.