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

@ehacke/transparent-cache

v1.0.2

Published

Simple transparent caching for Node. Wrap a function and then call it like normal

Downloads

14

Readme

transparent-cache

npm CircleCI GitHub Codecov

Simple transparent caching for Node. Wrap a function and then call it like normal.

Sponsor

asserted.io

asserted.io - Test in Prod

Features

  • The cache is periodically updated in the background without blocking the primary call. So it's always fast.
  • Just wrap any function and it becomes cached
  • Includes both local LRU cache and Redis

Install

This has a peer-dependency on ioredis

npm i -S @ehacke/transparent-cache ioredis

Use

In the most basic case, you can just supply the redis config, and then wrap the function.

The redisConfig option is passed directly to ioredis and has the same properties as its constructor.

import { TransparentCache } from '@ehacke/transparent-cache';

const transparentCache = new TransparentCache({ redisConfig: { host: 'localhost', port: 6379 } });

function someSlowAndExpensiveFunction(someCoolArg) {
  // ... do things you want to cache
}

// By default, the wrapped version will cache results for 30 sec locally and 10 minutes in Redis
const wrappedVersion = transparentCache.wrap(someSlowAndExpensiveFunction);

// No cache yet, call original function. Still slow.
let result = await wrappedVersion('foo');

// Immediately call again...

// Returns value from local cache for arg 'foo'
result = await wrappedVersion('foo'); 

// .... waiting two minutes ....

// Nothing in the local cache, so it goes to Redis
result = await wrappedVersion('foo');

// ... waiting 15 minutes ...

// Nothing in local or Redis, so it calls the original function
result = await wrappedVersion('foo');

// Clear caches locally and remotely for 'foo'
// NOTE: This doesn't clear local caches on other nodes
await wrappedVersion.delete('foo');

// Hits original again because caches were cleared
result = await wrappedVersion('foo');

API

Constructor

During construction, the default values for each wrap call are set.

The local and remote properties are used as the default for each wrap call, but can be overridden.

interface ConstructorInterface {
  redis?: Redis;                // Instead of providing the config below, you can provide a constructed IORedis instance
  redisConfig?: RedisOptions;   // Same as: https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
                                // Defaults to: { keyPrefix: 'trans-cache-', enableOfflineQueue: false }
                                //    This namespaces the keys, and ensures the redis calls fail fast if disconnected
  local?: {
    size?: number;              // Defaults to 1000
    ttlMs?: number;             // Defaults to 30 seconds
  };
  remote?: {
    size?: number;              // Defaults to 10000
    ttlMs?: number;             // Defaults to 10 minutes
    commandTimeoutMs?: number;  // Redis command timeout. Defaults to 50 ms
  };
}

Wrap

import { TransparentCache } from '@ehacke/transparent-cache';

const transparentCache = new TransparentCache({ redisConfig: { host: 'localhost', port: 6379 } });

transparentCache.wrap<TypeOfReturn>(
  functionToWrap,                 // Any function. Promises are awaited
  overrideConfig: {               // Defaults to constructor values 
    local?: {
      size?: number;              // Defaults to 1000
      ttlMs?: number;             // Defaults to 30 seconds
    };
    remote?: {
      size?: number;              // Defaults to 10000
      ttlMs?: number;             // Defaults to 10 minutes
      commandTimeoutMs?: number;  // Redis command timeout. Defaults to 50 ms
    };
    waitForRefresh?: boolean;     // Normally cache refreshes are done in the background. This forces them to block.
  }, 
  functionId,                     // Used as part of the cache key, defaults to the function name if present
  that,                           // Reference to 'this' for the wrapped function, used with .apply()
)

The wrapped version of the function also exposes a .delete() property that can be called to wipe the cache for a specific set of args.

// Clear caches locally and remotely for 'foo'
// NOTE: This doesn't clear local caches on other nodes
await wrappedVersion.delete('foo');