Skip to content

Commit 87f5274

Browse files
committed
删除事务走缓存方法
1 parent fb6773d commit 87f5274

File tree

3 files changed

+49
-61
lines changed

3 files changed

+49
-61
lines changed

src/libs/MysqlBin.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,23 @@ export class MysqlBin {
3434

3535
async safeTransaction<T>(handler: (trx: CoaMysql.Transaction) => Promise<T>): Promise<T> {
3636

37-
let cacheTasks = {
38-
trxUpdateCacheTaskList: [] as Array<{ model: MysqlCache<any>, ids: string[], dataList: any[] }>,
39-
trxRaeadCacheNspsList: [] as Array<{ model: MysqlCache<any>, nsp: string }>
40-
};
37+
let cacheTasks = [] as Array<{ model: MysqlCache<any>, ids: string[], dataList: any[] }>
4138

4239
const result = await this.io.transaction(async (trx: any) => {
4340
trx.id ||= secure.id25(`${Date.now()}-${Math.floor(Math.random() * 1e6).toString().padStart(6, '0')}`)
44-
trx.trxUpdateCacheTaskList = (model: MysqlCache<any>, ids: string[], dataList: any[]) => {
45-
cacheTasks.trxUpdateCacheTaskList.push({ model, ids, dataList })
46-
}
47-
trx.trxRaeadCacheNspsList = (model: MysqlCache<any>, nsp: string) => {
48-
cacheTasks.trxRaeadCacheNspsList.push({ model, nsp })
41+
// 收集事务更新缓存数据
42+
trx.trxUpdateCacheTaskList = async (model: MysqlCache<any>, ids: string[], dataList: any[]) => {
43+
cacheTasks.push({ model, ids, dataList })
4944
}
5045
return await handler(trx)
5146
})
52-
53-
for (const task of cacheTasks.trxUpdateCacheTaskList) {
47+
// 统一清理事务更新缓存
48+
for (const task of cacheTasks) {
5449
await task.model.deleteCache(task.ids, task.dataList)
5550
}
5651

57-
for (const readCacheNsp of cacheTasks.trxRaeadCacheNspsList) {
58-
await readCacheNsp.model.redisCache.clear(readCacheNsp.nsp)
59-
}
6052
// 初始数组 减少trx未及时销毁时的内存占用
61-
cacheTasks = { trxUpdateCacheTaskList: [], trxRaeadCacheNspsList: [] }
53+
cacheTasks = []
6254

6355
return result
6456
}

src/services/MysqlCache.ts

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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[]) {

src/test/demo.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-redeclare */
22
// @ts-nocheck
3-
import { $ } from 'coa-helper'
3+
import { $, _ } from 'coa-helper'
44
import { RedisBin, RedisCache } from 'coa-redis'
55
import { MysqlBin, MysqlCache } from '..'
66
// MySQL配置
@@ -69,6 +69,7 @@ const User = new (class extends MysqlCache<UserScheme> {
6969
title: '用户表', // 表的备注名称
7070
scheme: userScheme, // 表的默认结构
7171
pick: ['userId', 'name'], // 查询列表时显示的字段信息
72+
caches: { index: ['name'], count: ['userId', 'name'] }
7273
},
7374
mysqlBin,
7475
redisCache,
@@ -99,12 +100,14 @@ const User = new (class extends MysqlCache<UserScheme> {
99100

100101
// await User.updateById('id002', { name: '李四' }) // 返回 1
101102
const a = async () => {
103+
await User.checkById('41102319990728253X')
102104
await mysqlBin.safeTransaction(async (trx: CoaMysql.Transaction) => {
103105
await User.updateById('41102319990728253X', { name: 'mmm' }, trx)
104-
const q = await User.checkById('41102319990728253X', Object.keys(userScheme), trx)
105-
console.log(q);
106-
await $.timeout(3000)
107-
await User.updateById('1758003943672Y2', { name: 'heyifan2' }, trx)
106+
for (let index = 0; index < 10; index++) {
107+
const userId = `${_.now()}-${index}-Y`
108+
await $.timeout(3000)
109+
await User.insert({ userId, name: 'heyifan2' }, trx)
110+
}
108111
})
109112
const b = await User.checkById('41102319990728253X')
110113
console.log(b);

0 commit comments

Comments
 (0)