mm_mysql 2.2.1 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/index.js +7 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -213,10 +213,10 @@ const config = {
|
|
|
213
213
|
### Mysql管理器
|
|
214
214
|
|
|
215
215
|
#### mysqlAdmin(scope, config)
|
|
216
|
-
获取Mysql
|
|
216
|
+
获取Mysql管理器实例
|
|
217
217
|
- `scope` {String} 作用域名称(可选,默认为'sys')
|
|
218
218
|
- `config` {Object} 配置参数
|
|
219
|
-
- 返回:{
|
|
219
|
+
- 返回:{Mysql} Mysql实例
|
|
220
220
|
|
|
221
221
|
使用示例:
|
|
222
222
|
```javascript
|
|
@@ -224,7 +224,7 @@ const { mysqlAdmin } = require('mm_mysql');
|
|
|
224
224
|
|
|
225
225
|
async function example() {
|
|
226
226
|
// 获取默认作用域的Mysql实例
|
|
227
|
-
const mysql1 =
|
|
227
|
+
const mysql1 = mysqlAdmin('default', {
|
|
228
228
|
host: 'localhost',
|
|
229
229
|
user: 'root',
|
|
230
230
|
password: '123456',
|
|
@@ -232,7 +232,7 @@ async function example() {
|
|
|
232
232
|
});
|
|
233
233
|
|
|
234
234
|
// 获取另一个作用域的Mysql实例
|
|
235
|
-
const mysql2 =
|
|
235
|
+
const mysql2 = mysqlAdmin('backup', {
|
|
236
236
|
host: 'localhost',
|
|
237
237
|
user: 'root',
|
|
238
238
|
password: '123456',
|
package/index.js
CHANGED
|
@@ -602,9 +602,9 @@ function _acquireLock(key) {
|
|
|
602
602
|
* @description Mysql管理器,用于创建缓存(线程安全版)
|
|
603
603
|
* @param {String} scope 作用域
|
|
604
604
|
* @param {Object} config 配置参数
|
|
605
|
-
* @return {
|
|
605
|
+
* @return {Object} 返回一个Mysql类实例
|
|
606
606
|
*/
|
|
607
|
-
|
|
607
|
+
function mysqlAdmin(scope, config) {
|
|
608
608
|
if (!scope) {
|
|
609
609
|
scope = 'sys';
|
|
610
610
|
}
|
|
@@ -614,20 +614,12 @@ async function mysqlAdmin(scope, config) {
|
|
|
614
614
|
return $.pool.mysql[scope];
|
|
615
615
|
}
|
|
616
616
|
|
|
617
|
-
//
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
try {
|
|
621
|
-
// 双重检查,防止在等待锁期间其他线程已创建实例
|
|
622
|
-
if (!$.pool.mysql[scope]) {
|
|
623
|
-
$.pool.mysql[scope] = new Mysql(config);
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
return $.pool.mysql[scope];
|
|
627
|
-
} finally {
|
|
628
|
-
// 释放锁
|
|
629
|
-
release();
|
|
617
|
+
// 同步方式创建实例(无需锁,因为Node.js是单线程的)
|
|
618
|
+
if (!$.pool.mysql[scope]) {
|
|
619
|
+
$.pool.mysql[scope] = new Mysql(config);
|
|
630
620
|
}
|
|
621
|
+
|
|
622
|
+
return $.pool.mysql[scope];
|
|
631
623
|
}
|
|
632
624
|
|
|
633
625
|
/**
|