From cfef41cd8f80b81f935ae63332649e5409aad75e Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Tue, 5 Sep 2023 14:10:20 +0700 Subject: [PATCH] Added support for custom cache key generation refs https://github.com/TryGhost/Arch/issues/83 This allows endpoints to implement their own key generation, with access to the frame object they can be smart about key generation and use only options and context values that are appropriate. --- ghost/api-framework/lib/pipeline.js | 9 ++++++++- ghost/core/package.json | 1 + yarn.lock | 9 ++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ghost/api-framework/lib/pipeline.js b/ghost/api-framework/lib/pipeline.js index ebbadf384a..7ea6434306 100644 --- a/ghost/api-framework/lib/pipeline.js +++ b/ghost/api-framework/lib/pipeline.js @@ -1,5 +1,6 @@ const debug = require('@tryghost/debug')('pipeline'); const _ = require('lodash'); +const stringify = require('json-stable-stringify'); const errors = require('@tryghost/errors'); const {sequence} = require('@tryghost/promise'); @@ -229,7 +230,13 @@ const pipeline = (apiController, apiUtils, apiType) => { frame.docName = docName; frame.method = method; - let cacheKey = JSON.stringify(frame.options); + let cacheKeyData = frame.options; + if (apiImpl.generateCacheKeyData) { + cacheKeyData = await apiImpl.generateCacheKeyData(frame); + } + + const cacheKey = stringify(cacheKeyData); + if (apiImpl.cache) { const response = await apiImpl.cache.get(cacheKey); diff --git a/ghost/core/package.json b/ghost/core/package.json index 9dc4e1d429..61d6013633 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -196,6 +196,7 @@ "intl-messageformat": "5.4.3", "js-yaml": "4.1.0", "jsdom": "22.1.0", + "json-stable-stringify": "1.0.2", "jsonpath": "1.1.1", "jsonwebtoken": "8.5.1", "juice": "9.1.0", diff --git a/yarn.lock b/yarn.lock index 8046e1367a..05f83fedf8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -21255,6 +21255,13 @@ json-stable-stringify-without-jsonify@^1.0.1: resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651" integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw== +json-stable-stringify@1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/json-stable-stringify/-/json-stable-stringify-1.0.2.tgz#e06f23128e0bbe342dc996ed5a19e28b57b580e0" + integrity sha512-eunSSaEnxV12z+Z73y/j5N37/In40GK4GmsSy+tEHJMxknvqnA7/djeYtAgW0GsWHUfg+847WJjKaEylk2y09g== + dependencies: + jsonify "^0.0.1" + json-stable-stringify@^1.0.0, json-stable-stringify@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz#9a759d39c5f2ff503fd5300646ed445f88c4f9af" @@ -21321,7 +21328,7 @@ jsonfile@^6.0.1: optionalDependencies: graceful-fs "^4.1.6" -jsonify@~0.0.0: +jsonify@^0.0.1, jsonify@~0.0.0: version "0.0.1" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.1.tgz#2aa3111dae3d34a0f151c63f3a45d995d9420978" integrity sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==