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

redis-expiry

v1.1.8

Published

Use redis to expire your keys and handling the value

Downloads

354

Readme

Build Status

redis-expiry

Use redis to expire your keys and handling the value

📋 Features

  • Schedule the expiration of your keys
  • Handling your keys and values
  • CRUD your scheduler + rescheduling
  • Save multiple values in a single key
  • Retrieve your value when the key expire
  • Add cron task
  • Retrieve/Search by regexp

☁️ Installation

$ npm install redis-expiry

⚙️ Examples

const Redis = require("redis");
const redisExpiry = require("redis-expiry");

const redisSetter = Redis.createClient(process.env.REDIS_URL);
const redisGetter = Redis.createClient(process.env.REDIS_URL);

const rexp = redisExpiry(redisSetter, redisGetter);

rexp.set("myKeyByTimeout", "myValue").timeout(60000); // key will be expire in 60sec

const expireDate = new Date();
expireDate.setSeconds(expireDate.getSeconds() + 60);
rexp.set("myKeyByDate", "myValue").at(expireDate); // key will be expire in 60sec

rexp.set("myKeyByCron", "myValue").cron("*/30 * * * * *"); // key will be expire every 30sec

rexp.on(/myKeyBy(.)/, (value, key) => { // event will always be scheduled if the application restart
  console.log("Value returned", value, "From key", key);
});

📝 Usage

Initialization

Create a new instance :

const Redis = require("redis");
const redisExpiry = require("redis-expiry");

const redisSetter = Redis.createClient(process.env.REDIS_URL);
const redisGetter = Redis.createClient(process.env.REDIS_URL);

const rexp = redisExpiry(redisSetter, redisGetter);

The code bellow is deprecated since v1.0.4

const rexp = redisExpiry(redisSetter, process.env.REDIS_URL);

Information

⚠ If your application is shutdown and one of your keys expire, redis-expiry will detect them ⚠
⚠ Then when your application will be operationnal, the event rexp.on("myKey", callback) will be called⚠

Schedule a new scheduler

Before choosing the type of expiration, you have to set the key/value:

rexp.set("myKey", "myValue")...

rexp.set(...).infinit();
A simple redis set:

await rexp.set("myKeyByInfinit", "myValue").infinit();

myKeyByInfinit will never expired.

rexp.set(...).at(date);
Schedule from a date:

const currentDate = new Date();
currentDate.setSeconds(currentDate.getSeconds() + 30);
await rexp.set("myKeyByAt", "myValue").at(currentDate);

myKeyByAt will expire in 30 seconds.

rexp.set(...).timeout(integer);
Schedule from a timeout:

await rexp.set("myKeyByTimeout", "myValue").timeout(60000);

myKeyByTimeout will expire in 60 seconds.

rexp.set(...).now();
Schedule from now:

await rexp.set("myKeyByNow", "myValue").now();

myKeyByNow will expire in few milliseconds.

rexp.set(...).cron();
Schedule from cron:

await rexp.set("myKeyByCron", "myValue").cron("*/4 * * * * *", cronOption);

myKeyByCron will expire in the next multiple of 4 seconds.

⚠ after expiration the event is rescheduled. To stop the cron, check "Adding event handler" part bellow⚠

cronOption is optional, check the link bellow to know more.

More information: https://www.npmjs.com/package/cron-parser

Adding event handler

The handler will be call every time a specified key expires:

rexp.on("myKeyByTimeout", (value, key) => {
  // value === "myValue"
}, {
  maxConcurrent: 1 // Synchro event
});

To stop the cron task execute stop parameter:

rexp.on("myKeyByTimeout", (value, key, stop) => {
  stop(); // stop cron task
});

Using regexp :

rexp.on(/myKeyBy(.)/, (value, key, stop) => {
  // value === "myValue"
});

Every myKeyBy* key will be returned

Cancel scheduler

If no value is specified then every keys will be removed:

await rexp.del("myKeyByTimeout");

Remove specific contents by value:

await rexp.del("myKeyByTimeout", "myValue");

By regexp:

await rexp.del(/(.)Timeout/);

By regexp with a value:

await rexp.del(/(.)Timeout/, "myValue");

By guuid:

await rexp.delByGuuid("Dzokijo");

Retrieve scheduler

If no value is specified then every keys will be returned:

const result = await rexp.get("myKeyByTimeout");

Return specific contents by value:

const result = await rexp.get("myKeyByTimeout", "myValue");

By regexp:

await rexp.get(/(.)Timeout/);

By regexp with a value:

await rexp.get(/(.)Timeout/, "myValue");

By guuid:

await rexp.getByGuuid("Dzokijo");

Edit scheduled value

If no value is specified then every keys will be updated:

const result = await rexp.update("myKeyByTimeout")("myNewValue");

Update specific contents by value:

const result = await rexp.update("myKeyByTimeout", "myValue")("myNewValue");

By regexp:

await rexp.update(/(.)Timeout/)("myNewValue");

By regexp with a value:

await rexp.update(/(.)Timeout/, "myValue")("myNewValue");

By guuid:

await rexp.updateByGuuid("Dzokijo")("myNewValue");

Reschedule a scheduler

Reschedule date:

const currentDate = new Date();
currentDate.setSeconds(currentDate.getSeconds() + 30);
await rexp.reschedule("myKeyByAt", "myValue").at(currentDate);

Reschedule timeout:

await rexp.reschedule("myKeyByTimeout", "myValue").timeout(60000);

Reschedule now:

await rexp.reschedule("myKeyByNow", "myValue").now();

Reschedule cron:

await rexp.reschedule("myKeyByCron", "myValue").cron("*/4 * * * * *");

Reschedule all contents :

await rexp.reschedule("myKeyByTimeout").timeout(30000);

By regexp:

await rexp.reschedule(/(.)Timeout/).timeout(30000);

By regexp with a value:

await rexp.reschedule(/(.)Timeout/, "myValue").timeout(30000);

Every contents is rescheduled with a timeout at 30 secs

By guuid:

await rexp.rescheduleByGuuid("Dzokijo").timeout(30000);

Chainable API
Update value with andUpdateValue function :

await rexp.rescheduleByGuuid("Dzokijo").andUpdateValue("myNewValue").timeout(30000);

❓️ Testing

Clone the repo and run from the project root:

$ npm install
$ npm test