缓存数据的方法类

class CacheData {
    constructor(options) {
        // 缓存的大小
        this.cacheData = new Map();
        // 当前的数据列表
        this.dataList = [];
        // 缓存大小, 默认缓存10000
        // eslint-disable-next-line no-magic-numbers
        this.cacheSize = options && options.cacheSize || 10000;
    }

    // 添加某条数据
    set(key, value) {
        // 缓存超过限制的话,那么删除前面的数据
        if (this.cacheData.size > this.cacheSize) {
            // 需要删除的key
            const _key = this.dataList.shift();
            // 删除某条数据
            this.delete(_key);
        }
        // 添加key
        this.dataList.push(key);
        // 继续添加数据
        this.cacheData.set(key, value);
    }

    // 删除某条数据
    delete(key) {
        this.cacheData.delete(key);
        // 删除数组中的数据
        const index = this.dataList.indexOf(key);
        if (index !== -1) {
            // 删除该条数据
            this.dataList.splice(index, 1);
        }
    }

    // 获取某条数据
    get(key) {
        return this.cacheData.get(key);
    }

    // 获取某条数据
    has(key) {
        return this.cacheData.has(key);
    }

    // 清除数据
    clear() {
        this.cacheData.clear();
        // 清除列表中的数据
        this.dataList = [];
    }

    // 缓存的大小
    size() {
        return this.cacheData.size;
    }
}

// 导出模块
export default CacheData;
import CacheData from '@/components/tools/extend/CacheData.js';

const module = new FdGlobal();
// 缓存对象
module.CacheData = CacheData;
//  注册全局变量,FdGlobal
window.fdGlobal = module;

// 请求缓存的数据
this.requestCacheData = new window.fdGlobal.CacheData({
   cacheSize: 10000
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值