@boardgame.io/storage-cache
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

@boardgame.io/storage-cache

NPM Version Test CI Status Coverage Status

🔌 A caching layer for boardgame.io database connectors

This package provides a wrapper for boardgame.io storage implementations, adding an in-memory cache for the most recently used data.

The cache expects all interactions with the underlying database to pass through it, so it is not appropriate for situations where you may have multiple processes or server instances. There are also limitations to what can be reliably cached: listing all games will always require a call to your database.

Installation

npm install --save @boardgame.io/storage-cache

Usage

Instantiate your database class as usual and pass it to StorageCache. This example uses the flat-file storage solution bundled with boardgame.io.

const { Server, FlatFile } = require('boardgame.io/server');
const { StorageCache } = require('@boardgame.io/storage-cache');
const { MyGame } = require('./game');

// instantiate the database class
const db = new FlatFile({ dir: '/storage/directory' });

// wrap the database with the caching layer
const dbWithCaching = new StorageCache(db, { cacheSize: 200 });

// pass the wrapped database to the boardgame.io server
const server = Server({
  games: [MyGame],
  db: dbWithCaching,
});

server.run(8000);

API

new StorageCache(database[, options])

database

An instance of a class implementing the boardgame.io storage API.

options

  • type: object
  • default: {}

An options object configuring the cache:

{
  /**
   * The number of games to cache in memory.
   * @type {number}
   * @default 1000
   */
  cacheSize: 1000,
}

Contributing

Bug reports, suggestions, and pull requests are very welcome! If you run into any problems or have questions, please open an issue.

Please also note the code of conduct and be kind to each other.

License

The code in this repository is provided under the MIT License.

Dependents (0)

Package Sidebar

Install

npm i @boardgame.io/storage-cache

Weekly Downloads

2

Version

0.6.0

License

MIT

Unpacked Size

21.6 kB

Total Files

7

Last publish

Collaborators

  • nicolodavis
  • jasonharrison
  • flamecoals
  • delucis