• Stars
    star
    381
  • Rank 112,502 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 1 year ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿฑ Bentocache is a robust multi-tier caching solution for Node.js applications

image


Bentocache is a robust multi-tier caching solution for Node.js applications

Features

  • ๐Ÿ—„๏ธ Multi-tier caching
  • ๐Ÿ”„ Synchronization of local cache via Bus
  • ๐Ÿš€ Many drivers (Redis, Upstash, In-memory, Postgres, Sqlite and others)
  • ๐Ÿ›ก๏ธ Grace period and timeouts. Serve stale data when the store is dead or slow
  • ๐Ÿ”„ Early refresh. Refresh cached value before needing to serve it
  • ๐Ÿ—‚๏ธ Namespaces. Group your keys by categories.
  • ๐Ÿ›‘ Cache stamped protection.
  • ๐Ÿท๏ธ Named caches
  • ๐Ÿ“– Well documented + handy JSDoc annotations
  • ๐Ÿ“Š Events. Useful for monitoring and metrics
  • ๐Ÿ“ Easy Prometheus integration and ready-to-use Grafana dashboard
  • ๐Ÿงฉ Easily extendable with your own driver

See documentation at bentocache.dev

Why Bentocache ?

There are already caching libraries for Node: keyv, cache-manager, or unstorage. However, I think that we could rather consider these libraries as bridges that allow different stores to be used via a unified API, rather than true caching solutions as such.

Not to knock them, on the contrary, they have their use cases and cool. Some are even "marketed" as such and are still very handy for simple caching system.

Bentocache, on the other hand, is a full-featured caching solution. We indeed have this notion of unified access to differents drivers, but in addition to that, we have a ton of features that will allow you to do robust caching.

With that in mind, then I believe there is no serious alternative to Bentocache in the JavaScript ecosystem. Which is regrettable, because all other languages have powerful solutions. This is why Bentocache was created.

Quick presentation

Bentocache is a caching solution aimed at combining performance and flexibility. If you are looking for a caching system that can transition from basic use to advanced multi-level configuration, you are in the right place. Here's what you need to know :

One-level

The one-level mode is a standard caching method. Choose from a variety of drivers such as Redis, In-Memory, Filesystem, DynamoDB, and more, and you're ready to go.

In addition to this, you benefit from many features that allow you to efficiently manage your cache, such as cache stampede protection, grace periods, timeouts, namespaces, etc.

Two-levels

For those looking to go further, you can use the two-levels caching system. Here's basically how it works:

  • L1: Local Cache: First level cache. Data is stored in memory with an LRU algorithm for quick access
  • L2: Distributed Cache: If the data is not in the in-memory cache, it is searched in the distributed cache (Redis, for example)
  • Synchronization via Bus: In a multi-instance context, you can synchronize different local in-memory caches of your instances via a Bus like Redis or RabbitMQ. This method maintains cache integrity across multiple instances

Here is a simplified diagram of the flow :

Bentocache Flow

All of this is managed invisibly for you via Bentocache. The only thing to do is to set up a bus in your infrastructure. But if you need multi-level cache, you're probably already using Redis rather than your database as a distributed cache. So you can leverage it to synchronize your local caches

The major benefit of multi-tier caching, is that it allows for responses between 2,000x and 5,000x faster. While Redis is fast, accessing RAM is REALLY MUCH faster.

In fact, it's a quite common pattern : to quote an example, it's what Stackoverflow does.

To give some perspective, here's a simple benchmark that shows the difference between a simple distributed cache ( using Redis ) vs a multi-tier cache ( using Redis + In-memory cache ) :

Redis vs Multi-tier caching

Features

Below is a list of the main features of BentoCache. If you want to know more, you can read each associated documentation page.

Multi layer caching

Multi-layer caching allows you to combine the speed of in-memory caching with the persistence of a distributed cache. Best of both worlds.

Lot of drivers

Many drivers available to suit all situations: Redis, Upstash, Database (MySQL, SQLite, PostgreSQL), DynamoDB, Filesystem, In-memory (LRU Cache), Vercel KV...

See the drivers documentation for list of available drivers. Also very easy to extend the library and add your own driver

Resiliency

  • Grace period: Keep your application running smoothly with the ability to temporarily use expired cache entries when your database is down, or when a factory is failing.

  • Cache stamped prevention: Ensuring that only one factory is executed at the same time.

  • Retry queue : When a application fails to publish something to the bus, it is added to a queue and retried later.

Timeouts

If your factory is taking too long to execute, you can just return a little bit of stale data while keeping the factory running in the background. Next time the entry is requested, it will be already computed and served immediately.

Namespaces

The ability to create logical groups for cache keys together, so you can invalidate everything at once later :

const users = bento.namespace('users')

users.set('32', { name: 'foo' })
users.set('33', { name: 'bar' })

users.clear() 

Events

Events are emitted by Bentocache throughout its execution, allowing you to collect metrics and monitor your cache.

bento.on('cache:hit', () => {})
bento.on('cache:miss', () => {})
// ...

See the events documentation for more information.

Friendly TTLs

All TTLs can be passed in a human-readable string format. We use lukeed/ms under the hood. (this is optional, and you can pass a number in milliseconds if you prefer)

bento.getOrSet('foo', () => getFromDb(), {
  ttl: '2.5h'
  gracePeriod: { enabled: true, duration: '6h' }
})

Early refresh

When you cached item will expire soon, you can refresh it in advance, in the background. This way, next time the entry is requested, it will already be computed and thus returned to the user super quickly.

bento.getOrSet('foo', () => getFromDb(), {
  earlyExpiration: 0.8
})

In this case, when only 20% or less of the TTL remains and the entry is requested :

  • It will returns the cached value to the user.
  • Start a background refresh by calling the factory.
  • Next time the entry is requested, it will be already computed, and can be returned immediately.

Logging

You can pass a logger to Bentocache, and it will log everything that happens. Can be useful for debugging or monitoring.

import { pino } from 'pino'

const bento = new BentoCache({
  logger: pino()
})

See the logging documentation for more information.

Sponsor

If you like this project, please consider supporting it by sponsoring it. It will help a lot to maintain and improve it. Thanks a lot !

More Repositories

1

cli-candlestick-chart

๐Ÿ“ˆ Display candlestick charts right into your terminal.
Rust
253
star
2

verrou

๐Ÿ”’ Verrou is a library for managing Locks in Node.js. Support multiple drivers
TypeScript
174
star
3

vite-plugin-validate-env

โœ… Vite plugin for validating your environment variables
TypeScript
163
star
4

hot-hook

๐Ÿช Simple HMR for NodeJS + ESM
TypeScript
158
star
5

fast-ssh

โŒจ๏ธ FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config.
Rust
150
star
6

pino-loki

๐Ÿ”‰ This package provides a transport for pino that forwards messages to Loki.
TypeScript
111
star
7

adonis-sail

โ›ตGenerate a ready-to-use local docker environment for your Adonis application
TypeScript
97
star
8

adonis-vscode-extension

๐Ÿ’ป VSCode Extension for AdonisJS
TypeScript
64
star
9

unocss-preset-forms

๐Ÿ“‹ Port of @tailwindcss/forms for UnoCSS.
TypeScript
56
star
10

adonisjs-prometheus

๐Ÿ“Š Prometheus Provider for AdonisJS with some builtins metrics for monitoring your application.
TypeScript
42
star
11

unocss-preset-heropatterns

๐Ÿ UnoCSS preset that integrates Hero Patterns.
TypeScript
27
star
12

unocss-preset-flowbite

๐Ÿ’… An adaptation of the Flowbite Tailwind plugin for UnoCSS
TypeScript
27
star
13

pretty-list-routes

๐Ÿ›ฃ๏ธ A beautiful `list:routes` for AdonisJS
TypeScript
25
star
14

adoscope

๐Ÿ”ญ An elegant development assistant for your AdonisJS Application
TypeScript
22
star
15

factorify

๐Ÿญ Framework-agnostic model factory system for clean testing
TypeScript
19
star
16

japa-vscode

๐Ÿงช A Japa extension for VSCode
TypeScript
17
star
17

adonis-grpc-consumer

๐Ÿ•ธ๏ธ Adonis gRPC client provider for easily communicate with gRPC services.
TypeScript
13
star
18

cron-expression-generator

๐Ÿ”ง Generate crontab expression using friendly and declarative API
TypeScript
10
star
19

socket.io-prometheus

๐Ÿ“ก Exposes metrics endpoint for Prometheus to collect data about Socket.io.
TypeScript
5
star
20

lit-valtio-state

๐Ÿช A simple state management library for Lit components
TypeScript
3
star
21

japa-database-plugin

๐Ÿ’ฝ Database assertions and testing helpers for Japa
TypeScript
3
star
22

adonis-packages

TypeScript
3
star
23

adonis-extension-pack

๐Ÿ“ฆ Collection of extensions for Adonis.js development
2
star
24

tooling-configs

๐Ÿ‘Œ My custom tool configurations ( ESLint, TS, Prettier ... )
TypeScript
2
star
25

module-methods-extractor

๐Ÿช› Utility module to extract public methods for a given default export.
TypeScript
1
star
26

crypto-rss-news-app

Nativescript application that displays crypto market news from various RSS feeds.
Vue
1
star
27

music-ddl

Simple music downloader built on top of octlif, that downloads music from multiples DDL sites as sources.
TypeScript
1
star
28

Wolf3D

A Wolfenstein 3D like game, using raycasting.
C
1
star
29

Julien-R44

1
star
30

gitlab-slack-release-notifier

๐Ÿค– A small service that sends a notification on Slack when a release has been published on GitLab.
TypeScript
1
star
31

binance-scanner-wma-sma-cross

Program that analyzes the charts of all symbols on Binance Futures, and when a WMA/SMA cross is found on a certain UT, a notification is sent on a discord channel.
TypeScript
1
star
32

tuyau-stackblitz-demo

TypeScript
1
star