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

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

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

mikro-orm redis cache adapter using v8 serialize

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
increased by50%
Maintainers
0
Weekly downloads
 
Created
Source

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
    } 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.

Debug Mode

If you use debug mode, saving data is shown with JSON.stringify applied. it doesn't mean JSON.stringify is used to serialize the data. It's only for logging purposes.

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.

Keywords

FAQs

Package last updated on 17 Jul 2024

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