Skip to content

Commit

Permalink
remove readAt and expireTimestamp from cache objects (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogorman9 committed Jul 17, 2023
1 parent a2f3e3e commit 58cab26
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 58 deletions.
7 changes: 3 additions & 4 deletions __tests__/core/cache.ts
Expand Up @@ -95,7 +95,7 @@ describe("Core", () => {
await cache.load("testKey_slow");
throw new Error("should not get here");
} catch (error) {
expect(String(error)).toEqual("Error: Object expired");
expect(String(error)).toEqual("Error: Object not found");
}
});

Expand Down Expand Up @@ -127,8 +127,7 @@ describe("Core", () => {
await cache.load(key);
throw new Error("should not get here");
} catch (error) {
// expect(String(error)).toMatch(/Error: Object expired/)
expect(error).toBeTruthy();
expect(String(error)).toMatch(/Error: Object not found/);
}
});

Expand Down Expand Up @@ -161,7 +160,7 @@ describe("Core", () => {
loadResp = await cache.load("testKey_slow");
throw new Error("should not get here");
} catch (error) {
expect(String(error)).toEqual("Error: Object expired");
expect(String(error)).toEqual("Error: Object not found");
}
});

Expand Down
2 changes: 0 additions & 2 deletions src/actions/cacheTest.ts
Expand Up @@ -24,9 +24,7 @@ export class CacheTest extends Action {
loadResp: {
key: "cacheTest_key",
value: "value",
expireTimestamp: 1420953274716,
createdAt: 1420953269716,
readAt: null as number,
},
deleteResp: true,
},
Expand Down
57 changes: 5 additions & 52 deletions src/modules/cache.ts
Expand Up @@ -5,16 +5,12 @@ export namespace cache {
export enum CacheErrorMessages {
locked = "Object locked",
notFound = "Object not found",
expired = "Object expired",
}

export interface CacheObject {
key: string;
value: any;
expireTimestamp: number;
createdAt: number;
lastReadAt: number;
readAt?: number;
}

export interface CacheOptions {
Expand Down Expand Up @@ -152,14 +148,7 @@ export namespace cache {
const count = Object.keys(data).length;

const saveDumpedElement = async (key: string, content: any) => {
const parsedContent = JSON.parse(content);
await client().set(key, content);
if (parsedContent.expireTimestamp) {
const expireTimeSeconds = Math.ceil(
(parsedContent.expireTimestamp - new Date().getTime()) / 1000,
);
await client().expire(key, expireTimeSeconds);
}
await client().set(key, content, "KEEPTTL");
};

Object.keys(data).forEach((key) => {
Expand Down Expand Up @@ -191,48 +180,21 @@ export namespace cache {

if (!cacheObj) throw new Error(CacheErrorMessages.notFound);

if (
cacheObj.expireTimestamp &&
cacheObj.expireTimestamp < new Date().getTime()
) {
throw new Error(CacheErrorMessages.expired);
}

const lastReadAt = cacheObj.readAt;
let expireTimeSeconds: number;
cacheObj.readAt = new Date().getTime();

if (cacheObj.expireTimestamp) {
if (options.expireTimeMS) {
cacheObj.expireTimestamp = new Date().getTime() + options.expireTimeMS;
expireTimeSeconds = Math.ceil(options.expireTimeMS / 1000);
} else {
expireTimeSeconds = Math.floor(
(cacheObj.expireTimestamp - new Date().getTime()) / 1000,
);
}
}

lockOk = await cache.checkLock(key, options.retry);
if (lockOk !== true) throw new Error(CacheErrorMessages.locked);

await client().set(redisPrefix + key, JSON.stringify(cacheObj));
if (expireTimeSeconds) {
await client().expire(redisPrefix + key, expireTimeSeconds);
if (options.expireTimeMS) {
await client().pexpire(redisPrefix + key, options.expireTimeMS);
return {
key,
value: cacheObj.value,
expireTimestamp: cacheObj.expireTimestamp,
createdAt: cacheObj.createdAt,
lastReadAt,
};
} else {
return {
key,
value: cacheObj.value,
expireTimestamp: cacheObj.expireTimestamp,
createdAt: cacheObj.createdAt,
lastReadAt,
};
}
}
Expand Down Expand Up @@ -262,26 +224,17 @@ export namespace cache {
value: any,
expireTimeMS?: number,
): Promise<boolean> {
let expireTimeSeconds = null;
let expireTimestamp = null;
if (expireTimeMS !== null) {
expireTimeSeconds = Math.ceil(expireTimeMS / 1000);
expireTimestamp = new Date().getTime() + expireTimeMS;
}

const cacheObj = {
value: value,
expireTimestamp: expireTimestamp,
createdAt: new Date().getTime(),
readAt: null as number,
};

const lockOk = await cache.checkLock(key, null);
if (!lockOk) throw new Error(CacheErrorMessages.locked);

await client().set(redisPrefix + key, JSON.stringify(cacheObj));
if (expireTimeSeconds) {
await client().expire(redisPrefix + key, expireTimeSeconds);
if (expireTimeMS) {
await client().pexpire(redisPrefix + key, expireTimeMS);
}
return true;
}
Expand Down

0 comments on commit 58cab26

Please sign in to comment.