1.0.14 • Published 2 years ago

simple-cache-node v1.0.14

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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.
1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago