Skip to content

Commit

Permalink
optimize caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jowavp committed Oct 7, 2020
1 parent 8e5b7fe commit b1707b9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
29 changes: 19 additions & 10 deletions dist/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ function getDestination(access_token, destinationName, ds, jwtToken) {
const cacheKey = `${destinationName}__${access_token}__${jwtToken}`;
const cacheDest = destinationCache.get(cacheKey);
if (cacheDest) {
return (yield cacheDest).data;
return cacheDest;
}
try {
return yield (destinationCache.set(cacheKey, axios_1.default({
url: `${ds.uri}/destination-configuration/v1/destinations/${destinationName}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${access_token}`,
'X-user-token': jwtToken
},
responseType: 'json',
}))).data;
const destinationPromise = fetchDestination(access_token, destinationName, ds, jwtToken);
destinationCache.set(cacheKey, destinationPromise);
return destinationPromise;
/*
destinationCache.set(cacheKey, response)
return (await response).data;
Expand Down Expand Up @@ -132,6 +126,21 @@ function getService() {
}
return destination;
}
function fetchDestination(access_token, destinationName, ds, jwtToken) {
return __awaiter(this, void 0, void 0, function* () {
const destination = (yield axios_1.default({
url: `${ds.uri}/destination-configuration/v1/destinations/${destinationName}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${access_token}`,
'X-user-token': jwtToken
},
responseType: 'json',
})).data;
console.log(destination);
return destination;
});
}
function fetchToken(subscribedSubdomain, ds) {
return __awaiter(this, void 0, void 0, function* () {
const tokenBaseUrl = subscribedSubdomain ? `https://${subscribedSubdomain}.${ds.uaadomain}` : ds.url;
Expand Down
5 changes: 3 additions & 2 deletions dist/destinationCache.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export declare function get(key: string): any;
export declare function set(key: string, destination: any): any;
import { IDestinationConfiguration, IDestinationData } from "./destination";
export declare function get(key: string): Promise<IDestinationData<any>> | undefined;
export declare function set<T extends IDestinationConfiguration>(key: string, destination: Promise<IDestinationData<T>>): Promise<IDestinationData<T>> | undefined;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sap-cf-destconn",
"version": "0.0.22",
"version": "0.0.23",
"description": "NodeJS Library that wraps the calls for the destination and connectivity service",
"main": "dist/index.js",
"files": [
Expand Down
32 changes: 21 additions & 11 deletions src/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as destinationCache from './destinationCache';
export async function readDestination<T extends IDestinationConfiguration>(destinationName: string, authorizationHeader?: string, subscribedSubdomain?: string) {

const access_token = await createToken(getService(), subscribedSubdomain);

// if we have a JWT token, we send it to the destination service to generate the new authorization header
const jwtToken = /bearer /i.test(authorizationHeader || "") ? (authorizationHeader || "").replace(/bearer /i, "") : null;
return getDestination<T>(access_token, destinationName, getService(), jwtToken);
Expand Down Expand Up @@ -91,19 +90,15 @@ async function getDestination<T extends IDestinationConfiguration>(access_token:
const cacheDest = destinationCache.get(cacheKey);

if(cacheDest) {
return (await cacheDest).data;
return cacheDest;
}

try{
return await( destinationCache.set( cacheKey, axios({
url: `${ds.uri}/destination-configuration/v1/destinations/${destinationName}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${access_token}`,
'X-user-token': jwtToken
},
responseType: 'json',
}) ) ).data;

const destinationPromise = fetchDestination<T>(access_token, destinationName, ds, jwtToken);
destinationCache.set(cacheKey, destinationPromise );
return destinationPromise;

/*
destinationCache.set(cacheKey, response)
return (await response).data;
Expand Down Expand Up @@ -163,6 +158,21 @@ function getService(): IDestinationService {
return destination;
}

async function fetchDestination<T extends IDestinationConfiguration> (access_token: string, destinationName: string, ds: IDestinationService, jwtToken: string | null) {
const destination: IDestinationData<T> = (await axios({
url: `${ds.uri}/destination-configuration/v1/destinations/${destinationName}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${access_token}`,
'X-user-token': jwtToken
},
responseType: 'json',
}) ).data;

console.log(destination);
return destination;
}

async function fetchToken (subscribedSubdomain: string, ds: IDestinationService) {
const tokenBaseUrl = subscribedSubdomain ? `https://${subscribedSubdomain}.${ds.uaadomain}` : ds.url;
const token: tokenCache.IOauthToken = (await axios({
Expand Down
6 changes: 4 additions & 2 deletions src/destinationCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IDestinationConfiguration, IDestinationData } from "./destination";

const destinations: {[clientid: string]: {
validUntil: Date,
value: any
value: Promise<IDestinationData<any>>
}} = {};
const cacheLifetime = 60000;

Expand All @@ -13,7 +15,7 @@ export function get(key: string){
}
}

export function set(key: string, destination: any){
export function set<T extends IDestinationConfiguration>(key: string, destination: Promise<IDestinationData<T>>){
cleanCache();
if(destination) {
destinations[key] = {
Expand Down

0 comments on commit b1707b9

Please sign in to comment.