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

cacheiro

v0.1.3

Published

The simplest -yet effective- cache manager

Downloads

317

Readme

Cacheiro logo NPM Version NPM Downloads


cacheiro. substantivo masculino:

Porco semental.

Levar a porca ao cacheiro.


Intro

cacheiro is the simplest -yet effective- cache manager.

Install

npm install cacheiro

Getting Started

import {cacheiro} from 'cacheiro'

const options = {
  type: 'combined', // or 'memory' or 'redis'
  redis: {
    host: '127.0.0.1',
    port: 6379
  },
  namespace: 'my_cache',
  version: 1,
  clean: true,
  ttl: 86400 * 1000 * 30,
  log: 'debug'
}

const cache= await cacheiro(options)

await cache.setItem('key', 'value')
// true

await cache.hasItem('key')
// true

await cache.getKeys()
// ['key']

await cache.getItem('key')
// 'value'

await cache.unsetItem('key')
// true

await cache.getOrSetItem('key', 
  () => {
    console.log('Value is not there, let\'s create it')
    return 'value'
  } 
)
// Value is not there, let's create it
// => 'value'

await cache.unsetAll()
// 1

API

await cacheiro(options)

Creates and inits the cache store.

type

  • memory: cache stuff directly on memory. If you loose the variable, you lose the cache.
  • redis: use node-redis as caching layer. You need to pass options.redis field.
  • combined: combination of both memory and redis. You need to pass options.redis field. memory cache will act as a read-only replica of redis.

redis

Redis connection parameters. Refer to node-redis for further info.

namespace

Prefix to be used for all the cache keys managed by cacheiro. Default is 'cacheiro'.

version

Handle cached data versions to easily unvalidate previous content. Default is 1.

clean

If true, cache will be clean right after initialization. (It applies only to redis or combined). Default is false.

ttl

Expiration time in miliseconds for the cached values. They can be setted also at item level on cache.setItem(key, value, ttl). Default is 86400000, 1 day. Notice that for memory cache, ttl is handled though setTimeout. This has a limit of 32-buit integers (max ttl is 2147483647, a bit less of 25 days).

log

It can be a string with the log level (silly, debug, info, warn, error) or a class exposing methods named as those log levels, for example:

  log: class CustomLogger {
    _log(l, s) {
      console.log(`[${l}] ${s}`)
    }
    
    silly(s) { this.log('silly', s) }
    debug(s) { this.log('debug', s) }
    info(s)  { this.log('info', s) }
    warn(s)  { this.log('warn', s) }
    error(s) { this.log('error', s) }
  }
}

Default is debug.

await setItem(key, value, ttl = <ms>)

Stores a value in the cache, identified by key. If specified, ttl is the expiration time (or Time To Live) in miliseconds.

await getItem(key)

Returns, if exists, stored value for key. undefined otherwise.

await hasItem(key)

Returns true if there is some value stored for key. false otherwise.

await unsetItem(key)

Removes from cache any value stored for key. Returns true if there was some value stored. false otherwise.

await getKeys(pattern)

Returns an array of keys present in the cache and matching pattern.

await getAll(pattern)

Returns an object like {key: value...} of all the stuff present in the cache and matching pattern.

await getValues(pattern)

Returns an array of all the values present in the cache whose key is matching pattern.

await unsetAll(pattern)

Removes from cache all values matching pattern.

pattern parameter

In the case of redis or combined caches, pattern is handled by Redis.

In the case of memory cache, cacheiro will create a RegExp(pattern), unless you specify no pattern or the '*' wildcard value.

TODO

detect Redis is installed in the system

And, if not, failback to memory cache.

memory cache and ttl

Find a better expiring method than setTimeout(). Probably passing a cron through options.

memory cache and pattern

Find a beter solution than RegExp. Something closer to Redis pattern's handling.

Changelog

0.1.3

Fix logger.warning => logger.warn.

0.1.2

Limit memory cache's ttl to the max 32-bit int (2147483647). Show warning if greater value was passed.

0.1.1

Added getAll(pattern) and getValues(pattern) methods. initCache() is now cacheiro().

0.1.0

Created redis and combined stores. raw is now memory. Every method is now async. npm run test