-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add support safe transaction #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ityifan
wants to merge
26
commits into
main
Choose a base branch
from
fix-transaction-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
23c1d76
修改删除缓存滞后执行
ityifan e09b928
格式化代码
ityifan fb6773d
删除事务id缓存
ityifan 87f5274
删除事务走缓存方法
ityifan b013f80
暂存
ityifan db98282
格式化代码
ityifan 028c30a
删除输出
ityifan 5058a4a
格式化代码
ityifan d46c8ed
新增if判断
ityifan 8cc6e50
增加if
ityifan 73b5f4f
移除trx.id
ityifan 2f47139
回退demo
ityifan d98bb4d
修改安全事务的方法位置
ityifan 4020990
恢复demo
ityifan be54b48
删除注释
ityifan a44846e
修改
ityifan 6200b73
定义类型
ityifan 7259845
修改demo
ityifan 58952fc
新增if判断
ityifan c5cf3ba
删除多余判断
ityifan f869ab1
修改问题
ityifan 069a2dd
解决问题
ityifan e2a00fc
修改冗余push
ityifan 8b8d6df
demo文件新增示例化参数传入
ityifan 44367ac
解决类型声明问题
ityifan 4b6043b
补充getById方法__isSafeTransaction判断
ityifan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { RedisCache } from "coa-redis" | ||
import { MysqlBin } from "../libs/MysqlBin" | ||
import { CoaMysql } from "../typings" | ||
|
||
export class MysqlSafeTransaction { | ||
private readonly bin: MysqlBin | ||
private readonly cache: RedisCache | ||
|
||
constructor(bin: MysqlBin, cache: RedisCache) { | ||
this.bin = bin | ||
this.cache = cache | ||
} | ||
|
||
async safeTransaction<T>(handler: (trx: CoaMysql.Transaction) => Promise<T>): Promise<T> { | ||
let clearCacheNsps: any[] = [] | ||
const result = await this.bin.io.transaction(async (trx: CoaMysql.Transaction) => { | ||
trx.__isSafeTransaction = true | ||
trx.clearCacheNsps = [] | ||
|
||
const result = await handler(trx) | ||
|
||
clearCacheNsps = trx.clearCacheNsps || [] | ||
return result | ||
}) | ||
|
||
if (clearCacheNsps.length > 0) { await this.cache.mDelete(clearCacheNsps) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 考虑下这句话能不能放到上面18行的位置,是否有必要在事务外面执行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 放到事务内 应该还是有概率导致缓存内存在脏数据的 事务外比较保险一点 |
||
return result | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export { CoaMysql } from './typings' | ||
export { MysqlSafeTransaction } from './components/MysqlSafeTransaction' | ||
export { MysqlStorage } from './components/MysqlStorage' | ||
export { MysqlUuid } from './components/MysqlUuid' | ||
export { MysqlBin } from './libs/MysqlBin' | ||
export { MysqlNative } from './services/MysqlNative' | ||
export { MysqlCache } from './services/MysqlCache' | ||
export { MysqlUuid } from './components/MysqlUuid' | ||
export { MysqlStorage } from './components/MysqlStorage' | ||
export { MysqlNative } from './services/MysqlNative' | ||
export { CoaMysql } from './typings' | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* eslint-disable @typescript-eslint/no-redeclare */ | ||
// @ts-nocheck | ||
import { $, _ } from 'coa-helper' | ||
import { RedisBin, RedisCache } from 'coa-redis' | ||
import { MysqlBin, MysqlCache, MysqlSafeTransaction } from '..' | ||
// MySQL配置 | ||
const mysqlConfig = { | ||
host: '127.0.0.1', | ||
port: 3306, | ||
user: 'root', | ||
password: '', | ||
charset: 'utf8mb4', | ||
trace: true, | ||
debug: false, | ||
databases: { | ||
main: { database: 'coa-mysql', ms: 7 * 24 * 3600 * 1000 }, | ||
}, | ||
} | ||
|
||
const redisConfig = { | ||
host: '127.0.0.1', | ||
port: 6379, | ||
password: '', | ||
db: 1, | ||
prefix: 'coa-mysql', | ||
trace: false, | ||
|
||
} | ||
const redisBin = new RedisBin(redisConfig) | ||
const redisCache = new RedisCache(redisBin) | ||
|
||
// 初始化Mysql基本连接,后续所有模型均依赖此实例 | ||
const mysqlBin = new MysqlBin(mysqlConfig) | ||
const safeTransaction = new MysqlSafeTransaction(mysqlBin, redisCache) | ||
|
||
const userScheme = { | ||
userId: '' as string, | ||
name: '' as string, | ||
mobile: '' as string, | ||
avatar: '' as string, | ||
gender: 1 as number, | ||
language: '' as string, | ||
status: 1 as number, | ||
created: 0 as number, | ||
updated: 0 as number, | ||
} | ||
|
||
|
||
// 定义User类型(通过默认结构自动生成) | ||
type UserScheme = typeof userScheme | ||
|
||
|
||
// 通过基类初始化 | ||
const User = new (class extends MysqlCache<UserScheme> { | ||
constructor() { | ||
super( | ||
{ | ||
name: 'User', // 表名,默认会转化为下划线(snackCase)形式,如 User->user UserPhoto->user_photo | ||
title: '用户表', // 表的备注名称 | ||
scheme: userScheme, // 表的默认结构 | ||
pick: ['userId', 'name'], // 查询列表时显示的字段信息 | ||
caches: { index: ['name'], count: ['userId', 'name'] }, | ||
|
||
}, | ||
mysqlBin, | ||
redisCache, | ||
) | ||
} | ||
})() | ||
|
||
const a = async () => { | ||
await User.checkById('411******728253X') | ||
await $.timeout(3000) | ||
await safeTransaction.safeTransaction(async (trx: CoaMysql.Transaction) => { | ||
await User.updateById('411******728253X', { name: 'mmm' }) | ||
for (let index = 0; index < 10; index++) { | ||
const userId = `${_.now()}-${index}-Y` | ||
await User.insert({ userId, name: 'heyifan2' }, trx) | ||
} | ||
}) | ||
await $.timeout(3000) | ||
const b = await User.checkById('411******728253X') | ||
console.log(b); | ||
} | ||
a() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.