Mikro ORM Redis Cache Adapter
This is mikro orm redis cache adapter that uses v8.serialize
and
vs.deserialize
rather than JSON.stringify
and JSON.parse
.
Quick start
After install the package, add resultCache
config.
import
RedisCacheAdapter,
{ type RedisCacheAdapterOptions}
from '@javien/mikro-orm-redis-cache-adapter'
defineConfig({
resultCache: {
adapter: RedisCacheAdapter
options: {
client: redis
debug: true,
prefix: 'mikro',
prefixDelimiter: ':',
logger: console.log,
gracefulShutdown: false
} as RedisCacheAdapterOptions
}
})
Why not JSON.stringify?
JSON.stringify
is usually used to serialize object,
but there're several kind of data that it can't serialize and deserialize - BigInt, recursive, Buffer, Map, Set and etc.
JSON.stringify(BigInt(1))
const obj = {}
obj.obj = obj
JSON.stringify(obj)
const buf = Buffer.from([0])
const str = JSON.stringify(buf)
const parsed = JSON.parse(str)
To solve this, we can pass replacer
parameter. However I thought this is not a best way to serialize data.