Skip to content

Commit

Permalink
Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovepixelart committed May 9, 2024
1 parent 2c29a03 commit b146f59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
],
"cSpell.words": [
"autosync",
"bson",
"dbaeumer",
"Dsonar",
"EJSON",
"ilovepixelart",
"jsonpatch",
"lcov",
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"release": "npm run lint && npm run build && np"
},
"dependencies": {
"bson": "6.7.0",
"ioredis": "5.4.1",
"ms": "2.1.3",
"sort-keys": "4.2.0"
Expand Down
16 changes: 3 additions & 13 deletions src/cache/engine/RedisCacheEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import IORedis from 'ioredis'
import mongoose, { Types } from 'mongoose'
import { EJSON } from 'bson'

import type { Redis, RedisOptions } from 'ioredis'
import type IData from '../../interfaces/IData'
Expand All @@ -20,21 +20,11 @@ class RedisCacheEngine implements ICacheEngine {
if (value === null) {
return undefined
}
return JSON.parse(value, (_, value) => {
if (typeof value === 'string') {
const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
if (dateRegex.test(value)) {
return new Date(value)
} else if (mongoose.isObjectIdOrHexString(value)) {
return new Types.ObjectId(value)
}
}
return value as unknown
}) as Promise<Record<string, unknown> | Record<string, unknown>[]>
return EJSON.parse(value) as Promise<Record<string, unknown> | Record<string, unknown>[]>
}

async set(key: string, value: unknown, ttl = Infinity): Promise<void> {
const serializedValue = JSON.stringify(value)
const serializedValue = EJSON.stringify(value)
await this.#client.setex(key, Math.ceil(ttl / 1000), serializedValue)
}

Expand Down

0 comments on commit b146f59

Please sign in to comment.