Socket
Socket
Sign inDemoInstall

@cacher/cacher

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cacher/cacher

Modern implementation of key-value storage


Version published
Weekly downloads
7
increased by600%
Maintainers
1
Install size
17.7 kB
Created
Weekly downloads
 

Readme

Source

NPM version NPM downloads

Modern implementation of key-value storage

Installation

Node.js 14.0.0 or newer is required

Yarn

yarn add @cacher/cacher

NPM

npm i @cacher/cacher

Example usage

import { Cacher } from '@cacher/cacher';

interface IMyData {
    someData: boolean;
}

const cacher = new Cacher<IMyData>({
    namespace: 'users'
});


async function run() {
    // Single
    
    const { 1: user } = await cacher.get({
        key: '1',
        // alias: 'user'
    });
    
    // With alias
    
    const { user } = await cacher.get({
        key: '1',
        alias: 'user'
    });

    // Multi get

    const result = await cacher.get([
        { key: '1' },
        { key: '2' },
        { key: '3' },
        { key: '4' }
    ]);

    // result[id] // => IMyData | undefined

    // Set

    await cacher.set({
        key: '1',
        value: {
            someData: true
        },
        // ms
        // ttl: 10_000
    });

    // Multi set

    await cacher.set([
        {
            key: '1',
            value: {
                someData: true
            },
            // ms
            // ttl: 10_000
        },
        {
            key: '2',
            value: {
                someData: true
            },
            // ms
            // ttl: 10_000
        }
    ]);

    // Increment
    // /!\ Cacher data must be float

    await cacher.increment({
        key: '1',
        value: 5
    });

    // Multi set

    await cacher.increment([
        {
            key: '1',
            value: 2
        },
        {
            key: '2',
            value: 4
        }
    ]);

    // Delete

    await cacher.delete('1');

    // Multi delete

    await cacher.delete(['1', '2', '3']);

    // Update ttl

    await cacher.touch({
        key: '1',
        ttl: 60_000
    });

    // Multi update ttl

    await cacher.touch([
        {
            key: '1',
            ttl: 60_000
        },
        {
            key: '2',
            ttl: 60_000
        }
    ]);

    // Clear all namespace

    await cacher.clear();
}

run().catch(console.log);

Use adapter

import { Cacher } from '@cacher/cacher';
import { RedisAdapter } from '@cacher/redis';

const adapter = new RedisAdapter();

const cacher = new Cacher({
    adapter,

    namespace: 'users'
});

Keywords

FAQs

Last updated on 27 Jul 2023

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc