二、ioredis
https://www.cnblogs.com/JhoneLee/p/9355993.html
通过使用ioredis,可以方便的进行node对redis进行操作,包说明如下:https://www.npmjs.com/package/ioredis
通过其封装的管道操作pipeline 实现redis的事务操作
ioredis使用如下所示:
export default { port:8079, host:‘xxxxx‘, family:4, password:‘123456‘, retryStrategy(times){ return Math.min(times*50,2000); }, reconnectOnError(err){ if(err.message.slice(0,8) == ‘READONLY‘){ return true; } } } const redis = new Redis(conf);
/** * 配置store对象的get set destroy属性即可操作session对象 * 通过简单读取session.xxx 即可触发get方法,session={...} 即可触发set方法 session = null 或重置 session即可触发destroy方法 * koa-session 与 koa-session2区别在于 koa-session2支持 async/await同步操作 * koa-session2 需要extends Store才能使用 */ // koa-session配置如下 export default { async get(key, maxAge, { rolling }){ // console.log(‘get‘,key,maxAge,rolling); let res = await redis.hgetall(key).then(r=>r).catch(e=>{ {error:e} }); return res; console.log(‘get‘,res); }, async set(key, sess, maxAge, { rolling, changed }){ console.log(‘set‘,key,sess,maxAge,rolling,changed); if(changed){ let seconds = Math.floor(maxAge/1000); redis.hmset(key,sess); redis.expire(key,seconds); } }, async destroy(key){ console.log(‘destory‘,key); await redis.del(key); } } //koa-session2 class RedisStore extends Store { constructor(){ super(); } async get(key,ctx){ // console.log(‘get‘,key,maxAge,rolling); let res = await redis.hgetall(key).then(r=>r).catch(e=>{ {error:e} }); return res; } async set(session, { sid = this.getID(24), maxAge = 1000000 } = {}, ctx){ console.log(‘set‘,session.openid,maxAge,sid); let seconds = Math.floor(maxAge/1000); await redis.hmset(sid,session); await redis.expire(sid,seconds); return sid; } async destroy(key,ctx){ console.log(‘destory‘,key); await redis.del(key); } } let store = new RedisStore(); export default store;
sequelize数据库操作
https://blog.csdn.net/weixin_44665610/article/details/103138203
查询一条记录 findOne
async function get(params) { return await demo.findOne({ where:{ id:params.id } }).then(result=>{ return result }) }
更新数据update
async function name(params) { return await demo.update( { name:params.name //更新的字段 }, { where:{ id:params.id //更新哪条数据 } } ).then(result=>{ return result; }) }
操作redis数据库hgetall
https://blog.csdn.net/themagickeyjianan/article/details/90515265
function get_uinfo_inredis(uid, callback) { if (center_redis === null) { callback(Respones.SYSTEM_ERR, null); return; } var key = "bycw_center_user_uid_" + uid; center_redis.hgetall(key, function(err, data) { if (err) { callback(Respones.SYSTEM_ERR, null); return; } var uinfo = data; uinfo.uface = parseInt(uinfo.uface); uinfo.usex = parseInt(uinfo.usex); uinfo.uvip = parseInt(uinfo.uvip); uinfo.is_guest = parseInt(uinfo.is_guest); callback(Respones.OK, uinfo); }); }
client.hgetall("hash_vm", function(err, obj) { var vm_list = []; for (key in obj) { // console.dir(obj); // console.log(JSON.parse(obj[key])); vm_list.push(JSON.parse(obj[key])); } console.log(vm_list); });