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

jnoria-redis-lru-cache

v0.0.3

Published

LRU (Least Recently Used) cache with time expiration using redis

Downloads

1

Readme

jnoria-redis-lru-cache

LRU (Least Recently Used) cache with time expiration using redis. The library uses Redis in order to persist the elements in a centralized server. The cache method is straightforward: a FIFO (First In, First Out) queue is created with a fixed size for the cache. When the cache reaches its limit, we remove the first item from the list. Frequently accessed items continuously move to the end of the queue, reducing the likelihood of being evicted from the cache

Dependancies

  • Redis, v4x

Pre-requisites

  • Install Node 16x or higher:
  • It's a prerequisite to have Redis installed. https://redis.io/docs/getting-started/installation/. If you prefer, you can use a docker cointainer

Start a redis if you install on OS :

docker run -p 6379:6379 -it redis/redis-stack-server:latest

Start a redis via docker:

docker run -p 6379:6379 -it redis/redis-stack-server:latest

Installation

npm install jnoria-redis-lru-cache

Using the library

import LruCache  from 'lru-cache'
import { createClient } from 'redis';

const redisClient = await createClient();
//Redis client must be connected
await redisClient.connect();
const cache = new LruCache(redisClient, "mycache", 3, 60);
//Init Method must be called. If not, it will throw an Error
await cache.init();
//Call clear to delete all elements from cache and redis
await cache.clear();
console.log(await cache.put('one', 'uno')); //cache ['uno']
console.log(await cache.put('two', 'dos')); //cache ['uno','dos']
console.log(await cache.put('three', 'tres')); //cache ['uno','dos','tres']
console.log(await cache.get('one')); //cache ['dos','tres','uno']
console.log(await cache.put('four', 'cuatro')); //cache ['tres','uno','cuatro']
console.log(await cache.get('two')); //returns undefinied

Options

  • redisClient: Intance of redis client. Should be connected
  • cacheName: Cache name saved on Redis in order to create multiples cache on the same redir server
  • maxSize: Size of the cache stack
  • expirationTime: Expiration time in seconds for a Node. If the expiration time is completed, the node will be deleted automatically

API

  • init(): inicializes cache. Must be called before use other methods.
  • put(key, value): set value for the given key, marking it as the most recently accessed one. Keys should be strings, value be strings.
  • get(key): get the value stored in the cache for the given key or undefinied if not exists. If exixts, the element will be marked as the most recently accessed one.
  • delete(key): removes the element from the cache.
  • clear(): empties the cache locally and server.
  • getAll(key): return all elements in a Map.

Run test project

$ npm install
$ npm test

Example creating a new typescript project and use the library

Create new typescript project

$ mkdir my_project
$ cd my_project
$ npm init -y
$ npm install typescript --save-dev
$ npx tsc --init

Install the library

$ npm install jnoria-redis-lru-cache --save

Create new file index.ts

import LruCache  from 'lru-cache'
import { createClient } from 'redis';

const redisClient = await createClient();
//Redis client must be connected
await redisClient.connect();
const cache = new LruCache(redisClient, "mycache", 3, 60);
//Init Method must be called. If not, it will throw an Error
await cache.init();
//Call clear to delete all elements from cache and redis
await cache.clear();
console.log(await cache.put('one', 'uno')); //cache ['uno']
console.log(await cache.put('two', 'dos')); //cache ['uno','dos']
console.log(await cache.put('three', 'tres')); //cache ['uno','dos','tres']
console.log(await cache.get('one')); //cache ['dos','tres','uno']
console.log(await cache.put('four', 'cuatro')); //cache ['tres','uno','cuatro']
console.log(await cache.get('two')); //returns undefinied

Build project

$ tsc --build

Run project

$ node index.js

Reference

https://levelup.gitconnected.com/implementing-lru-cache-with-node-js-and-typescript-a7c8d3f6a63