/
destination-cache.ts
73 lines (68 loc) · 2.43 KB
/
destination-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { MapType } from '@sap-cloud-sdk/util';
import { tenantId, userId } from '../util';
import { Cache, IsolationStrategy } from './cache';
import { DestinationsByType } from './destination-accessor-types';
import { Destination } from './destination-service-types';
const DestinationCache = (cache: Cache<Destination>) => ({
retrieveDestinationFromCache: (
decodedJwt: MapType<any>,
name: string,
isolation: IsolationStrategy
): Destination | undefined =>
cache.get(getDestinationCacheKey(decodedJwt, name, isolation)),
cacheRetrievedDestinations: (
decodedJwt: MapType<any>,
retrievedDestinations: DestinationsByType,
isolation: IsolationStrategy
): void => {
[
...retrievedDestinations.instance,
...retrievedDestinations.subaccount
].forEach(destination => {
if (!destination.name) {
throw new Error('The destination name is undefined.');
}
const key = getDestinationCacheKey(
decodedJwt,
destination.name,
isolation
);
cache.set(key, destination);
});
},
clear: () => {
cache.clear();
},
getCacheInstance: () => cache
});
/**
* Calculates a cache key based on the jwt and destination name for the given isolation strategy.
* Cache keys for strategies are non-overlapping, i.e. using a cache key for strategy [[IsolationStrategy.Tenant]]
* will not result in a cache hit for a destination that has been cached with strategy [[IsolationStrategy.Tenant_User]].
*
* @param decodedJwt - The decoded JWT of the current request.
* @param destinationName - The name of the destination.
* @param isolationStrategy - The strategy used to isolate cache entries.
* @returns The cache key.
* @hidden
*/
export function getDestinationCacheKey(
decodedJwt: MapType<any>,
destinationName: string,
isolationStrategy: IsolationStrategy
): string {
switch (isolationStrategy) {
case IsolationStrategy.No_Isolation:
return `::${destinationName}`;
case IsolationStrategy.Tenant_User:
return `${tenantId(decodedJwt)}:${userId(decodedJwt)}:${destinationName}`;
case IsolationStrategy.User:
return `:${userId(decodedJwt)}:${destinationName}`;
default:
return `${tenantId(decodedJwt)}::${destinationName}`;
}
}
export const destinationCache = DestinationCache(
new Cache<Destination>({ hours: 0, minutes: 5, seconds: 0 })
);