Skip to content

Commit b013f80

Browse files
committed
暂存
1 parent 87f5274 commit b013f80

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

src/services/MysqlCache.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,16 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
6868
}
6969

7070
async getById(id: string, pick = this.columns, trx?: CoaMysql.Transaction, ms = this.ms, force = false) {
71-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'id') : this.getCacheNsp('id')
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)
71+
const result = trx ? 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)
7372
return this.pickResult(result, pick)
7473
}
7574

7675
async getIdBy(field: string, value: string | number, trx?: CoaMysql.Transaction) {
77-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'index', field) : this.getCacheNsp('index', field)
78-
return trx ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(cacheNsp, '' + value, async () => await super.getIdBy(field, value, trx))
76+
return trx ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(this.getCacheNsp('index', field), '' + value, async () => await super.getIdBy(field, value, trx))
7977
}
8078

8179
async mGetByIds(ids: string[], pick = this.pick, trx?: CoaMysql.Transaction, ms = this.ms, force = false) {
82-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'id') : this.getCacheNsp('id')
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)
80+
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)
8481
_.forEach(result, (v, k) => {
8582
result[k] = this.pickResult(v, pick)
8683
})
@@ -93,28 +90,24 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
9390
}
9491

9592
protected async findListCount(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
96-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'data') : this.getCacheNsp('data')
9793
const cacheId = 'list-count:' + secure.sha1($.sortQueryString(...finger))
98-
return trx ? await super.selectListCount(query, trx) : await this.redisCache.warp(cacheNsp, cacheId, async () => await super.selectListCount(query, trx))
94+
return trx ? await super.selectListCount(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectListCount(query, trx))
9995
}
10096

10197
protected async findIdList(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
102-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'data') : this.getCacheNsp('data')
10398
const cacheId = 'list:' + secure.sha1($.sortQueryString(...finger))
104-
return trx ? await super.selectIdList(query, trx) : await this.redisCache.warp(cacheNsp, cacheId, async () => await super.selectIdList(query, trx))
99+
return trx ? await super.selectIdList(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdList(query, trx))
105100
}
106101

107102
protected async findIdSortList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
108-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'data') : this.getCacheNsp('data')
109103
const cacheId = `sort-list:${pager.rows}:${pager.last}:` + secure.sha1($.sortQueryString(...finger))
110-
return trx ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(cacheNsp, cacheId, async () => await super.selectIdSortList(pager, query, trx))
104+
return trx ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdSortList(pager, query, trx))
111105
}
112106

113107
protected async findIdViewList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
114-
const cacheNsp = trx ? this.getTransactionCacheNsp(trx, 'data') : this.getCacheNsp('data')
115108
const cacheId = `view-list:${pager.rows}:${pager.page}:` + secure.sha1($.sortQueryString(...finger))
116109
const count = await this.findListCount(finger, query, trx)
117-
return trx ? await super.selectIdViewList(pager, query, trx, count) : await this.redisCache.warp(cacheNsp, cacheId, async () => await super.selectIdViewList(pager, query, trx, count))
110+
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))
118111
}
119112

120113
protected async mGetCountBy(field: string, ids: string[], trx?: CoaMysql.Transaction) {
@@ -128,8 +121,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
128121
if (trx) {
129122
return await queryFunction()
130123
} else {
131-
const cacheNsp = this.getCacheNsp('count', field)
132-
return await this.redisCache.mWarp(cacheNsp, ids, queryFunction)
124+
return await this.redisCache.mWarp(this.getCacheNsp('count', field), ids, queryFunction)
133125
}
134126
}
135127

@@ -146,8 +138,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
146138
return await queryFunction()
147139
} else {
148140
// 非事务走缓存
149-
const cacheNsp = this.getCacheNsp('count', field)
150-
return await this.redisCache.warp(cacheNsp, value, queryFunction)
141+
return await this.redisCache.warp(this.getCacheNsp('count', field), value, queryFunction)
151142
}
152143
}
153144

@@ -160,12 +151,6 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
160151
return this.system + ':' + this.name + ':' + nsp.join(':')
161152
}
162153

163-
// 为每个事务维护独立的缓存空间
164-
protected getTransactionCacheNsp(trx?: CoaMysql.Transaction, ...nsp: string[]) {
165-
const base = this.system + ':' + this.name + ':' + nsp.join(':')
166-
return trx && (trx as any).id ? `${base}:trx:${(trx as any).id}` : base
167-
}
168-
169154
protected async getCacheChangedDataList(ids: string[], data?: CoaMysql.SafePartial<Scheme>, trx?: CoaMysql.Transaction) {
170155
let has = true
171156
const resultList = [] as Array<CoaMysql.SafePartial<Scheme>>

0 commit comments

Comments
 (0)