Merged in fix/cache-fixes (pull request #1555)
fix/cache: reduce cachetime when null response from CS. Fix fuzzy delete * fix/cache: reduce cachetime when null response from CS. Fix fuzzy delete Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -10,9 +10,13 @@ export const fetchAndCacheEntry = async (path: string, lang: Lang) => {
|
|||||||
|
|
||||||
return cache.cacheOrGet(
|
return cache.cacheOrGet(
|
||||||
cacheKey,
|
cacheKey,
|
||||||
async () => {
|
async (overrideTTL) => {
|
||||||
const { contentType, uid } = await resolveEntry(path, lang)
|
const { contentType, uid } = await resolveEntry(path, lang)
|
||||||
|
|
||||||
|
if (!contentType || !uid) {
|
||||||
|
overrideTTL?.("10m")
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
contentType,
|
contentType,
|
||||||
uid,
|
uid,
|
||||||
|
|||||||
@@ -64,7 +64,9 @@ export type DataCache = {
|
|||||||
*/
|
*/
|
||||||
cacheOrGet: <T>(
|
cacheOrGet: <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
getDataFromSource: () => Promise<T>,
|
getDataFromSource: (
|
||||||
|
overrideTTL?: (cacheTime: CacheTime) => void
|
||||||
|
) => Promise<T>,
|
||||||
ttl: CacheTime
|
ttl: CacheTime
|
||||||
) => Promise<T>
|
) => Promise<T>
|
||||||
|
|
||||||
|
|||||||
@@ -7,21 +7,27 @@ import { set } from "./set"
|
|||||||
|
|
||||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
callback: () => Promise<T>,
|
callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||||
ttl: CacheTime
|
ttl: CacheTime
|
||||||
) => {
|
) => {
|
||||||
const cacheKey = generateCacheKey(key)
|
const cacheKey = generateCacheKey(key)
|
||||||
const cachedValue = await get<T>(cacheKey)
|
const cachedValue = await get<T>(cacheKey)
|
||||||
|
|
||||||
|
let realTTL = ttl
|
||||||
|
|
||||||
|
const overrideTTL = function (cacheTime: CacheTime) {
|
||||||
|
realTTL = cacheTime
|
||||||
|
}
|
||||||
|
|
||||||
if (!cachedValue) {
|
if (!cachedValue) {
|
||||||
const perf = performance.now()
|
const perf = performance.now()
|
||||||
const data = await callback()
|
const data = await callback(overrideTTL)
|
||||||
|
|
||||||
cacheLogger.debug(
|
cacheLogger.debug(
|
||||||
`Getting data '${cacheKey}' took ${(performance.now() - perf).toFixed(2)}ms`
|
`Getting data '${cacheKey}' took ${(performance.now() - perf).toFixed(2)}ms`
|
||||||
)
|
)
|
||||||
|
|
||||||
await set<T>(cacheKey, data, ttl)
|
await set<T>(cacheKey, data, realTTL)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,15 @@ import { cacheLogger } from "../logger"
|
|||||||
import { API_KEY } from "./client"
|
import { API_KEY } from "./client"
|
||||||
import { getCacheEndpoint } from "./endpoints"
|
import { getCacheEndpoint } from "./endpoints"
|
||||||
|
|
||||||
export async function deleteKey<T>(key: string) {
|
export async function deleteKey<T>(key: string, opts?: { fuzzy?: boolean }) {
|
||||||
const perf = performance.now()
|
const perf = performance.now()
|
||||||
|
const endpoint = getCacheEndpoint(key)
|
||||||
|
|
||||||
const response = await fetch(getCacheEndpoint(key), {
|
if (opts?.fuzzy) {
|
||||||
|
endpoint.searchParams.set("fuzzy", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(endpoint, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
cache: "no-cache",
|
cache: "no-cache",
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -6,13 +6,18 @@ import { set } from "./set"
|
|||||||
|
|
||||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
callback: () => Promise<T>,
|
callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||||
ttl: CacheTime
|
ttl: CacheTime
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
if (Array.isArray(key)) {
|
if (Array.isArray(key)) {
|
||||||
key = key.join("-")
|
key = key.join("-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let realTTL = ttl
|
||||||
|
const overrideTTL = function (cacheTime: CacheTime) {
|
||||||
|
realTTL = cacheTime
|
||||||
|
}
|
||||||
|
|
||||||
const cached = await get(key)
|
const cached = await get(key)
|
||||||
|
|
||||||
if (cached) {
|
if (cached) {
|
||||||
@@ -22,8 +27,8 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
|||||||
cacheLogger.debug(`Miss for key '${key}'`)
|
cacheLogger.debug(`Miss for key '${key}'`)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await callback()
|
const data = await callback(overrideTTL)
|
||||||
await set(key, data, ttl)
|
await set(key, data, realTTL)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { cacheLogger } from "../../logger"
|
|||||||
|
|
||||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
callback: () => Promise<T>,
|
callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||||
ttl: CacheTime
|
ttl: CacheTime
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
if (!Array.isArray(key)) {
|
if (!Array.isArray(key)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user