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 🙏

© 2025 – Pkg Stats / Ryan Hefner

memory-sri

v1.3.2

Published

Powerful yet simple memory and queue manager for building efficient Node.js apps.

Readme

Memory Library

Powerful yet simple memory and queue manager for building efficient Node.js apps.

Installation

You can install this package using NPM:

npm install memory-sri

Import memory-sri

import Memory from 'memory-sri';

OR

const Memory = require('memory-sri');

🛠 API Reference

Memory store

Memory.set(key, value, ttlSeconds?)
Memory.get(key)
Memory.ttl(key)
Memory.del(key)
Memory.multiple()
Memory.delall()

Task queue

Memory.process(name, fn) → Register a processor for a task type
Memory.addtask(name, data, retries?) → Enqueue a job
Memory.runtask(name) → Manually run a queued job

⚠️ Notes

This is in-memory only (no persistence). Data clears when the process restarts.
Use in production only for lightweight or temporary jobs.

Example of Full Usage:

function has three parameters: Identifier, Data, Expire

Memory.set('1', "a1", 10); // Key '1', Value "a1", Expires in 10 seconds

Memory.set('2', { b: 2 }, 20); // Key '2', Value is an object, Expires in 20 seconds

Memory.set('3', [ "a1", "a2" ], 30); // Key '3', Value is an array of strings, Expires in 30 seconds

Memory.set('4', [ { a: "a1", b: "b1" } ], 40); // Key '4', Value is an array of objects, Expires in 40 seconds

Memory.set('5', "not set expire"); // If no expiration time is set, the data will automatically expire after 3 days or 259200 seconds. // Maximum 7 days or 604800 seconds.

set

// for set it take job to stored data
Memory.set(1, 'a1', 20);
Memory.set(2, { b: 2 }, 30);
Memory.set(3, [2, 3], 40);
Memory.set(4, [{ a: 'a1', b: 'b1' }], 50);
Memory.set('5', "not set expire");

get

// for get data realtime
let get1 = Memory.get(1); // 'a1'
let get2 = Memory.get(2); // { b: 2 }
let get3 = Memory.get(3); // [ 2, 3 ]
let get4 = Memory.get(4); // [ { a: 'a1', b: 'b1' } ]

get timeout

// for get timeout data realtime
let ttl1 = Memory.ttl(1); // 20000

multiple

// for get data all realtime
let getall = Memory.multiple();

Output:

[
  { id: '1', data: 'a1' },
  { id: '2', data: { b: 2 } },
  { id: '3', data: [ 2, 3 ] },
  { id: '4', data: [ { a: 'a1', b: 'b1' } ] }
  { id: '5', data: 'not set expire' }
]

delete

// for delete it take job to delete data from stored
Memory.del(1);

delete all

// for delete all it take job to delete all data from stored
Memory.delall();

Task

Main page for process

// queue.service.js
const Memory = require('memory-sri');

const plus = function (a, b) {
  return a + b;
};

// register processor
Memory.process('test', async function (data, meta) {
  console.log("Running queued task with:");
  console.log(data);
  console.log(meta);
  // do your background work
  let a = data.price;
  let b = data.value;
  let c = plus(a, b);
  console.log(`total: ${c}`);
});

Task usage

// import queue to index.js or app.js for side effects
const queue = require('./queue.service');

Task usage

// index.js
import express from 'express';

// import queue for side effects
const queue = require('./queue.service');

// import Memory for in-memory data storage
const Memory = require('memory-sri');

const app = express();
app.use(express.json());

// Example route to demonstrate Memory usage
app.get('/memory', async (req, res) => {
  try {
    // set key with TTL (seconds)
    Memory.set(1, 'a1', 20);
    Memory.set(2, { b: 2 }, 30);
    Memory.set(3, [2, 3], 40);
    Memory.set(4, [{ a: 'a1', b: 'b1' }], 50);

    // set key with no expiry
    Memory.set(5, 'not set expire');

    // get value
    console.log(Memory.get(5)); // { b: 2 }

    // check TTL
    console.log(Memory.ttl(5)); // remaining seconds

    // get all keys
    let getAll = await Memory.multiple();

    let result = [];
    for (let i = 0; i < getAll.length; i++) {
      let key = i + 1;
      let ttl = Memory.ttl(key);
      result.push({ id: getAll[i].id, data: getAll[i].data, ttl: ttl });
    }

    // delete a key
    Memory.del(1);

    // delete all keys
    Memory.delall();

    res.json({ data: result });
  } catch (error) {
    console.error('Retrieve Error:', error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
});

// Example route to demonstrate Memory usage
app.get('/run', async (req, res) => {
  try {
    // run the task processor
    let testFunction = function (a, b) {
      console.log(`result from task function: ${a + b}`);
      return a + b;
    };

    // run task with arguments
    let result = await Memory.runtask(testFunction, [2, 4]);

    res.json({ id: result.id });
  } catch (error) {
    console.error('Retrieve Error:', error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
});

// Example route to demonstrate Memory usage
app.get('/queue', async (req, res) => {
  try {
    let dataQueue = {
      user: 1,
      value: Math.random(),
      price: Math.random() * 100,
    };
    // enqueue a job
    let result = Memory.addtask('test', dataQueue, 3);

    res.json({ id: result.id, data: result.data });
  } catch (error) {
    console.error('Retrieve Error:', error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
});

// Start Server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

Key Features:

  • Installation: Instructions on how to install the package with NPM.
  • Usage: Detailed steps on how to import and use the memory-sri library in your application.
  • Lightweight In-Memory Storage: Store strings, objects, arrays, or nested structures with minimal overhead.
  • Simple function: Provides set, get, ttl, multiple, del, and delall for easy data handling.
  • Automatic Expiry Support: Each key can have an expiration time (TTL) in seconds.
  • Flexible Data Types: Supports strings, objects, arrays, and complex nested data.
  • Batch Retrieval: multiple() method returns all stored data in a clean array format.
  • Easy Integration: Works with both CommonJS (require) and ES Modules (import).

New Features:

  • Background Task Processing: A separate worker process reads tasks from the queue and runs the function.

  • Test jest: Jest tests verify core functionality, TTL sorting, and correct task recording.

Repositories