Skip to content

Commit f869ab1

Browse files
committed
修改问题
1 parent c5cf3ba commit f869ab1

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/components/MysqlSafeTransaction.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export class MysqlSafeTransaction {
1414
async safeTransaction<T>(handler: (trx: CoaMysql.Transaction) => Promise<T>): Promise<T> {
1515
let clearCacheNsps: any[] = []
1616
const result = await this.bin.io.transaction(async (trx: any) => {
17+
trx.__isSafeTransaction = true
18+
trx.clearCacheNsps = []
19+
1720
const result = await handler(trx)
21+
1822
clearCacheNsps = trx.clearCacheNsps || []
1923
return result
2024
})

src/services/MysqlCache.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
7070
}
7171

7272
async getIdBy(field: string, value: string | number, trx?: CoaMysql.Transaction) {
73-
return trx ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(this.getCacheNsp('index', field), '' + value, async () => await super.getIdBy(field, value, trx))
73+
return (trx as any).__isSafeTransaction ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(this.getCacheNsp('index', field), '' + value, async () => await super.getIdBy(field, value, trx))
7474
}
7575

7676
async mGetByIds(ids: string[], pick = this.pick, trx?: CoaMysql.Transaction, ms = this.ms, force = false) {
77-
const result = trx ? 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)
77+
const result = (trx as any).__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)
7878
_.forEach(result, (v, k) => {
7979
result[k] = this.pickResult(v, pick)
8080
})
@@ -88,23 +88,23 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
8888

8989
protected async findListCount(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
9090
const cacheId = 'list-count:' + secure.sha1($.sortQueryString(...finger))
91-
return trx ? await super.selectListCount(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectListCount(query, trx))
91+
return (trx as any)?.__isSafeTransaction ? await super.selectListCount(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectListCount(query, trx))
9292
}
9393

9494
protected async findIdList(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
9595
const cacheId = 'list:' + secure.sha1($.sortQueryString(...finger))
96-
return trx ? await super.selectIdList(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdList(query, trx))
96+
return (trx as any)?.__isSafeTransaction ? await super.selectIdList(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdList(query, trx))
9797
}
9898

9999
protected async findIdSortList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
100100
const cacheId = `sort-list:${pager.rows}:${pager.last}:` + secure.sha1($.sortQueryString(...finger))
101-
return trx ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdSortList(pager, query, trx))
101+
return (trx as any)?.__isSafeTransaction ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdSortList(pager, query, trx))
102102
}
103103

104104
protected async findIdViewList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
105105
const cacheId = `view-list:${pager.rows}:${pager.page}:` + secure.sha1($.sortQueryString(...finger))
106106
const count = await this.findListCount(finger, query, trx)
107-
return trx ? await super.selectIdViewList(pager, query, trx, count) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdViewList(pager, query, trx, count))
107+
return (trx as any)?.__isSafeTransaction ? await super.selectIdViewList(pager, query, trx, count) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdViewList(pager, query, trx, count))
108108
}
109109

110110
protected async mGetCountBy(field: string, ids: string[], trx?: CoaMysql.Transaction) {
@@ -114,7 +114,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
114114
_.forEach(rows, ({ id, count }) => (result[id] = count))
115115
return result
116116
}
117-
const result = trx ? await queryFunction() : await this.redisCache.mWarp(this.getCacheNsp('count', field), ids, queryFunction)
117+
const result = (trx as any)?.__isSafeTransaction ? await queryFunction() : await this.redisCache.mWarp(this.getCacheNsp('count', field), ids, queryFunction)
118118
return result
119119
}
120120

@@ -125,7 +125,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
125125
const rows = await qb
126126
return (rows[0]?.count as number) || 0
127127
}
128-
const result = trx ? await queryFunction() : await this.redisCache.warp(this.getCacheNsp('count', field), value, queryFunction)
128+
const result = (trx as any)?.__isSafeTransaction ? await queryFunction() : await this.redisCache.warp(this.getCacheNsp('count', field), value, queryFunction)
129129
return result
130130
}
131131

@@ -153,12 +153,9 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
153153
}
154154

155155
async deleteCache(ids: string[], dataList: Array<CoaMysql.SafePartial<Scheme>>, trx?: CoaMysql.Transaction) {
156-
if (trx && !(trx as any).clearCacheNsps) {
157-
(trx as any).clearCacheNsps = [] as CoaRedis.CacheDelete[]
158-
}
159-
if (trx && (trx as any).clearCacheNsps) {
160-
(trx as any).clearCacheNsps.push([this.getCacheNsp('id'), ids]);
161-
(trx as any).clearCacheNsps.push([this.getCacheNsp('data'), []])
156+
if ((trx as any)?.__isSafeTransaction) {
157+
(trx as any)?.clearCacheNsps.push([this.getCacheNsp('id'), ids]);
158+
(trx as any)?.clearCacheNsps.push([this.getCacheNsp('data'), []])
162159
}
163160
const deleteIds = [] as CoaRedis.CacheDelete[]
164161
deleteIds.push([this.getCacheNsp('id'), ids])
@@ -173,9 +170,11 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
173170
data?.[key] && ids.push(data[key])
174171
})
175172
ids.push(...keys.slice(1))
176-
trx ? (ids.length && (trx as any).clearCacheNsps.push([this.getCacheNsp(name, key), ids])) : (ids.length && deleteIds.push([this.getCacheNsp(name, key), ids]))
173+
if (ids.length) {
174+
((trx as any)?.__isSafeTransaction) ? (trx as any)?.clearCacheNsps.push([this.getCacheNsp(name, key), ids]) : deleteIds.push([this.getCacheNsp(name, key), ids])
175+
}
177176
})
178177
})
179-
if (!trx) await this.redisCache.mDelete(deleteIds)
178+
if ((!trx as any).__isSafeTransaction) await this.redisCache.mDelete(deleteIds)
180179
}
181180
}

0 commit comments

Comments
 (0)