New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

multi-layers-cache

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multi-layers-cache

A very simple multi layers cache.

latest
npmnpm
Version
0.0.4
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

multi-layers-cache

A very simple multi layers cache.

Usage

import { newCacheStorage, newLRUCache } from 'multi-layers-cache';

const cache = newCacheStorage({ layers: [newLRUCache()] });
await cache.set('ah', 10);
console.log(await cache.get('ah'));

newLRUCache will create a simple in-memory LRU cache using Map.

You can build your own cache, with just simple interfaces:

interface Cache {
  get(key: string): Promise<Value | undefined>;
  /**
   * @param expireAt In seconds
   */
  set(key: string, value: string | number, expireAt?: number): Promise<void>;
  del(key: string): Promise<void>;
}

interface Value {
  expireAt: number;
  value: string | number;
  key: string;
}

Redis cache layer

import { Redis } from 'ioredis'

interface Cache {
  get(key: string): Promise<Value | undefined>
  /**
   * @param expireAt In seconds
   */
  set(key: string, value: string | number, expireAt?: number): Promise<void>
  del(key: string): Promise<void>
}

interface Value {
  /**
   * `-1` means No expiration.
   */
  expireAt: number
  value: string | number
  key: string
}

export function newRedisCache(): Cache {
  const redis = new Redis()
  return {
    async get(key) {
      const results = await redis.pipeline().get(key).ttl(key).exec()

      if (!results) {
        return
      }

      const [[, value], [, ttl]] = results
      return {
        value: value as string,
        key,
        expireAt: ttl as number,
      }
    },
    async set(key, value, expireAt) {
      expireAt ? await redis.setex(key, expireAt, value) : await redis.set(key, value)
    },
    async del(key) {
      await redis.del(key)
    },
  }
}

Usage with redis cache

import { newCacheStorage, newLRUCache } from 'multi-layers-cache';

const cache = newCacheStorage({ layers: [newLRUCache(), newRedisCache()] });
await cache.set('ah', 'hello', 10 /* 10 seconds */);
console.log(await cache.get('ah'));

How it works?

newCacheStorage returns a Cache compatible instance.

It maintains an internal array of cache layers. When a get operation is performed, it loops through the cache layers sequentially. If it finds a value on a higher layer, it saves that value to the lower layers with the same ttl. For set and del operations, items are modified or removed in all layers.

LICENSE

BSD-3-Clause @ Ninh Pham (ReeganExE)

Keywords

LRU

FAQs

Package last updated on 07 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts