Removed node-cache dependancy
This commit is contained in:
parent
3b3b6951fe
commit
db6b331380
|
@ -13,7 +13,6 @@
|
|||
"accept-language": "^3.0.18",
|
||||
"astro": "4.10.2",
|
||||
"astro-icon": "^1.1.0",
|
||||
"node-cache": "^5.1.2",
|
||||
"tippy.js": "^6.3.7",
|
||||
"ua-parser-js": "^1.0.38"
|
||||
},
|
||||
|
@ -4104,14 +4103,6 @@
|
|||
"url": "https://github.com/chalk/wrap-ansi?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/clone": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/clsx": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz",
|
||||
|
@ -6545,17 +6536,6 @@
|
|||
"url": "https://opencollective.com/unified"
|
||||
}
|
||||
},
|
||||
"node_modules/node-cache": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||
"dependencies": {
|
||||
"clone": "2.x"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/node-releases": {
|
||||
"version": "2.0.14",
|
||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz",
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
"accept-language": "^3.0.18",
|
||||
"astro": "4.10.2",
|
||||
"astro-icon": "^1.1.0",
|
||||
"node-cache": "^5.1.2",
|
||||
"tippy.js": "^6.3.7",
|
||||
"ua-parser-js": "^1.0.38"
|
||||
},
|
||||
|
|
|
@ -4,16 +4,9 @@ import {
|
|||
type EndpointWebsiteConfig,
|
||||
} from "src/shared/payload/payload-sdk";
|
||||
import { getPayloadSDK } from "src/shared/payload/payload-sdk";
|
||||
import NodeCache from "node-cache";
|
||||
|
||||
const REFRESH_FREQUENCY_IN_SEC = 60;
|
||||
const nodeCache = new NodeCache({
|
||||
checkperiod: REFRESH_FREQUENCY_IN_SEC,
|
||||
deleteOnExpire: true,
|
||||
forceString: true,
|
||||
maxKeys: 1,
|
||||
});
|
||||
const TOKEN_KEY = "token";
|
||||
let token: string | undefined = undefined;
|
||||
let expiration: number | undefined = undefined;
|
||||
|
||||
export const payload = getPayloadSDK({
|
||||
apiURL: import.meta.env.PAYLOAD_API_URL,
|
||||
|
@ -21,22 +14,20 @@ export const payload = getPayloadSDK({
|
|||
password: import.meta.env.PAYLOAD_PASSWORD,
|
||||
tokenCache: {
|
||||
get: () => {
|
||||
const cachedToken = nodeCache.get<string>(TOKEN_KEY);
|
||||
if (cachedToken !== undefined) {
|
||||
const cachedTokenTtl = nodeCache.getTtl(TOKEN_KEY) as number;
|
||||
const diffInMinutes = Math.floor((cachedTokenTtl - Date.now()) / 1000 / 60);
|
||||
console.log("Retrieved token from cache. TTL is", diffInMinutes, "minutes.");
|
||||
return cachedToken;
|
||||
}
|
||||
console.log("No token to be retrieved or the token expired");
|
||||
if (!token) return undefined;
|
||||
if (!expiration || expiration < Date.now()) {
|
||||
console.log("[PayloadSDK] No token to be retrieved or the token expired");
|
||||
return undefined;
|
||||
}
|
||||
const diffInMinutes = Math.floor((expiration - Date.now()) / 1000 / 60);
|
||||
console.log("[PayloadSDK] Retrieved token from cache. TTL is", diffInMinutes, "minutes.");
|
||||
return token;
|
||||
},
|
||||
set: (token, exp) => {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const ttl = Math.floor(exp - now - REFRESH_FREQUENCY_IN_SEC * 2);
|
||||
const ttlInMinutes = Math.floor(ttl / 60);
|
||||
console.log("Token was refreshed. TTL is", ttlInMinutes, "minutes.");
|
||||
nodeCache.set(TOKEN_KEY, token, ttl);
|
||||
set: (newToken, newExpiration) => {
|
||||
token = newToken;
|
||||
expiration = newExpiration * 1000;
|
||||
const diffInMinutes = Math.floor((expiration - Date.now()) / 1000 / 60);
|
||||
console.log("[PayloadSDK] New token set. TTL is", diffInMinutes, "minutes.");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue