Selaa lähdekoodia

refactor(core): Make RequestContextCacheService sync where possible

Michael Bromley 4 vuotta sitten
vanhempi
sitoutus
179679f4b9

+ 21 - 13
packages/core/src/cache/request-context-cache.service.spec.ts

@@ -8,31 +8,39 @@ describe('Request context cache', () => {
         cache = new RequestContextCacheService();
     });
 
-    it('stores and retrieves a multiple values', async () => {
+    it('stores and retrieves a multiple values', () => {
         const ctx = RequestContext.empty();
 
-        await cache.set(ctx, 'test', 1);
-        await cache.set(ctx, 'test2', 2);
-        expect(await cache.get(ctx, 'test')).toBe(1);
-        expect(await cache.get(ctx, 'test2')).toBe(2);
+        cache.set(ctx, 'test', 1);
+        cache.set(ctx, 'test2', 2);
+        expect(cache.get(ctx, 'test')).toBe(1);
+        expect(cache.get(ctx, 'test2')).toBe(2);
     });
 
-    it('can use objects as keys', async () => {
+    it('uses getDefault function', async () => {
+        const ctx = RequestContext.empty();
+        const result = cache.get(ctx, 'test', async () => 'foo');
+
+        expect(result instanceof Promise).toBe(true);
+        expect(await result).toBe('foo');
+    });
+
+    it('can use objects as keys', () => {
         const ctx = RequestContext.empty();
 
         const x = {};
-        await cache.set(ctx, x, 1);
-        expect(await cache.get(ctx, x)).toBe(1);
+        cache.set(ctx, x, 1);
+        expect(cache.get(ctx, x)).toBe(1);
     });
 
-    it('uses separate stores per context', async () => {
+    it('uses separate stores per context', () => {
         const ctx = RequestContext.empty();
         const ctx2 = RequestContext.empty();
 
-        await cache.set(ctx, 'test', 1);
-        await cache.set(ctx2, 'test', 2);
+        cache.set(ctx, 'test', 1);
+        cache.set(ctx2, 'test', 2);
 
-        expect(await cache.get(ctx, 'test')).toBe(1);
-        expect(await cache.get(ctx2, 'test')).toBe(2);
+        expect(cache.get(ctx, 'test')).toBe(1);
+        expect(cache.get(ctx2, 'test')).toBe(2);
     });
 });

+ 22 - 7
packages/core/src/cache/request-context-cache.service.ts

@@ -1,20 +1,35 @@
 import { RequestContext } from '../api';
 
+/**
+ * @description
+ * This service is used to cache arbitrary data relative to an ongoing request.
+ * It does this by using a WeakMap bound to the current RequestContext, so the cached
+ * data is available for the duration of the request. Once the request completes, the
+ * cached data will be automatically garbage-collected.
+ */
 export class RequestContextCacheService {
     private caches = new WeakMap<RequestContext, Map<any, any>>();
 
-    async set(ctx: RequestContext, key: any, val: any): Promise<void> {
+    set<T = any>(ctx: RequestContext, key: any, val: T): void {
         this.getContextCache(ctx).set(key, val);
     }
 
-    async get(ctx: RequestContext, key: any, getDefault?: () => Promise<any>): Promise<any> {
+    get<T = any>(ctx: RequestContext, key: any): T | undefined;
+    get<T = any>(ctx: RequestContext, key: any, getDefault?: () => Promise<T>): Promise<T>;
+    get<T = any>(ctx: RequestContext, key: any, getDefault?: () => Promise<T>): Promise<T> | T | undefined {
         const ctxCache = this.getContextCache(ctx);
-        let result = ctxCache.get(key);
-        if (!result && getDefault) {
-            result = await getDefault();
-            ctxCache.set(key, result);
+        const result = ctxCache.get(key);
+        if (result) {
+            return result;
+        }
+        if (getDefault) {
+            return getDefault().then(defaultResult => {
+                ctxCache.set(key, defaultResult);
+                return defaultResult;
+            });
+        } else {
+            return;
         }
-        return result;
     }
 
     private getContextCache(ctx: RequestContext): Map<any, any> {

+ 1 - 1
packages/core/src/service/services/tax-rate.service.ts

@@ -132,7 +132,7 @@ export class TaxRateService {
     }
 
     async updateActiveTaxRates(ctx: RequestContext) {
-        await this.cacheService.set(ctx, activeTaxRatesKey, await this.findActiveTaxRates(ctx));
+        this.cacheService.set(ctx, activeTaxRatesKey, await this.findActiveTaxRates(ctx));
     }
 
     private async findActiveTaxRates(ctx: RequestContext): Promise<TaxRate[]> {