Merged in feat/sw-3596-cache (pull request #3106)
feat(SW-3598): Don't call redis api if cachetime=0 * feat(SW-3596): no cache if ttl=0 * Fixed on set as well * No cache if redemption * Revert no cache if redemption Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { type CacheTime, type DataCache, getCacheTimeInSeconds } from "../Cache"
|
||||||
import {
|
import {
|
||||||
type CacheOrGetOptions,
|
type CacheOrGetOptions,
|
||||||
shouldGetFromCache,
|
shouldGetFromCache,
|
||||||
@@ -7,14 +8,23 @@ import { generateCacheKey } from "./generateCacheKey"
|
|||||||
import { get } from "./get"
|
import { get } from "./get"
|
||||||
import { set } from "./set"
|
import { set } from "./set"
|
||||||
|
|
||||||
import type { CacheTime, DataCache } from "../Cache"
|
|
||||||
|
|
||||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise<T>,
|
callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||||
ttl: CacheTime,
|
ttl: CacheTime,
|
||||||
opts?: CacheOrGetOptions
|
opts?: CacheOrGetOptions
|
||||||
) => {
|
) => {
|
||||||
|
let realTTL = ttl
|
||||||
|
|
||||||
|
const overrideTTL = function (cacheTime: CacheTime) {
|
||||||
|
realTTL = cacheTime
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getCacheTimeInSeconds(ttl) <= 0) {
|
||||||
|
cacheLogger.debug(`'Fetching ${key}' with ttl=0. Skipping cache!`)
|
||||||
|
return await callback(overrideTTL)
|
||||||
|
}
|
||||||
|
|
||||||
const cacheKey = generateCacheKey(key, {
|
const cacheKey = generateCacheKey(key, {
|
||||||
includeGitHashInKey: opts?.includeGitHashInKey ?? true,
|
includeGitHashInKey: opts?.includeGitHashInKey ?? true,
|
||||||
})
|
})
|
||||||
@@ -24,12 +34,6 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
|||||||
cachedValue = await get<T>(cacheKey)
|
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(overrideTTL)
|
const data = await callback(overrideTTL)
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
import {
|
||||||
|
type CacheTime,
|
||||||
|
type DataCache,
|
||||||
|
getCacheTimeInSeconds,
|
||||||
|
} from "../../Cache"
|
||||||
import {
|
import {
|
||||||
type CacheOrGetOptions,
|
type CacheOrGetOptions,
|
||||||
shouldGetFromCache,
|
shouldGetFromCache,
|
||||||
@@ -6,8 +11,6 @@ import { cacheLogger } from "../../logger"
|
|||||||
import { get } from "./get"
|
import { get } from "./get"
|
||||||
import { set } from "./set"
|
import { set } from "./set"
|
||||||
|
|
||||||
import type { CacheTime, DataCache } from "../../Cache"
|
|
||||||
|
|
||||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||||
key: string | string[],
|
key: string | string[],
|
||||||
callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise<T>,
|
callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||||
@@ -23,6 +26,11 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
|||||||
realTTL = cacheTime
|
realTTL = cacheTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getCacheTimeInSeconds(ttl) <= 0) {
|
||||||
|
cacheLogger.debug(`'Fetching ${key}' with ttl=0. Skipping cache!`)
|
||||||
|
return await callback(overrideTTL)
|
||||||
|
}
|
||||||
|
|
||||||
let cached: Awaited<T> | undefined = undefined
|
let cached: Awaited<T> | undefined = undefined
|
||||||
if (shouldGetFromCache(opts)) {
|
if (shouldGetFromCache(opts)) {
|
||||||
cached = await get(key)
|
cached = await get(key)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { type CacheTime, getCacheTimeInSeconds } from "../../Cache"
|
import { type CacheTime, getCacheTimeInSeconds } from "../../Cache"
|
||||||
|
import { cacheLogger } from "../../logger"
|
||||||
import { cacheMap } from "./cacheMap"
|
import { cacheMap } from "./cacheMap"
|
||||||
|
|
||||||
export async function set<T>(
|
export async function set<T>(
|
||||||
@@ -6,8 +7,14 @@ export async function set<T>(
|
|||||||
data: T,
|
data: T,
|
||||||
ttl: CacheTime
|
ttl: CacheTime
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const cacheTimeInSeconds = getCacheTimeInSeconds(ttl)
|
||||||
|
if (cacheTimeInSeconds <= 0) {
|
||||||
|
cacheLogger.info(`'Trying to set ${key}' with ttl=0. Skipping cache!`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
cacheMap.set(key, {
|
cacheMap.set(key, {
|
||||||
data: data,
|
data: data,
|
||||||
expiresAt: Date.now() + getCacheTimeInSeconds(ttl) * 1000,
|
expiresAt: Date.now() + cacheTimeInSeconds * 1000,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user