@platformatic/next 2.19.0-alpha.1 → 2.19.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,12 +14,43 @@ const sections = {
14
14
  tags: 'tags'
15
15
  }
16
16
 
17
+ const kReferences = Symbol('references')
18
+ const clients = new Map()
19
+
17
20
  export function keyFor (prefix, subprefix, section, key) {
18
21
  return [prefix, 'cache:next', subprefix, section, key ? Buffer.from(key).toString('base64url') : undefined]
19
22
  .filter(c => c)
20
23
  .join(':')
21
24
  }
22
25
 
26
+ export function getConnection (url) {
27
+ let client = clients.get(url)
28
+
29
+ if (!client) {
30
+ client = new Redis(url, { enableAutoPipelining: true })
31
+ client[kReferences] = 0
32
+ clients.set(url, client)
33
+ }
34
+
35
+ client[kReferences]++
36
+ return client
37
+ }
38
+
39
+ export function releaseConnection (url) {
40
+ const client = clients.get(url)
41
+
42
+ if (!client) {
43
+ return
44
+ }
45
+
46
+ client[kReferences]--
47
+
48
+ if (client[kReferences] < 1) {
49
+ client.disconnect(false)
50
+ clients.remove(url)
51
+ }
52
+ }
53
+
23
54
  export class CacheHandler {
24
55
  #config
25
56
  #logger
@@ -30,13 +61,13 @@ export class CacheHandler {
30
61
  constructor () {
31
62
  this.#logger = this.#createLogger()
32
63
  this.#config = globalThis.platformatic.config.cache
33
- this.#store = new Redis(this.#config.url, { enableAutoPipelining: true })
64
+ this.#store = getConnection(this.#config.url)
34
65
  this.#maxTTL = this.#config.maxTTL
35
66
  this.#subprefix = this.#getSubprefix()
36
67
 
37
68
  // Handle disconnection not to hang the process on exit
38
69
  globalThis.platformatic.events.on('plt:next:close', () => {
39
- this.#store.disconnect(false)
70
+ releaseConnection(this.#config.url)
40
71
  })
41
72
  }
42
73
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/next",
3
- "version": "2.19.0-alpha.1",
3
+ "version": "2.19.0-alpha.2",
4
4
  "description": "Platformatic Next.js Stackable",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -22,9 +22,9 @@
22
22
  "iovalkey": "^0.2.1",
23
23
  "msgpackr": "^1.11.2",
24
24
  "semver": "^7.6.3",
25
- "@platformatic/basic": "2.19.0-alpha.1",
26
- "@platformatic/config": "2.19.0-alpha.1",
27
- "@platformatic/utils": "2.19.0-alpha.1"
25
+ "@platformatic/basic": "2.19.0-alpha.2",
26
+ "@platformatic/config": "2.19.0-alpha.2",
27
+ "@platformatic/utils": "2.19.0-alpha.2"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@fastify/reply-from": "^11.0.0",
@@ -38,8 +38,8 @@
38
38
  "react-dom": "^18.3.1",
39
39
  "typescript": "^5.5.4",
40
40
  "ws": "^8.18.0",
41
- "@platformatic/composer": "2.19.0-alpha.1",
42
- "@platformatic/service": "2.19.0-alpha.1"
41
+ "@platformatic/composer": "2.19.0-alpha.2",
42
+ "@platformatic/service": "2.19.0-alpha.2"
43
43
  },
44
44
  "scripts": {
45
45
  "test": "npm run lint && borp --concurrency=1 --no-timeout",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/next/2.19.0-alpha.1.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/next/2.19.0-alpha.2.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Next.js Stackable",
5
5
  "type": "object",