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

memory-cache-node

v1.4.0

Published

Fast, modern and event loop non-blocking memory cache for Node.js and browse

Downloads

18,617

Readme

memory-cache-node

version build coverage Quality Gate Status Bugs Vulnerabilities Downloads MIT License

A fast and type safe memory cache for Node.js and browser. memory-cache-node use Javascript Map as cache implementation which is faster than using Javascript object that some other similar libraries use. memory-cache-node also uses implementation that does not block the event loop for a long time, if the cache is very large (hundreds of thousands or millions of entries).

Table of Contents

Installation

npm install --save memory-cache-node

Usage

Creating a memory cache

Below example creates a memory cache for items which has string keys and number values. Memory cache checks expiring items every 600 seconds (i.e. every 10 minutes) The maximum number of items in the cache is 1 million.

import { MemoryCache } from 'memory-cache-node';

const itemsExpirationCheckIntervalInSecs = 10 * 60;
const maxItemCount = 1000000;
const memoryCache = new MemoryCache<string, number>(itemsExpirationCheckIntervalInSecs, maxItemCount);

Storing items in the memory cache

Below example stores a permanent item in the memory cache with key key1 and stores an expiring item with key key2. The latter item expires earliest after 30 minutes.

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);

const timeToLiveInSecs = 30 * 60;
memoryCache.storeExpiringItem('key2', 2, timeToLiveInSecs);

Getting the number of items in the memory cache

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);

const timeToLiveInSecs = 30 * 60;
memoryCache.storeExpiringItem('key2', 2, timeToLiveInSecs);
console.log(memoryCache.getItemCount()); // Logs to console: 2

Checking if an item exists in the memory cache

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);

console.log(memoryCache.hasItem('key1')); // Logs to console: true
console.log(memoryCache.hasItem('notFound')); // Logs to console: false

Retrieving the value of an item in the memory cache

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);

console.log(memoryCache.retrieveItemValue('key1')); // Logs to console: 1
console.log(memoryCache.retrieveItemValue('notFound')); // Logs to console: undefined

Getting the item expiration timestamp

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);

console.log(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key1')); // Logs some large number to console

Removing an item from the memory cache

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
memoryCache.storePermanentItem('key1', 1);
console.log(memoryCache.hasItem('key1')); // Logs to console: true
memoryCache.removeItem('key1');
console.log(memoryCache.hasItem('key1')); // Logs to console: false

Clearing the memory cache

Below examples removes all items from the memory cache

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
// Use cache here
// ...
memoryCache.clear()

Destroying the memory cache

Below example destroys the memory cache and it should not be used after that. It clears the memory cache and also removes the timer for checking expired items. NOTE! You should NEVER use a destroyed cache again! If you try to use a destroyed memory cache, an exception will be thrown. You should destroy your memory cache if it is not used in your application anymore.

import { MemoryCache } from 'memory-cache-node';

const memoryCache = new MemoryCache<string, number>(600, 1000000);
// Use cache here
// ...
memoryCache.destroy()

API Documentation

`K` is the type of the item key.
`V` is the type of the item value.
  
class MemoryCache<K, V> {
  constructor(itemsExpirationCheckIntervalInSecs: number, maxItemCount: number);
  storePermanentItem(itemKey: K, itemValue: V): void;
  storeExpiringItem(itemKey: K, itemValue: V, timeToLiveInSecs: number): void;
  getItemCount(): number;
  hasItem(itemKey: K): boolean;
  getValues(): V[];
  getItems(): [K, V][];
  retrieveItemValue(itemKey: K): V | undefined;
  getItemExpirationTimestampInMillisSinceEpoch(itemKey: K): number | undefined;
  removeItem(itemKey: K): void;
  exportItemsToJson(): string;
  
  // Use below function only with JSON output from exportItemsToJson method
  importItemsFrom(json: string): void; // Can throw if JSON is invalid
  
  clear(): void;
  destroy(): void;
}

License

MIT