import moment from 'moment'; export class AUniqCacheDay { dateMap = {}; deleted = false; keepDays = 10; constructor ({ keepDays } = { keepDays: 10 }) { this.keepDays = keepDays; } remove(item) { const day = moment().format('YYYY-MM-DD'); delete this.dateMap[day][item]; } add(item) { const day = moment().format('YYYY-MM-DD'); this.dateMap[day] = { ...this.dateMap[day], [item]: 1, }; this.cleanMap(); } addItems(items) { for (const item of items) { const day = moment().format('YYYY-MM-DD'); this.dateMap[day] = { ...this.dateMap[day], [item]: 1, }; } this.cleanMap(); } cleanMap() { const minute = moment().minute(); const dates = Object.keys(this.dateMap); if (!this.deleted && minute === 0) { for (const d of dates) { if (moment().diff(moment(d), 'days') > this.keepDays) { // 如果当前日期超过1天 delete this.dateMap[d]; } } this.deleted = true; } else if (minute === 1) { this.deleted = false; } } has(item) { const dates = Object.keys(this.dateMap); for (const d of dates) { if (this.dateMap[d] && this.dateMap[d][item]) { return true; } } return false; } isValid(item) { return !this.has(item); } }