@@ -16,61 +16,49 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
1616
1717 async insert ( data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
1818 const id = await super . insert ( data , trx )
19- if ( id ) {
20- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , [ data ] ) : await this . deleteCache ( [ id ] , [ data ] )
21- }
19+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , [ data ] ) : await this . deleteCache ( [ id ] , [ data ] )
2220 return id
2321 }
2422
2523 async mInsert ( dataList : Array < CoaMysql . SafePartial < Scheme > > , trx ?: CoaMysql . Transaction ) {
2624 const ids = await super . mInsert ( dataList , trx )
27- if ( ids ) {
28- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
29- }
25+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
3026 return ids
3127 }
3228
3329 async updateById ( id : string , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
3430 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
3531 const result = await super . updateById ( id , data , trx )
36- if ( result ) {
37- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
38- }
32+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
3933 return result
4034 }
4135
4236 async updateByIds ( ids : string [ ] , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
4337 const dataList = await this . getCacheChangedDataList ( ids , data , trx )
4438 const result = await super . updateByIds ( ids , data , trx )
45- if ( result ) {
46- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
47- }
39+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
4840 return result
4941 }
5042
5143 async updateForQueryById ( id : string , query : CoaMysql . Query , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
5244 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
5345 const result = await super . updateForQueryById ( id , query , data , trx )
54- if ( result ) {
55- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
56- }
46+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
5747 return result
5848 }
5949
6050 async upsertById ( id : string , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
6151 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
6252 const result = await super . upsertById ( id , data , trx )
63- if ( result ) {
64- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
65- }
53+ trx && ( trx as any ) . trxUpdateCacheTaskList ? await ( trx as any ) . trxUpdateCacheTaskList ( this , [ id ] , dataList ) : await this . deleteCache ( [ id ] , dataList )
6654 return result
6755 }
6856
6957 async deleteByIds ( ids : string [ ] , trx ?: CoaMysql . Transaction ) {
7058 const dataList = await this . getCacheChangedDataList ( ids , undefined , trx )
7159 const result = await super . deleteByIds ( ids , trx )
7260 if ( result ) {
73- ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
61+ ( trx && ( trx as any ) . trxUpdateCacheTaskList ) ? await ( trx as any ) . trxUpdateCacheTaskList ( this , ids , dataList ) : await this . deleteCache ( ids , dataList )
7462 }
7563 return result
7664 }
@@ -81,21 +69,18 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
8169
8270 async getById ( id : string , pick = this . columns , trx ?: CoaMysql . Transaction , ms = this . ms , force = false ) {
8371 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'id' ) : this . getCacheNsp ( 'id' )
84- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
85- const result = await this . redisCache . warp ( cacheNsp , id , async ( ) => await super . getById ( id , this . columns , trx ) , ms , force )
72+ const result = trx ? await super . getById ( id , this . columns , trx ) : await this . redisCache . warp ( cacheNsp , id , async ( ) => await super . getById ( id , this . columns , trx ) , ms , force )
8673 return this . pickResult ( result , pick )
8774 }
8875
8976 async getIdBy ( field : string , value : string | number , trx ?: CoaMysql . Transaction ) {
9077 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'index' , field ) : this . getCacheNsp ( 'index' , field )
91- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
92- return await this . redisCache . warp ( cacheNsp , '' + value , async ( ) => await super . getIdBy ( field , value , trx ) )
78+ return trx ? await super . getIdBy ( field , value , trx ) : await this . redisCache . warp ( cacheNsp , '' + value , async ( ) => await super . getIdBy ( field , value , trx ) )
9379 }
9480
9581 async mGetByIds ( ids : string [ ] , pick = this . pick , trx ?: CoaMysql . Transaction , ms = this . ms , force = false ) {
9682 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'id' ) : this . getCacheNsp ( 'id' )
97- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
98- const result = await this . redisCache . mWarp ( cacheNsp , ids , async ids => await super . mGetByIds ( ids , this . columns , trx ) , ms , force )
83+ const result = trx ? await super . mGetByIds ( ids , this . columns , trx ) : await this . redisCache . mWarp ( cacheNsp , ids , async ids => await super . mGetByIds ( ids , this . columns , trx ) , ms , force )
9984 _ . forEach ( result , ( v , k ) => {
10085 result [ k ] = this . pickResult ( v , pick )
10186 } )
@@ -109,53 +94,61 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
10994
11095 protected async findListCount ( finger : Array < CoaMysql . Dic < any > > , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
11196 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'data' ) : this . getCacheNsp ( 'data' )
112- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
11397 const cacheId = 'list-count:' + secure . sha1 ( $ . sortQueryString ( ...finger ) )
114- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectListCount ( query , trx ) )
98+ return trx ? await super . selectListCount ( query , trx ) : await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectListCount ( query , trx ) )
11599 }
116100
117101 protected async findIdList ( finger : Array < CoaMysql . Dic < any > > , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
118102 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'data' ) : this . getCacheNsp ( 'data' )
119- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
120103 const cacheId = 'list:' + secure . sha1 ( $ . sortQueryString ( ...finger ) )
121- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdList ( query , trx ) )
104+ return trx ? await super . selectIdList ( query , trx ) : await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdList ( query , trx ) )
122105 }
123106
124107 protected async findIdSortList ( finger : Array < CoaMysql . Dic < any > > , pager : CoaMysql . Pager , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
125108 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'data' ) : this . getCacheNsp ( 'data' )
126- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
127109 const cacheId = `sort-list:${ pager . rows } :${ pager . last } :` + secure . sha1 ( $ . sortQueryString ( ...finger ) )
128- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdSortList ( pager , query , trx ) )
110+ return trx ? await super . selectIdSortList ( pager , query , trx ) : await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdSortList ( pager , query , trx ) )
129111 }
130112
131113 protected async findIdViewList ( finger : Array < CoaMysql . Dic < any > > , pager : CoaMysql . Pager , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
132114 const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'data' ) : this . getCacheNsp ( 'data' )
133- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
134115 const cacheId = `view-list:${ pager . rows } :${ pager . page } :` + secure . sha1 ( $ . sortQueryString ( ...finger ) )
135116 const count = await this . findListCount ( finger , query , trx )
136- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdViewList ( pager , query , trx , count ) )
117+ return trx ? await super . selectIdViewList ( pager , query , trx , count ) : await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdViewList ( pager , query , trx , count ) )
137118 }
138119
139120 protected async mGetCountBy ( field : string , ids : string [ ] , trx ?: CoaMysql . Transaction ) {
140- const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'count' , field ) : this . getCacheNsp ( 'count' , field )
141- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
142- return await this . redisCache . mWarp ( cacheNsp , ids , async ids => {
121+ const queryFunction = async ( ) => {
143122 const rows = ( await this . table ( trx ) . select ( { id : field } ) . count ( { count : this . key } ) . whereIn ( field , ids ) . groupBy ( field ) ) as any [ ]
144123 const result : CoaMysql . Dic < number > = { }
145124 _ . forEach ( rows , ( { id, count } ) => ( result [ id ] = count ) )
146125 return result
147- } )
126+ }
127+
128+ if ( trx ) {
129+ return await queryFunction ( )
130+ } else {
131+ const cacheNsp = this . getCacheNsp ( 'count' , field )
132+ return await this . redisCache . mWarp ( cacheNsp , ids , queryFunction )
133+ }
148134 }
149135
150136 protected async getCountBy ( field : string , value : string , query ?: CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
151- const cacheNsp = trx ? this . getTransactionCacheNsp ( trx , 'count' , field ) : this . getCacheNsp ( 'count' , field )
152- trx && ( trx as any ) . trxRaeadCacheNspsList ( this , cacheNsp )
153- return await this . redisCache . warp ( cacheNsp , value , async ( ) => {
137+ const queryFunction = async ( ) => {
154138 const qb = this . table ( trx ) . count ( { count : this . key } )
155139 query ? query ( qb ) : qb . where ( field , value )
156140 const rows = await qb
157141 return ( rows [ 0 ] ?. count as number ) || 0
158- } )
142+ }
143+
144+ if ( trx ) {
145+ // 事务内直接查询数据库,不走缓存
146+ return await queryFunction ( )
147+ } else {
148+ // 非事务走缓存
149+ const cacheNsp = this . getCacheNsp ( 'count' , field )
150+ return await this . redisCache . warp ( cacheNsp , value , queryFunction )
151+ }
159152 }
160153
161154 protected pickResult < T > ( data : T , pick : string [ ] ) {
0 commit comments