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

@yakiyo/cache-js

v2.1.1

Published

Minimal implementation of a cache using javascript Map

Downloads

3

Readme

Cache JS

About

Minimal implementation of a cache using javascript's Map. Lightweight and easy to use. Includes typescript typings.

Installation

Just install the package

$ npm install @yakiyo/cache-js
# or 
$ yarn add @yakiyo/cache-js
# or
$ pnpm add @yakiyo/cache-js

Usage

Import the package in your file and initialize the class

// Es6 imports
import Cache from '@yakiyo/cache-js';

// Commonjs require
const Cache = require('@yakiyo/cache-js'); 

const cache = new Cache<k, v>(60);

The constructor takes a number which indicated the default duration in seconds upto which the cache should remain valid. You can also set duration per entry with the add method. The default is 0 if not specified while initiating.

Methods

The Cache class extends javascript's base Map class, so every method of Map can be used on instances of the cache class too.

valid(key: k): boolean

Checks if a key is valid or not based on its duration.

cache.valid('key-name'); // true or false

add(key: k, value: v, duration?: number): this

Adds a new entry to the cache. Duration is the time in seconds till which the entry should be valid. If unspecified, it uses the default duration specified while initiating the class. If even that is set to 0, the entry remains forever valid.

cache.add('key', 'value', 20); // this

addMany(...entires: Array<[K, V]>): this

Adds multiple entries to the cache. The duration is the default duration set during initialization. It takes multiple arguments, each of them being an array. The array being composed to two elements, the first one being the key, the second one being the value

cache.addMany(['k1', 'v1'], ['k2', 'v2'], ['k3', 'v3']) // this

fetch(key: k): value | undefined

Fetches a value from the cache. Returns undefined if the key doesn't exist or if the key expires. If the key expires, it is internally deleted.

cache.fetch('key'); // 'value'
cache.fetch('non-existent-key'); // undefined
cache.fetch('expired-key'); // undefined

remove(key: k): this

Removes an entry from the cache

cache.remove('key'); // this

removeMany(...keys: Array<K>): this

Removes multiple entries from the cache. It takes multiple arguments, each of them being a key, the key of the entry to remove

cache.removeMany('key 1', 'key 2', 'key 3') // this

sweep(): this

Clears all expired entries from the cache.

cache.sweep(); // cache

toArray(): Array<V>

Returns an array of all the values of cache

cache.toArray(); // ['value 1', 'value 2']

toMap(): Map<K, V>

Returns a new map of the key value pairs in the cache

cache.toMap() // [Map Object]

Side-note

This project is inspired by @discordjs/Collection

Author

Cache-js © Yakiyo. Authored and maintained by Yakiyo.

Released under MIT License