@@ -5,7 +5,6 @@ import { secure } from 'coa-secure'
55import { MysqlBin } from '../libs/MysqlBin'
66import { CoaMysql } from '../typings'
77import { MysqlNative } from './MysqlNative'
8-
98export class MysqlCache < Scheme > extends MysqlNative < Scheme > {
109 redisCache : RedisCache
1110
@@ -15,49 +14,49 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
1514 }
1615
1716 async insert ( data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
18- const id = await super . insert ( data , trx )
19- await this . deleteCache ( [ id ] , [ data ] )
17+ const id = await super . insert ( data , trx ) ;
18+ await this . deleteCache ( [ id ] , [ data ] , trx )
2019 return id
2120 }
2221
2322 async mInsert ( dataList : Array < CoaMysql . SafePartial < Scheme > > , trx ?: CoaMysql . Transaction ) {
24- const ids = await super . mInsert ( dataList , trx )
25- await this . deleteCache ( ids , dataList )
23+ const ids = await super . mInsert ( dataList , trx ) ;
24+ await this . deleteCache ( ids , dataList , trx )
2625 return ids
2726 }
2827
2928 async updateById ( id : string , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
3029 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
3130 const result = await super . updateById ( id , data , trx )
32- if ( result ) await this . deleteCache ( [ id ] , dataList )
31+ if ( result ) await this . deleteCache ( [ id ] , dataList , trx )
3332 return result
3433 }
3534
3635 async updateByIds ( ids : string [ ] , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
3736 const dataList = await this . getCacheChangedDataList ( ids , data , trx )
38- const result = await super . updateByIds ( ids , data , trx )
39- if ( result ) await this . deleteCache ( ids , dataList )
37+ const result = await super . updateByIds ( ids , data , trx ) ;
38+ if ( result ) await this . deleteCache ( ids , dataList , trx )
4039 return result
4140 }
4241
4342 async updateForQueryById ( id : string , query : CoaMysql . Query , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
4443 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
45- const result = await super . updateForQueryById ( id , query , data , trx )
46- if ( result ) await this . deleteCache ( [ id ] , dataList )
44+ const result = await super . updateForQueryById ( id , query , data , trx ) ;
45+ if ( result ) await this . deleteCache ( [ id ] , dataList , trx )
4746 return result
4847 }
4948
5049 async upsertById ( id : string , data : CoaMysql . SafePartial < Scheme > , trx ?: CoaMysql . Transaction ) {
5150 const dataList = await this . getCacheChangedDataList ( [ id ] , data , trx )
52- const result = await super . upsertById ( id , data , trx )
53- await this . deleteCache ( [ id ] , dataList )
51+ const result = await super . upsertById ( id , data , trx ) ;
52+ await this . deleteCache ( [ id ] , dataList , trx )
5453 return result
5554 }
5655
5756 async deleteByIds ( ids : string [ ] , trx ?: CoaMysql . Transaction ) {
5857 const dataList = await this . getCacheChangedDataList ( ids , undefined , trx )
59- const result = await super . deleteByIds ( ids , trx )
60- if ( result ) await this . deleteCache ( ids , dataList )
58+ const result = await super . deleteByIds ( ids , trx ) ;
59+ await this . deleteCache ( ids , dataList , trx )
6160 return result
6261 }
6362
@@ -66,16 +65,16 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
6665 }
6766
6867 async getById ( id : string , pick = this . columns , trx ?: CoaMysql . Transaction , ms = this . ms , force = false ) {
69- const result = await this . redisCache . warp ( this . getCacheNsp ( 'id' ) , id , async ( ) => await super . getById ( id , this . columns , trx ) , ms , force )
68+ const result = trx ?. __isSafeTransaction ? await super . getById ( id , this . columns , trx ) : await this . redisCache . warp ( this . getCacheNsp ( 'id' ) , id , async ( ) => await super . getById ( id , this . columns , trx ) , ms , force )
7069 return this . pickResult ( result , pick )
7170 }
7271
7372 async getIdBy ( field : string , value : string | number , trx ?: CoaMysql . Transaction ) {
74- return await this . redisCache . warp ( this . getCacheNsp ( 'index' , field ) , '' + value , async ( ) => await super . getIdBy ( field , value , trx ) )
73+ return trx ?. __isSafeTransaction ? await super . getIdBy ( field , value , trx ) : await this . redisCache . warp ( this . getCacheNsp ( 'index' , field ) , '' + value , async ( ) => await super . getIdBy ( field , value , trx ) )
7574 }
7675
7776 async mGetByIds ( ids : string [ ] , pick = this . pick , trx ?: CoaMysql . Transaction , ms = this . ms , force = false ) {
78- const result = await this . redisCache . mWarp ( this . getCacheNsp ( 'id' ) , ids , async ids => await super . mGetByIds ( ids , this . columns , trx ) , ms , force )
77+ const result = trx ?. __isSafeTransaction ? await super . mGetByIds ( ids , this . columns , trx ) : await this . redisCache . mWarp ( this . getCacheNsp ( 'id' ) , ids , async ids => await super . mGetByIds ( ids , this . columns , trx ) , ms , force )
7978 _ . forEach ( result , ( v , k ) => {
8079 result [ k ] = this . pickResult ( v , pick )
8180 } )
@@ -88,48 +87,46 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
8887 }
8988
9089 protected async findListCount ( finger : Array < CoaMysql . Dic < any > > , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
91- const cacheNsp = this . getCacheNsp ( 'data' )
9290 const cacheId = 'list-count:' + secure . sha1 ( $ . sortQueryString ( ...finger ) )
93- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectListCount ( query , trx ) )
91+ return trx ?. __isSafeTransaction ? await super . selectListCount ( query , trx ) : await this . redisCache . warp ( this . getCacheNsp ( 'data' ) , cacheId , async ( ) => await super . selectListCount ( query , trx ) )
9492 }
9593
9694 protected async findIdList ( finger : Array < CoaMysql . Dic < any > > , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
97- const cacheNsp = this . getCacheNsp ( 'data' )
9895 const cacheId = 'list:' + secure . sha1 ( $ . sortQueryString ( ...finger ) )
99- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdList ( query , trx ) )
96+ return trx ?. __isSafeTransaction ? await super . selectIdList ( query , trx ) : await this . redisCache . warp ( this . getCacheNsp ( 'data' ) , cacheId , async ( ) => await super . selectIdList ( query , trx ) )
10097 }
10198
10299 protected async findIdSortList ( finger : Array < CoaMysql . Dic < any > > , pager : CoaMysql . Pager , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
103- const cacheNsp = this . getCacheNsp ( 'data' )
104100 const cacheId = `sort-list:${ pager . rows } :${ pager . last } :` + secure . sha1 ( $ . sortQueryString ( ...finger ) )
105- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdSortList ( pager , query , trx ) )
101+ return trx ?. __isSafeTransaction ? await super . selectIdSortList ( pager , query , trx ) : await this . redisCache . warp ( this . getCacheNsp ( 'data' ) , cacheId , async ( ) => await super . selectIdSortList ( pager , query , trx ) )
106102 }
107103
108104 protected async findIdViewList ( finger : Array < CoaMysql . Dic < any > > , pager : CoaMysql . Pager , query : CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
109- const cacheNsp = this . getCacheNsp ( 'data' )
110105 const cacheId = `view-list:${ pager . rows } :${ pager . page } :` + secure . sha1 ( $ . sortQueryString ( ...finger ) )
111106 const count = await this . findListCount ( finger , query , trx )
112- return await this . redisCache . warp ( cacheNsp , cacheId , async ( ) => await super . selectIdViewList ( pager , query , trx , count ) )
107+ return trx ?. __isSafeTransaction ? await super . selectIdViewList ( pager , query , trx , count ) : await this . redisCache . warp ( this . getCacheNsp ( 'data' ) , cacheId , async ( ) => await super . selectIdViewList ( pager , query , trx , count ) )
113108 }
114109
115110 protected async mGetCountBy ( field : string , ids : string [ ] , trx ?: CoaMysql . Transaction ) {
116- const cacheNsp = this . getCacheNsp ( 'count' , field )
117- return await this . redisCache . mWarp ( cacheNsp , ids , async ids => {
111+ const queryFunction = async ( ) => {
118112 const rows = ( await this . table ( trx ) . select ( { id : field } ) . count ( { count : this . key } ) . whereIn ( field , ids ) . groupBy ( field ) ) as any [ ]
119113 const result : CoaMysql . Dic < number > = { }
120114 _ . forEach ( rows , ( { id, count } ) => ( result [ id ] = count ) )
121115 return result
122- } )
116+ }
117+ const result = trx ?. __isSafeTransaction ? await queryFunction ( ) : await this . redisCache . mWarp ( this . getCacheNsp ( 'count' , field ) , ids , queryFunction )
118+ return result
123119 }
124120
125121 protected async getCountBy ( field : string , value : string , query ?: CoaMysql . Query , trx ?: CoaMysql . Transaction ) {
126- const cacheNsp = this . getCacheNsp ( 'count' , field )
127- return await this . redisCache . warp ( cacheNsp , value , async ( ) => {
122+ const queryFunction = async ( ) => {
128123 const qb = this . table ( trx ) . count ( { count : this . key } )
129124 query ? query ( qb ) : qb . where ( field , value )
130125 const rows = await qb
131126 return ( rows [ 0 ] ?. count as number ) || 0
132- } )
127+ }
128+ const result = trx ?. __isSafeTransaction ? await queryFunction ( ) : await this . redisCache . warp ( this . getCacheNsp ( 'count' , field ) , value , queryFunction )
129+ return result
133130 }
134131
135132 protected pickResult < T > ( data : T , pick : string [ ] ) {
@@ -155,10 +152,15 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
155152 return resultList
156153 }
157154
158- protected async deleteCache ( ids : string [ ] , dataList : Array < CoaMysql . SafePartial < Scheme > > ) {
155+ async deleteCache ( ids : string [ ] , dataList : Array < CoaMysql . SafePartial < Scheme > > , trx ?: CoaMysql . Transaction ) {
159156 const deleteIds = [ ] as CoaRedis . CacheDelete [ ]
160- deleteIds . push ( [ this . getCacheNsp ( 'id' ) , ids ] )
161- deleteIds . push ( [ this . getCacheNsp ( 'data' ) , [ ] ] )
157+ if ( trx ?. __isSafeTransaction ) {
158+ ( trx as any ) ?. clearCacheNsps . push ( [ this . getCacheNsp ( 'id' ) , ids ] ) ;
159+ ( trx as any ) ?. clearCacheNsps . push ( [ this . getCacheNsp ( 'data' ) , [ ] ] )
160+ } else {
161+ deleteIds . push ( [ this . getCacheNsp ( 'id' ) , ids ] )
162+ deleteIds . push ( [ this . getCacheNsp ( 'data' ) , [ ] ] )
163+ }
162164 _ . forEach ( this . caches , ( items , name ) => {
163165 // name可能为index,count,或自定义
164166 items . forEach ( item => {
@@ -169,9 +171,11 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
169171 data ?. [ key ] && ids . push ( data [ key ] )
170172 } )
171173 ids . push ( ...keys . slice ( 1 ) )
172- ids . length && deleteIds . push ( [ this . getCacheNsp ( name , key ) , ids ] )
174+ if ( ids . length ) {
175+ ( trx ?. __isSafeTransaction ) ? ( trx as any ) ?. clearCacheNsps . push ( [ this . getCacheNsp ( name , key ) , ids ] ) : deleteIds . push ( [ this . getCacheNsp ( name , key ) , ids ] )
176+ }
173177 } )
174178 } )
175- await this . redisCache . mDelete ( deleteIds )
179+ if ( ! trx ?. __isSafeTransaction ) await this . redisCache . mDelete ( deleteIds )
176180 }
177181}
0 commit comments