@@ -5,9 +5,20 @@ import { CubeStoreDriver } from './CubeStoreDriver';
55
66export class CubeStoreCacheDriver implements CacheDriverInterface {
77 public constructor (
8- protected readonly connection : CubeStoreDriver
8+ protected connectionFactory : ( ) => Promise < CubeStoreDriver > ,
99 ) { }
1010
11+ protected connection : CubeStoreDriver | null = null ;
12+
13+ protected async getConnection ( ) : Promise < CubeStoreDriver > {
14+ if ( this . connection ) {
15+ return this . connection ;
16+ }
17+
18+ // eslint-disable-next-line no-return-assign
19+ return this . connection = await this . connectionFactory ( ) ;
20+ }
21+
1122 public withLock = (
1223 key : string ,
1324 cb : ( ) => MaybeCancelablePromise < any > ,
@@ -18,11 +29,13 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
1829 return false ;
1930 }
2031
21- const rows = await this . connection . query ( 'CACHE SET NX TTL ? ? ?' , [ expiration , key , '1' ] ) ;
32+ const connection = ( await this . getConnection ( ) ) ;
33+
34+ const rows = await connection . query ( 'CACHE SET NX TTL ? ? ?' , [ expiration , key , '1' ] ) ;
2235 if ( rows && rows . length === 1 && rows [ 0 ] ?. success === 'true' ) {
2336 if ( tkn . isCanceled ( ) ) {
2437 if ( freeAfter ) {
25- await this . connection . query ( 'CACHE REMOVE ?' , [
38+ await connection . query ( 'CACHE REMOVE ?' , [
2639 key
2740 ] ) ;
2841 }
@@ -34,7 +47,7 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
3447 await tkn . with ( cb ( ) ) ;
3548 } finally {
3649 if ( freeAfter ) {
37- await this . connection . query ( 'CACHE REMOVE ?' , [
50+ await connection . query ( 'CACHE REMOVE ?' , [
3851 key
3952 ] ) ;
4053 }
@@ -47,7 +60,7 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
4760 } ) ;
4861
4962 public async get ( key : string ) {
50- const rows = await this . connection . query ( 'CACHE GET ?' , [
63+ const rows = await ( await this . getConnection ( ) ) . query ( 'CACHE GET ?' , [
5164 key
5265 ] ) ;
5366 if ( rows && rows . length === 1 ) {
@@ -59,7 +72,7 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
5972
6073 public async set ( key : string , value , expiration ) {
6174 const strValue = JSON . stringify ( value ) ;
62- await this . connection . query ( 'CACHE SET TTL ? ? ?' , [ expiration , key , strValue ] ) ;
75+ await ( await this . getConnection ( ) ) . query ( 'CACHE SET TTL ? ? ?' , [ expiration , key , strValue ] ) ;
6376
6477 return {
6578 key,
@@ -68,13 +81,13 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
6881 }
6982
7083 public async remove ( key : string ) {
71- await this . connection . query ( 'CACHE REMOVE ?' , [
84+ await ( await this . getConnection ( ) ) . query ( 'CACHE REMOVE ?' , [
7285 key
7386 ] ) ;
7487 }
7588
7689 public async keysStartingWith ( prefix : string ) {
77- const rows = await this . connection . query ( 'CACHE KEYS ?' , [
90+ const rows = await ( await this . getConnection ( ) ) . query ( 'CACHE KEYS ?' , [
7891 prefix
7992 ] ) ;
8093 return rows . map ( ( row ) => row . key ) ;
@@ -85,6 +98,6 @@ export class CubeStoreCacheDriver implements CacheDriverInterface {
8598 }
8699
87100 public async testConnection ( ) : Promise < void > {
88- return this . connection . testConnection ( ) ;
101+ return ( await this . getConnection ( ) ) . testConnection ( ) ;
89102 }
90103}
0 commit comments