Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hapi/catbox

Package Overview
Dependencies
Maintainers
7
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hapi/catbox

Multi-strategy object caching service

  • 11.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
645K
decreased by-17.52%
Maintainers
7
Weekly downloads
 
Created

What is @hapi/catbox?

@hapi/catbox is a multi-strategy caching library for Node.js, designed to work with the hapi.js framework but can be used independently. It provides a consistent API for various caching backends, making it easier to manage and optimize caching strategies.

What are @hapi/catbox's main functionalities?

Memory Caching

This code demonstrates how to use @hapi/catbox with an in-memory caching strategy. It initializes a Catbox client with the memory strategy, sets a value in the cache, and retrieves it.

const Catbox = require('@hapi/catbox');
const CatboxMemory = require('@hapi/catbox-memory');

const client = new Catbox.Client(CatboxMemory);

const start = async () => {
  await client.start();
  const key = { id: 'example', segment: 'examples' };
  await client.set(key, 'myValue', 10000);
  const value = await client.get(key);
  console.log(value.item); // 'myValue'
};

start();

Redis Caching

This code demonstrates how to use @hapi/catbox with a Redis caching strategy. It initializes a Catbox client with the Redis strategy, sets a value in the cache, and retrieves it.

const Catbox = require('@hapi/catbox');
const CatboxRedis = require('@hapi/catbox-redis');

const client = new Catbox.Client(CatboxRedis, { host: '127.0.0.1', port: 6379 });

const start = async () => {
  await client.start();
  const key = { id: 'example', segment: 'examples' };
  await client.set(key, 'myValue', 10000);
  const value = await client.get(key);
  console.log(value.item); // 'myValue'
};

start();

Custom Caching Strategy

This code demonstrates how to create and use a custom caching strategy with @hapi/catbox. It defines a custom cache class, initializes a Catbox client with this custom strategy, sets a value in the cache, and retrieves it.

const Catbox = require('@hapi/catbox');

class CustomCache {
  constructor(options) {
    this.cache = new Map();
  }

  async start() {}

  async stop() {}

  async get(key) {
    return { item: this.cache.get(key.id) };
  }

  async set(key, value, ttl) {
    this.cache.set(key.id, value);
  }

  async drop(key) {
    this.cache.delete(key.id);
  }
}

const client = new Catbox.Client(CustomCache);

const start = async () => {
  await client.start();
  const key = { id: 'example', segment: 'examples' };
  await client.set(key, 'myValue', 10000);
  const value = await client.get(key);
  console.log(value.item); // 'myValue'
};

start();

Other packages similar to @hapi/catbox

Keywords

FAQs

Package last updated on 10 Aug 2020

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc