node.js redis special delete del

https://github.com/luin/ioredis``` const Redis = require(‘ioredis’); const redis = new Redis(); const delScript = local all_keys = {}; local keys = {}; local done = false; local cursor = "0" repeat local result = redis.call("SCAN", cursor, "match", KEYS[1], "count", KEYS[2]) cursor = result[1]; keys = result[2]; for i, key in ipairs(keys) do all_keys[#all_keys+1] = key; end if cursor == "0" then done = true; end until done for i, key in ipairs(all_keys) do redis.call("DEL", key); end return true; ; ...

2020-07-02 · 1 min · 97 words · Me

javascript functions parm default value

https://stackoverflow.com/questions/6600868/set-default-value-of-javascript-object-attributes``` // default num: 0, unit: ‘pics’ function items(op) { const { num, unit } = Object.assign({}, { num: 0, unit: ‘pics’}, op); }

2020-06-30 · 1 min · 24 words · Me

firebase functions cold start use Express.js node.js get problem

System on firebase functions always have problems that code start. https://medium.com/@siriwatknp/cold-start-workaround-in-firebase-cloud-functions-8e9db1426bd3 So google office https://firebase.google.com/docs/functions/networking const http = require('http'); const functions = require('firebase-functions'); // Setting the `keepAlive` option to `true` keeps // connections open between function invocations const agent = new http.Agent({keepAlive: true}); exports.function = functions.https.onRequest((request, response) => { req = http.request({ host: '', port: 80, path: '', method: 'GET', agent: agent, // Holds the connection open after the first invocation }, res => { let rawData = ''; res.setEncoding('utf8'); res.on('data', chunk => { rawData += chunk; }); ... Two line let me confused. Not only me. const agent = new http.Agent({keepAlive: true}); agent: agent, // Holds the connection open after the first invocation Some guy same me. ...

2020-06-17 · 2 min · 217 words · Me

nodejs node.js console.log util.format ...args for logging log

For logging mulit args with “%O”``` const util = require(‘util’); function d(…args) { if (typeof (console) !== ‘undefined’) { console.log(’[Logging]’, util.format(…args)); } }

2020-06-12 · 1 min · 23 words · Me

nodejs express cache redis

https://sematext.com/blog/expressjs-best-practices/``` const express = require(’express’) const app = express() const redis = require(‘redis’) ​ const redisClient = redis.createClient(6379) ​ async function getSomethingFromDatabase (req, res, next) { try { const { id } = req.params; const data = await database.query() ​ // Set data to Redis redisClient.setex(id, 3600, JSON.stringify(data)) res.status(200).send(data) } catch (err) { console.error(err) res.status(500) } } ​ function cache (req, res, next) { const { id } = req.params ​ redisClient.get(id, (err, data) => { if (err) { return res.status(500).send(err) } ...

2020-05-18 · 1 min · 126 words · Me