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

cache-with-resolver

v1.0.0

Published

Function based cache storage built around Map-like object.

Downloads

3

Readme

Coverage Status

Cache with Resolver

Function based cache storage built around Map-like object.

Typed for TypeScript

npm install --save cache-with-resolver

Why?

Usually when working with cache storages, the storage itself operates only with get and set methods. This storage can also operate with functions.

Getting started

CachedControl constructor can be passed config object containing default TimeToLive (otherwise 3600 seconds if not specified in resolve method) and custom Map-like object (has to implement Map's methods and properties). Custom Map can be used to share cahces between multiple CacheControls.

Methods resolve and resolveSync take up to three arguments key, function to resolve and time to live . First two arguments are mandatory.

// ES6
import CacheControl from 'cache-with-resolver';
// CommonJS
const CacheControl = require('cache-with-resolver').default;

let cacheControl = new CacheControl();

...

async function GetUserFromDb(id) {
	return cacheControl.resolve('user' + id, cache => {
		db.query(...).then(userObject => {
			cache(userOBject);
		});
	})
}

Function that contains code to be cached follows similar pattern to function passed to Promise constructor. It is given one argument, callback function called cache that is passed a value to be cached. This allows to encapsulate logic in Promise-like fashion.

As long as cached value doesn't pass it's time to live, passed function wont be invoked, instead cached value will be directly returned.

Whenever a cached object is to be retunred, it's deep cloned in order to prevent mutation.

TimeToLive is always in seconds.

resolve method return Promise that resolves to cached value. Promise is returned as async functions may be invoked inside passed function. resolveSync return cached value directly thus using async functions inside passed function will lead to unwanted behaviour.

Reference

CacheControl

Properties

  • length - getter, number of alive caches
  • entries - getter, array of tuples [key, value] of alive caches

Methods

  • drop() - drop all caches
  • delete(key) - delete cache of the key
  • filteredDelete({ olderThan?: Date, newerThan?: Date}) - delete chaces based on creation time
  • has(key) - returns true if cache of the key exists, otherwise false
  • get(key) - returns cached value or null (if doesnt exist)
  • set(key, value, timeToLive) - creates new cache
  • resolve(key, func: (cs: (val: any) => void) => void, timeToLive) => Promise - returns cahced value or runs function and create new chace
  • resolveSync(key, func: (cs: (val: any) => void) => void, timeToLive) => any - returns cahced value or runs function and create new chace

CacheControlConfig

  • timeToLive - default duration of the cache in seconds
  • cacheMap - custom Map-like object to store caches in