nodejs限制IP一段时间内的访问次数
const Redis = require('ioredis');
const {getmac)= require(' getmac');getmac();//获取mac地址
const cache = new Redis({
port: 6300, // Redis port
host: "192.100.50.256", // Redis host
password: "123"
});
cache.get('eggsy').then(data => {
console.log(15, data)
if (data > 5) {
throw new Error('big than 5');
}
if (!data) {
cache.set('eggsy', 1, 'EX', 60);//设置60秒内访问次数大于5次,报错
} else {
cache.incr('eggsy');
}
}).catch(err => {
console.log(err)
})