Skip to content

Commit

Permalink
debug ttl v1.1.1 (#19)
Browse files Browse the repository at this point in the history
* debug ttl v1.1.0

* fix get
  • Loading branch information
PabloSzx committed Jul 14, 2022
1 parent f27a761 commit 11c5727
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@soundxyz/response-cache",
"version": "1.0.3",
"version": "1.1.1",
"description": "Heavily inspired by @envelop/response-cache",
"keywords": [
"envelop",
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type Cache = {
ttl: number
): PromiseOrValue<void>;
/** get a cached response */
get(id: string): PromiseOrValue<Maybe<ExecutionResult>>;
get(id: string): PromiseOrValue<[Maybe<ExecutionResult>, ({ ttl?: number } | undefined)?]>;
/** invalidate operations via typename or id */
invalidate(entities: Iterable<CacheEntityRecord>): PromiseOrValue<void>;
/** Function to be called when the operation couldn't be cached, by default, if the execution had errors */
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export function useResponseCache({
sessionId: session(ctx.args.contextValue),
});

const cachedResponse = await cache.get(operationId);
const [cachedResponse, extra] = await cache.get(operationId);

if (cachedResponse != null) {
if (includeExtensionMetadata) {
Expand All @@ -331,6 +331,7 @@ export function useResponseCache({
...cachedResponse.extensions,
responseCache: {
hit: true,
ttl: extra?.ttl,
},
},
});
Expand Down
59 changes: 50 additions & 9 deletions src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export type RedisCacheParameter = {
* By default `operations` is concatenated with the responseId e.g. `operations:arZm3tCKgGmpu+a5slrpSH9vjSQ=`
*/
buildRedisOperationResultCacheKey?: BuildRedisOperationResultCacheKey;
/**
* Debug TTL of existing cache results
*
* @default false
*/
debugTtl?: boolean;
};

function gracefullyFail(err: unknown) {
Expand All @@ -49,6 +55,7 @@ export const createRedisCache = (params: RedisCacheParameter): Cache => {
const lockDuration = params.redlock?.duration ?? 5000;
const lockRetryDelay = params.redlock?.settings?.retryDelay ?? 250;
const lockRetryCount = lockSettings?.retryCount ?? (lockDuration / lockRetryDelay) * 2;
const debugTtl = params.debugTtl ?? false;

const buildRedisEntityId = params?.buildRedisEntityId ?? defaultBuildRedisEntityId;
const buildRedisOperationResultCacheKey =
Expand Down Expand Up @@ -105,16 +112,50 @@ export const createRedisCache = (params: RedisCacheParameter): Cache => {
}

function getFromRedis<T>(responseId: string) {
return ConcurrentCachedCall<[T | null, boolean]>(responseId, async () => {
return ConcurrentCachedCall<[T | null, { ok: boolean; ttl?: number }]>(responseId, async () => {
let ok = true;

if (debugTtl) {
const resultWithTtl = await store
.pipeline()
.get(responseId)
.ttl(responseId)
.exec()
.catch(gracefullyFail);

if (!resultWithTtl || !resultWithTtl[0] || !resultWithTtl[1]) {
return [null, { ok: false, ttl: undefined }];
}

const [[resultError, result], [ttlError, ttlRedis]] = resultWithTtl;

const ttl = typeof ttlRedis === "number" ? ttlRedis : undefined;

if (resultError) {
gracefullyFail(resultError);
ok = false;
}

if (ttlError) {
gracefullyFail(ttlError);
ok = false;
}

if (result != null && typeof result === "string") {
return [JSON.parse(result), { ok, ttl }];
}

return [null, { ok, ttl }];
}

const result = await store.get(responseId).catch((err) => {
ok = false;
return gracefullyFail(err);
});

if (result != null) return [JSON.parse(result), ok];
if (result != null) return [JSON.parse(result), { ok }];

return [null, ok];
return [null, { ok }];
});
}

Expand Down Expand Up @@ -168,13 +209,13 @@ export const createRedisCache = (params: RedisCacheParameter): Cache => {
responseIdLocks[responseId] = null;
},
async get(responseId) {
const [firstTry, redisOk] = await getFromRedis<ExecutionResult>(responseId);
const [firstTry, { ok, ttl }] = await getFromRedis<ExecutionResult>(responseId);

if (!redisOk) return null;
if (!ok) return [null];

if (firstTry) return firstTry;
if (firstTry) return [firstTry, { ttl }];

if (!redLock) return null;
if (!redLock) return [null];

const lock = await redLock
.acquire(["lock:" + responseId], lockDuration, {
Expand All @@ -199,9 +240,9 @@ export const createRedisCache = (params: RedisCacheParameter): Cache => {
// Any lock that took more than 1 attempt should be released right away for the other readers
if (lock && lock.attempts.length > 1) lock.release().catch(console.error);
// If the lock was first attempt, skip the second get try, and go right to execute
else if (lock?.attempts.length === 1) return null;
else if (lock?.attempts.length === 1) return [null];

return getFromRedis<ExecutionResult>(responseId).then((v) => v[0]);
return getFromRedis<ExecutionResult>(responseId).then((v) => [v[0], { ttl: v[1].ttl }]);
},
async invalidate(entitiesToRemove) {
const invalidationKeys: string[] = [];
Expand Down

0 comments on commit 11c5727

Please sign in to comment.