Last active
January 28, 2020 08:22
-
-
Save crazyones110/5fcd3e866a37639d11135a3367e2db75 to your computer and use it in GitHub Desktop.
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
const env = process.env.NODE_ENV // 环境参数,是在 npm script 里设置的 | |
let MYSQL_CONF | |
let REDIS_CONF | |
if (env === "dev") { | |
// mysql | |
MYSQL_CONF = { | |
host: "localhost", | |
user: "root", | |
password: "123456", | |
port: "3306", | |
database: "my_blog2" // 相当于 use my_blog2 | |
} | |
//redis | |
REDIS_CONF = { | |
port: 6379, | |
host: "127.0.0.1" | |
} | |
} | |
if (env === "production") { | |
// mysql | |
MYSQL_CONF = { | |
host: "localhost", | |
user: "root", | |
password: "123456", | |
port: "3306", | |
database: "my_blog2" // 相当于 use my_blog2 | |
} | |
//redis | |
REDIS_CONF = { | |
port: 6379, | |
host: "127.0.0.1" | |
} | |
} | |
module.exports = { | |
MYSQL_CONF, | |
REDIS_CONF | |
} |
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
const mysql = require("mysql") | |
const { MYSQL_CONF } = require("../config/db") | |
// 创建链接对象 | |
const con = mysql.createConnection(MYSQL_CONF) | |
// 开始连接 | |
con.connect() | |
// 统一执行 sql 的函数 | |
function exec(sql) { // sql 是 sql 语句 | |
const promise = new Promise((resolve, reject) => { | |
con.query(sql, (err, result) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(result) | |
}) | |
}) | |
return promise | |
} | |
module.exports = { | |
exec, | |
escape: mysql.escape | |
} |
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
const redis = require("redis") | |
const { REDIS_CONF } = require("../config/db") | |
// 创建客户端 | |
const redisClient = redis.createClient(REDIS_CONF.port, REDIS_CONF.host) | |
redisClient.on("error", err => { | |
console.error(err) | |
}) | |
const set = (key, val) => { | |
if (typeof val === "object") { | |
val = JSON.stringify(val) | |
} | |
redisClient.set(key, val, redis.print) // redis.print 的作用是打印出是否 set 成功的消息 | |
} | |
const get = key => { | |
return new Promise((resolve, reject) => { | |
redisClient.get(key, (err, val) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
if (val === null) { // 瞎传了一个 key,值可能是 null | |
resolve(null) | |
return | |
} | |
try { // 兼容 JSON 转换的这种格式,很巧妙! | |
resolve( | |
JSON.parse(val) | |
) | |
} catch (ex) { | |
resolve(val) | |
} | |
}) | |
}) | |
} | |
module.exports = { | |
set, | |
get | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment