浏览代码

docs(core): Add teardown logic to example Redis session cache

Michael Bromley 2 年之前
父节点
当前提交
828580193d

+ 5 - 1
packages/core/src/config/session-cache/session-cache-strategy.ts

@@ -79,12 +79,16 @@ export type CachedSession = {
  *     this.client.on('error', err => Logger.error(err.message, loggerCtx, err.stack));
  *   }
  *
+ *   async destroy() {
+ *     await this.client.quit();
+ *   }
+ *
  *   async get(sessionToken: string): Promise<CachedSession | undefined> {
  *     const retrieved = await this.client.get(this.namespace(sessionToken));
  *     if (retrieved) {
  *       try {
  *         return JSON.parse(retrieved);
- *       } catch (e) {
+ *       } catch (e: any) {
  *         Logger.error(`Could not parse cached session data: ${e.message}`, loggerCtx);
  *       }
  *     }

+ 25 - 8
packages/dev-server/test-plugins/redis-session-cache-plugin.ts

@@ -14,23 +14,39 @@ export class RedisSessionCacheStrategy implements SessionCacheStrategy {
         this.client.on('error', err => Logger.error(err.message, loggerCtx, err.stack));
     }
 
+    async destroy() {
+        await this.client.quit();
+    }
+
     async get(sessionToken: string): Promise<CachedSession | undefined> {
-        const retrieved = await this.client.get(this.namespace(sessionToken));
-        if (retrieved) {
-            try {
-                return JSON.parse(retrieved);
-            } catch (e) {
-                Logger.error(`Could not parse cached session data: ${e.message}`, loggerCtx);
+        try {
+            const retrieved = await this.client.get(this.namespace(sessionToken));
+            if (retrieved) {
+                try {
+                    return JSON.parse(retrieved);
+                } catch (e: any) {
+                    Logger.error(`Could not parse cached session data: ${e.message}`, loggerCtx);
+                }
             }
+        } catch (e: any) {
+            Logger.error(`Could not get cached session: ${e.message}`, loggerCtx);
         }
     }
 
     async set(session: CachedSession) {
-        await this.client.set(this.namespace(session.token), JSON.stringify(session));
+        try {
+            await this.client.set(this.namespace(session.token), JSON.stringify(session));
+        } catch (e: any) {
+            Logger.error(`Could not set cached session: ${e.message}`, loggerCtx);
+        }
     }
 
     async delete(sessionToken: string) {
-        await this.client.del(this.namespace(sessionToken));
+        try {
+            await this.client.del(this.namespace(sessionToken));
+        } catch (e: any) {
+            Logger.error(`Could not delete cached session: ${e.message}`, loggerCtx);
+        }
     }
 
     clear() {
@@ -57,6 +73,7 @@ export interface RedisSessionCachePluginOptions {
 })
 export class RedisSessionCachePlugin {
     static options: RedisSessionCachePluginOptions;
+
     static init(options: RedisSessionCachePluginOptions) {
         this.options = options;
         return this;