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

simple-cache-node

v1.0.14

Published

Simple 4.87kB memory cache for node

Downloads

40

Readme

simple-cache-node

npm version

Description

The project provides a caching mechanism for storing and retrieving items in different caches. It offers a set of functions to manage the caches, cache items, invalidate cached items, retrieve cached items, seed caches, and convert arrays of items into cache objects.

Installation

npm install simple-cache-node

Usage

Single Cache Usage - (Caching user scenario)

import { cacheItem, getCachedItem, invalidateCachedItem, setAccessorKey, seedCache, arrayToCacheObject } from 'simple-cache-node';

// Set the accessor key for your items. 
// parameter "accessorKey" defaults to 'id', if your items have 'id' property with unique values you dont have to call it.
// Calling this function one time in your entry file is enough.
setAccessorKey('_id');

// Seed the cache
// Like "setAccessorKey" function calling this function one time in your entry file is also enough.
// WARNING: Calling this function will override all the cache items.
seedCache(arrayToCacheObject([
    { _id: 1, fullName: 'Simple Cache Node', role: 'user' },
    { _id: 2, fullName: 'Simple Cache Node', role: 'admin' }
]))
getCachedItem(1); // => { _id: 1, role: 'user' }
getCachedItem(2); // => { _id: 2, role: 'admin' }
// Cache the user when they are logged in
const login = async () => {
    const user = await SomeDB.SomeQuery.GetUser(); // => { _id: 1, fullName: 'Simple Cache Node', role: 'user' }
    // ...
    cacheItem(user)
    // ...
}

// Retrieve the user from cache
const insertSomethingToDb = async () => {
    let something = {/* ... */};
    const user = getCachedItem(1) // => { _id: 1, fullName: 'Simple Cache Node', role: 'user'} ;
    something.createdBy = user._id;
}

// Invalidate the user in needed parts of your code. e.g when user is updated
const updateUser = async () => {
    // ...
    const userId = 1; 
    invalidateCachedItem(1)
    // or 
    const user = { _id: 1, fullName: 'Simple Cache Node', role: 'user' }
    invalidateCachedItem(user)
    const userFromCache = getCachedItem(1) // => undefined
    // ...
}

Multi Cache Usage (Caching user and blog posts seperately)

Having different caches is just easy as passing second parameter to setAccessorKey, seedData, cacheItem, getCachedItem and invalidateCachedItem functions.

Example:

import { cacheItem, getCachedItem, invalidateCachedItem, setAccessorKey, seedCache, arrayToCacheObject } from 'simple-cache-node';
// Set the accessor key for your items. 
// First parameter "accessorKey" defaults to 'id', if your items have 'id' property with unique values you dont have to call it.
// Second parameter "cacheName" defaults to 'initial', 
// Calling this function one time in your entry file is enough.
setAccessorKey('_id', 'userCache');
setAccessorKey('id', 'blogCache');


// Cache the item 
// ...
const user = await SomeDB.SomeQuery.GetUser(); // => { _id: 1, fullName: 'Simple Cache Node', role: 'user' }
const blogPost = await SomeDB.SomeQuery.GetBlogPost(); // => { id: 'uuid', title: 'Simple Cache Node Blog Post', content: 'SOME_HTML_HERE' }

cacheItem(user, 'userCache')
cacheItem(blogPost, 'blogCache')
// ...

// Retrieve the item from cache
// ...

getCachedItem(1, 'userCache') => { _id: 1, fullName: 'Simple Cache Node', role: 'user' }
getCachedItem('uuid', 'blogCache') // => { id: 'uuid', title: 'Simple Cache Node Blog Post', content: 'SOME_HTML_HERE' }
// ...


// Invalidate cached item
// ...

getCachedItem(1, 'userCache') // => { _id: 1, fullName: 'Simple Cache Node', role: 'user' }
getCachedItem('uuid', 'blogCache') // => { id: 'uuid', title: 'Simple Cache Node Blog Post', content: 'SOME_HTML_HERE' }
invalidateCachedItem(1, 'userCache')
invalidateCachedItem('uuid', 'blogCache')
// ...

API Reference

setAccessorKey(accessorKey, cacheName)

Set the accessor key for a given cache name.

  • accessorKey (string): The accessor key to be set.
  • cacheName (string): The name of the cache. Defaults to 'initial'.

cacheItem(item, cacheName)

Cache an item in the specified cache.

  • item (any): The item to be cached.
  • cacheName (string): The name of the cache. Defaults to 'initial'.

invalidateCachedItem(itemOrAccessorValue, cacheName)

Invalidate a cached item based on the item itself or its accessor value.

  • itemOrAccessorValue (any|string): The item or accessor value of the item to be invalidated.
  • cacheName (string): The name of the cache. Defaults to 'initial'.

getCachedItem(accessorValue, cacheName)

Retrieve a cached item based on its accessor value.

  • accessorValue (string|number): The accessor value of the item to be retrieved.
  • cacheName (string): The name of the cache. Defaults to 'initial'.
  • Returns: The cached item if found, or undefined if not found.

seedCache(items, cacheName)

Seed a cache with the specified items.

  • items (object): The items to be seeded in the cache.
  • cacheName (string): The name of the cache. Defaults to 'initial'.

arrayToCacheObject(items, accessorKey)

Convert an array of items into a cache object using the specified accessor key.

  • items (array): The array of items to be converted.
  • accessorKey (string): The key used to access each item in the cache object. Defaults to 'id'.
  • Returns: The cache object where each item is indexed by the accessor value.