Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.
6 changes: 5 additions & 1 deletion src/configure/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RemoteGitHubWorkflowConfigurer } from './configurers/remoteGitHubWorkfl
import { ResourceSelectorFactory } from './configurers/ResourceSelectorFactory';
import { AssetHandler } from './helper/AssetHandler';
import { getAzureSession, getSubscriptionSession } from './helper/azureSessionHelper';
import { Cache } from './helper/Cache';
import { ControlProvider } from './helper/controlProvider';
import { AzureDevOpsHelper } from './helper/devOps/azureDevOpsHelper';
import { GitHubProvider } from './helper/gitHubHelper';
Expand Down Expand Up @@ -96,6 +97,7 @@ class Orchestrator {
private pipelineType: PipelineType;

public constructor() {
Cache.getCache().clearCache();
this.inputs = new WizardInputs();
this.controlProvider = new ControlProvider();
UniqueResourceNameSuffix = uuid().substr(0, 5);
Expand Down Expand Up @@ -123,6 +125,7 @@ class Orchestrator {
telemetryHelper.setTelemetry(TelemetryKeys.resourceType, this.inputs.targetResource.resource.type);
if (targetType === TargetResourceType.WebApp) {
this.context['resourceId'] = this.inputs.targetResource.resource.id;
Cache.getCache().put(this.inputs.targetResource.resource.id, this.inputs.targetResource.resource);
telemetryHelper.setTelemetry(TelemetryKeys.resourceKind, this.inputs.targetResource.resource.kind);
telemetryHelper.setTelemetry(TelemetryKeys.resourceIdHash, Utilities.createSha256Hash(this.inputs.targetResource.resource.id));
}
Expand Down Expand Up @@ -277,10 +280,11 @@ class Orchestrator {
if (!!node) {
if (await this.extractAzureResourceFromNode(node)) {
this.context['isResourceAlreadySelected'] = true;
Cache.getCache().put(this.inputs.targetResource.resource.id, this.inputs.targetResource.resource);
this.context['resourceId'] = this.inputs.targetResource.resource.id;
} else {
if (node.fsPath) {
//right click on a folder
// right click on a folder
this.workspacePath = node.fsPath;
telemetryHelper.setTelemetry(TelemetryKeys.SourceRepoLocation, SourceOptions.CurrentWorkspace);
}
Expand Down
42 changes: 42 additions & 0 deletions src/configure/helper/Cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ICache } from "./ICache";

export class Cache implements ICache {
public static getCache(): ICache {
if (!this.cache) {
this.cache = new Cache();
}
return this.cache;
}

private static cache: Cache;
private store: Map<string, any>;

constructor() {
this.store = new Map<string, any>();
}

public getValue(key: string): any {
key = this.sanitizeKey(key);
if (this.store.has(key)) {
return this.store.get(key);
}
return null;
}

public clearCache() {
this.store = new Map<string, any>();
}

public put(key: string, value: any): void {
key = this.sanitizeKey(key);
this.store.set(key, value);
}

private sanitizeKey(key: string): string {
const apiVersionIndex = key.indexOf('?api-version=');
if (apiVersionIndex >= 0 && !(key.indexOf('&') > apiVersionIndex)) {
key = key.substring(0, key.indexOf('?api-version='));
}
return key;
}
}
5 changes: 5 additions & 0 deletions src/configure/helper/ICache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ICache {
getValue(key: string): any;
put(key: string, value: any): void;
clearCache(): void;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JSONPath } from 'jsonpath-plus';
import { isNullOrUndefined } from 'util';
import { ArmRestClient } from "../../clients/azure/armRestClient";
import { Cache } from '../../helper/Cache';
import { MustacheHelper } from "../../helper/mustacheHelper";
import { DataSource } from "../../model/Contracts";
import { AzureSession, QuickPickItemWithData, StringMap } from "../../model/models";
Expand Down Expand Up @@ -38,17 +39,23 @@ export class DataSourceUtility {
var view = { inputs: inputs };
var armUri = MustacheHelper.render(dataSource.endpointUrlStem, view);
var httpMethod = dataSource.httpMethod || "GET";
var requestBody = !!dataSource.requestBody ? MustacheHelper.render(dataSource.requestBody, view) : null;
let amrClient = new ArmRestClient(azureSession);
return amrClient.fetchArmData(armUri, httpMethod, JSON.parse(requestBody))
.then((response: any) => {
return this.evaluateDataSourceResponse(dataSource, response, view);
});
const requestBody = !!dataSource.requestBody ? MustacheHelper.render(dataSource.requestBody, view) : null;
const armClient = new ArmRestClient(azureSession);
let result: any;
if (httpMethod == "GET" && Cache.getCache().getValue(armUri)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking at everyplace, whether the request is cached or not, Should we create a cache client which will check the URL in the map and if it is already processed then return cache result like https://www.npmjs.com/package/cacheable-request

result = Cache.getCache().getValue(armUri);
} else {
result = await armClient.fetchArmData(armUri, httpMethod, JSON.parse(requestBody));
if (httpMethod == "GET") {
Cache.getCache().put(armUri, result);
}
}
return this.evaluateDataSourceResponse(dataSource, result, view);
}

private static evaluateDataSourceResponse(dataSource: DataSource, response: any, view: { inputs: StringMap<any> }): any {
if (!!dataSource.resultSelector) {
var resultSelector = MustacheHelper.render(dataSource.resultSelector, view);
const resultSelector = MustacheHelper.render(dataSource.resultSelector, view);
response = JSONPath({ json: response, path: resultSelector, wrap: false, flatten: true });
if (response === "" || response === isNullOrUndefined) {
return null;
Expand Down