Skip to content

Commit

Permalink
cache manager expiremntal prop
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Dec 4, 2023
1 parent 6ddb7f4 commit 5725a35
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .changeset/four-carpets-watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"@saleor/app-sdk": minor
---

Saleor Cloud APL will now use built-in cache by default.
Added optional, experimental `cacheManager` property to CloudAPL constructor. By default it doesn't change any behavior
and it's meant to be used for internal testing.
9 changes: 7 additions & 2 deletions src/APL/saleor-cloud/saleor-cloud-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe("APL", () => {
expect(await apl.get("http://unknown-domain.example.com/graphql/")).toBe(undefined);
});

it("Uses cache when GET call is called 2nd time", async () => {
it("Uses cache when GET call is called 2nd time and cacheManager is set to Map", async () => {
fetchMock.mockResolvedValue({
status: 200,
ok: true,
Expand All @@ -175,7 +175,12 @@ describe("APL", () => {
}),
});

const apl = new SaleorCloudAPL(aplConfig);
const apl = new SaleorCloudAPL({
...aplConfig,
experimental: {
cacheManager: new Map(),
},
});

expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
Expand Down
38 changes: 30 additions & 8 deletions src/APL/saleor-cloud/saleor-cloud-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const debug = createAPLDebug("SaleorCloudAPL");
export type SaleorCloudAPLConfig = {
resourceUrl: string;
token: string;
cacheManager?: Map<string, AuthData>;
experimental?: {
cacheManager?: Map<string, AuthData>;
};
};

type CloudAPLAuthDataShape = {
Expand Down Expand Up @@ -85,7 +87,7 @@ export class SaleorCloudAPL implements APL {

private tracer: Tracer;

private cacheManager: Map<string, AuthData>;
private cacheManager?: Map<string, AuthData>;

constructor(config: SaleorCloudAPLConfig) {
this.resourceUrl = config.resourceUrl;
Expand All @@ -94,19 +96,39 @@ export class SaleorCloudAPL implements APL {
};

this.tracer = getOtelTracer();
this.cacheManager = config.cacheManager ?? new Map<string, AuthData>();
this.cacheManager = config?.experimental?.cacheManager;
}

private getUrlForDomain(saleorApiUrl: string) {
// API URL has to be base64url encoded
return `${this.resourceUrl}/${Buffer.from(saleorApiUrl).toString("base64url")}`;
}

private setToCacheIfExists(saleorApiUrl: string, authData: AuthData) {
if (!this.cacheManager) {
return;
}

this.cacheManager.set(authData.saleorApiUrl, authData);
}

private deleteFromCacheIfExists(saleorApiUrl: string) {
if (!this.cacheManager) {
return;
}

this.cacheManager.delete(saleorApiUrl);
}

private getFromCacheIfExists(saleorApiUrl: string) {
return this.cacheManager?.get(saleorApiUrl);
}

async get(saleorApiUrl: string): Promise<AuthData | undefined> {
const cachedData = this.cacheManager.get(saleorApiUrl);
const cachedData = this.getFromCacheIfExists(saleorApiUrl);

if (cachedData) {
debug("Returning authData from cache for saleorApiUrl %s", saleorApiUrl)
debug("Returning authData from cache for saleorApiUrl %s", saleorApiUrl);
return cachedData;
}

Expand Down Expand Up @@ -227,7 +249,7 @@ export class SaleorCloudAPL implements APL {

span.setAttribute("appId", authData.appId);

this.cacheManager.set(saleorApiUrl, authData);
this.setToCacheIfExists(authData.saleorApiUrl, authData);

span.end();

Expand Down Expand Up @@ -275,7 +297,7 @@ export class SaleorCloudAPL implements APL {

debug("Set command finished successfully for saleorApiUrl: %", authData.saleorApiUrl);

this.cacheManager.set(authData.saleorApiUrl, authData);
this.setToCacheIfExists(authData.saleorApiUrl, authData);

span.setStatus({
code: SpanStatusCode.OK,
Expand All @@ -296,7 +318,7 @@ export class SaleorCloudAPL implements APL {
headers: { "Content-Type": "application/json", ...this.headers },
});

this.cacheManager.delete(saleorApiUrl);
this.deleteFromCacheIfExists(saleorApiUrl);

debug(`Delete responded with ${response.status} code`);
} catch (error) {
Expand Down

0 comments on commit 5725a35

Please sign in to comment.