本文主要是介绍nodejs redis client api,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于mac redis client无法进行快速批量删除,故想通过脚本来执行批量删除。
一、安装依赖
npm install ioredis mocha
二、脚本
test.js
const Redis = require('ioredis');const redis = new Redis({port: 6379, // Redis porthost: 'xxxx.redis.rds.aliyuncs.com', // Redis hostfamily: 6, // 4 (IPv4) or 6 (IPv6)password: 'xxxx',db: 0
});describe('', async () => {it.skip('设置key', async () => {try {await redis.set('foo', 'bar'); // returns promise which resolves to string, "OK"} catch (error) {await console.error(error);}});it.skip('查询指定key', async () => {try {var result = await redis.get('USER_REMAINING_-1103');await console.log(`GET USER_REMAINING_-1103:${result}`); // Promise resolves to "bar"} catch (error) {await console.error(error);}});it('删除 USER_REMAINING_', async () => {try {for (let index = 1209; index < 1309; index++) {var result = await redis.del(`USER_REMAINING_-${index}`);await console.log(`DEL USER_REMAINING_-${index}:${result}:${result}`); // Promise resolves to "bar"}} catch (error) {await console.error(error);}});it('删除 GIFT_TOTAL_NUM_', async () => {try {for (let index = 1; index < 6; index++) {var result = await redis.del(`GIFT_TOTAL_NUM_${index}`);await console.log(`DEL GIFT_TOTAL_NUM_${index}:${result}`); // Promise resolves to "bar"}} catch (error) {await console.error(error);}});it('删除 GIFT_SEND_DAY_NUM_', async () => {try {for (let index = 1; index < 6; index++) {var result = await redis.del(`GIFT_SEND_DAY_NUM_${index}`);await console.log(`DEL GIFT_SEND_DAY_NUM_${index}:${result}`); // Promise resolves to "bar"}} catch (error) {await console.error(error);}});
});
三、执行结果
这篇关于nodejs redis client api的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!