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.
This commit is contained in:
Fabien "egg" O'Carroll 2023-09-05 14:10:20 +07:00 committed by Fabien 'egg' O'Carroll
parent a753ebc0ab
commit cfef41cd8f
3 changed files with 17 additions and 2 deletions

View File

@ -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);

View File

@ -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",

View File

@ -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==