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

@sapphirejs/cache

v0.0.13

Published

Caching for Sapphire Framework

Downloads

1

Readme

Cache

A simple, but effective cache system that supports different drivers and promises. It currently has a Redis transport, which should be good for almost any usecase, and a memory transport mostly for testing.

Usage

Install and initialize the cache with a Redis transport.

$ npm install --save @sapphirejs/cache
const { Cache, Transport } = require('@sapphirejs/cache')

const cache = new Cache(new Transport.Redis({ host: '127.0.0.1', port: 6379 }))
await cache.set('name', 'sapphire')
const value = await cache.get('name')

By using await it's a breeze to work with. Just keep in mind that you'll need an async function to use await, something omitted for brevity. Redis configuration can be set according to these options on the Redis client. For a local setup, they'll most probably work out of the box.

The Redis transport automatically executes JSON.stringify() when setting values, and JSON.parse() when retrieving them. There's no need to call them manually.

API

A list of the supported methods:

get(key) Retrieve key from the cache or null if it doesn't exist.

set(key, value, minutes = 60) Set the key to value with an expiry of minutes.

forever(key, value) Set the key to value with a big expiry time.

increment(key, amount = 1) Increment the number value under key with amount.

decrement(key, amount = 1) Decrement the number value under key with amount.

override(key, value, minutes = 60) Set the key to value only if it already exists in the cache.

add(key, value, minutes = 60) Set the key to value only if it doesn't already exist.

pop(key) Retrieve key and delete it.

has(key) Check if key exists in the cache.

delete(key) Delete key from the cache.

flush() Delete all the keys from the cache.

Custom Transports

Transports are classes that need to implement most of the methods above and wrap the return value into a promise. Let's imagine you need to build a Memcached driver. It should implement all the following methods:

class MemcachedTransport {
  get(key) {}
  set(key, value, minutes) {}
  increment(key, amount) {}
  decrement(key, amount) {}
  has(key) {}
  delete(key) {}
  flush() {}
}

Testing

A Memory transport is provided to ease testing without the need to hit Redis or write mocks for it. Just inject that instead of the actual transport you're using. An example in Jest:

const cache = new Cache(new Transport.Memory())
cache.set('name', 'sapphire')
expect(cache.get('name')).toBe('sapphire')