Skip to content

Commit

Permalink
fix(platform-cache): fix issue with the threshold management
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 5, 2022
1 parent da60c20 commit f48be8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PlatformCache, PlatformCacheInterceptor} from "@tsed/platform-cache";
import {PlatformTest} from "@tsed/common";
import {isClass} from "@tsed/core";
import {serialize} from "@tsed/json-mapper";
import {PlatformCache, PlatformCacheInterceptor} from "@tsed/platform-cache";

const defaultKeyResolver = (args: any[]) => {
return args.map((arg: any) => (isClass(arg) ? JSON.stringify(serialize(arg)) : arg)).join(":");
Expand Down Expand Up @@ -229,14 +229,7 @@ describe("PlatformCacheInterceptor", () => {

await new Promise((resolve) => setTimeout(resolve, 100));

expect(interceptor.canRefreshInBackground).toHaveBeenCalledWith(
"Test:test:value",
{
refreshThreshold: 1000,
ttl: 10000
},
expect.any(Function)
);
expect(interceptor.canRefreshInBackground).not.toHaveBeenCalled();
expect(cache.getCachedObject).toHaveBeenCalledWith("Test:test:value");
expect(cache.setCachedObject).toHaveBeenCalledWith(
"Test:test:value",
Expand All @@ -256,10 +249,11 @@ describe("PlatformCacheInterceptor", () => {
set: jest.fn().mockResolvedValue(false),
del: jest.fn().mockResolvedValue(true),
calculateTTL: jest.fn().mockImplementation((result: any, ttl: any) => ttl),
getCachedObject: jest.fn().mockResolvedValue(undefined),
getCachedObject: jest.fn().mockResolvedValue({data: JSON.stringify({})}),
setCachedObject: jest.fn().mockResolvedValue("test"),
defaultKeyResolver: () => defaultKeyResolver
};

const interceptor = await PlatformTest.invoke<PlatformCacheInterceptor>(PlatformCacheInterceptor, [
{
token: PlatformCache,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {isClass, isString, nameOf, Store} from "@tsed/core";
import {BaseContext, DIContext, Inject, Interceptor, InterceptorContext, InterceptorMethods, InterceptorNext} from "@tsed/di";
import {deserialize, serialize} from "@tsed/json-mapper";
import {JsonEntityStore} from "@tsed/schema";
import {Logger} from "@tsed/logger";
import {JsonEntityStore} from "@tsed/schema";
import {IncomingMessage, ServerResponse} from "http";
import {PlatformCachedObject} from "../interfaces/PlatformCachedObject";
import {PlatformCacheOptions} from "../interfaces/PlatformCacheOptions";
Expand Down Expand Up @@ -63,14 +63,22 @@ export class PlatformCacheInterceptor implements InterceptorMethods {
const {type, ttl, collectionType, refreshThreshold, keyArgs, args} = this.getOptions(context);
const key = [nameOf(context.target), context.propertyKey, keyArgs].join(":");

const cachedObject = await this.cache.getCachedObject(key);

const set = (result: any) => {
const calculatedTTL = this.cache.calculateTTL(result, ttl);
const data = serialize(result, {type, collectionType});
this.cache.setCachedObject(key, data, {args, ttl: calculatedTTL});
};

const cachedObject = await this.cache.getCachedObject(key);

if (!cachedObject) {
const result = await next();

set(result);

return result;
}

this.canRefreshInBackground(key, {refreshThreshold, ttl}, async () => {
const result = await next();
await set(result);
Expand All @@ -82,17 +90,9 @@ export class PlatformCacheInterceptor implements InterceptorMethods {
})
);

if (cachedObject) {
const {data} = cachedObject;

return deserialize(JSON.parse(data), {collectionType, type});
}

const result = await next();

set(result);
const {data} = cachedObject;

return result;
return deserialize(JSON.parse(data), {collectionType, type});
}

async cacheResponse(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext) {
Expand Down

0 comments on commit f48be8c

Please sign in to comment.