npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@javien/mikro-orm-redis-cache-adapter

v0.3.1

Published

mikro-orm redis cache adapter using v8 serialize

Downloads

127

Readme

Mikro ORM Redis Cache Adapter

This is mikro orm redis cache adapter that uses v8.serialize and v8.deserialize rather than JSON.stringify and JSON.parse.

Quick Start

After install the package, edit your resultCache configuration.

import { RedisCacheAdapter, type RedisCacheAdapterOptions} from '@javien/mikro-orm-redis-cache-adapter'

defineConfig({
  // your configuration
  resultCache: {
    adapter: RedisCacheAdapter
    options: {
      // pass your redis client
      client: redis
      // (optional) Debug mode. Defaults to `false`.
      debug: true,
      // (optional) The prefix for the cache keys. Defaults to `mikro`.
      prefix: 'mikro',
      // (optional) The delimiter between the prefix and the cache key. Defaults to `:`.
      prefixDelimiter: ':',
      // (optional) Logger. Defaults to `console.log`.
      logger: myLogger,
      // (optional) gracefulShutdown: If you want to close the Redis connection by yourself,
      // set it to `false`. Defaults to `true`.
      gracefulShutdown: false,
      // (optional) maximumCacheBytes: The maximum cache size of each key in bytes. Defaults to `-1`.
      maximumCacheBytes: 1024 * 1024,
      // (optional) base64Encode: If you want to use base64 encoding, set it to `true`. Defaults to `false`.
      base64Encode: false
    } as RedisCacheAdapterOptions
  }
})

Behavior Explained

  • Failing to store cache, it'll log the error regardless of the debug mode.
  • Failing to fetch cache, undefined will be returned, which means your data will be loaded from the database.
  • Failing to delete the cache, it'll log the error regardless of the debug mode.

Supported Data Types

v8.serialize is compatible with The structured clone algorithm, so only Supported types are able to get serialized.

Here're suported JavaScript types.

  • Array, ArrayBuffer, and TypedArray
  • Number, String, and Boolean
  • Primitive types, except symbol.
  • DataView
  • Date
  • Error types (see more at MDN page).
  • Map and Set
  • Object objects: but only plain objects (e.g. from object literals).
  • RegExp: but note that lastIndex is not preserved.

If your property is not supported type, consider using Custom Type.

Why not JSON.stringify?

JSON.stringify is usually used to serialize objects, but there are several kinds of data that it can't serialize and deserialize - BigInt, recursive, Buffer, Map, Set, etc.

JSON.stringify(BigInt(1))
// Uncaught TypeError: Do not know how to serialize a BigInt

JSON.stringify(new Date()) // '"2024-05-26T05:39:00.493Z"'
// serializes to string, so parsed result is different from original

const buf = Buffer.from([0])
const str = JSON.stringify(buf) //'{"type":"Buffer","data":[0]}'
// Buffer.toJSON method is called, so parsed result is different from original

To solve this, we can pass the replacer parameter. So you might think using JSON.stringify with replacer is fine.

However, I don't think this is the best way to serialize data, while we can choose v8 api.