javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > JavaScrip缓存类

JavaScrip实现一个有时间限制的缓存类的方式

作者:JYeontu

本文将探索 JavaScript 中一种基于自动过期机制的时间限制缓存实现方式,提高数据缓存策略的灵活性和效率,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

题目描述

编写一个类,它允许获取和设置键-值对,并且每个键都有一个 过期时间 。

该类有三个公共方法:

set(key, value, duration) :接收参数为整型键 key 、整型值 value 和以毫秒为单位的持续时间 duration 。一旦 duration 到期后,这个键就无法访问。如果相同的未过期键已经存在,该方法将返回 true ,否则返回 false 。如果该键已经存在,则它的值和持续时间都应该被覆盖。

get(key) :如果存在一个未过期的键,它应该返回这个键相关的值。否则返回 -1 。

count() :返回未过期键的总数。

示例 1:

输入: 
actions = ["TimeLimitedCache", "set", "get", "count", "get"]
values = [[], [1, 42, 100], [1], [], [1]]
timeDeays = [0, 0, 50, 50, 150]
输出: [null, false, 42, 1, -1]
解释:
在 t=0 时,缓存被构造。
在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 100ms 。因为该值不存在,因此返回false。
在 t=50 时,请求 key=1 并返回值 42。
在 t=50 时,调用 count() ,缓存中有一个未过期的键。
在 t=100 时,key=1 到期。
在 t=150 时,调用 get(1) ,返回 -1,因为缓存是空的。

示例 2:

输入:
actions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
values = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
timeDelays = [0, 0, 40, 50, 120, 200, 250]
输出: [null, false, true, 50, 50, -1]
解释:
在 t=0 时,缓存被构造。
在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 50ms。因为该值不存在,因此返回false。
当 t=40 时,添加一个键值对 (1: 50) ,过期时间为 100ms。因为一个未过期的键已经存在,返回 true 并覆盖这个键的旧值。
在 t=50 时,调用 get(1) ,返回 50。
在 t=120 时,调用 get(1) ,返回 50。
在 t=140 时,key=1 过期。
在 t=200 时,调用 get(1) ,但缓存为空,因此返回 -1。
在 t=250 时,count() 返回0 ,因为缓存是空的,没有未过期的键。

提示:

解题思路

1、构造函数 TimeLimitedCache 创建了一个新的 Map 对象,并将其赋值给实例属性 map

var TimeLimitedCache = function () {
    this.map = new Map();
};

2、set 方法用于向缓存中添加一个键值对,并设置该键值对的过期时间

TimeLimitedCache.prototype.set = function (key, value, duration) {
    let res = true;
    if(this.get(key) == -1) res = false;
    const obj = {
        exceedTime :new Date().getTime() + duration,
        value
    };
    this.map.set(key,obj);
    return res;
};

3、get 方法用于获取指定键的值

TimeLimitedCache.prototype.get = function (key) {
    const obj = this.map.get(key);
    if(!obj) return -1;
    const exceedTime = obj.exceedTime;
    if(exceedTime <= new Date().getTime()) return -1;
    return obj.value;
};

4、count 方法用于计算缓存中未过期的键的数量

TimeLimitedCache.prototype.count = function () {
    let cnt = 0;
    const dateTime = new Date().getTime();
    this.map.forEach(obj=>{
        const exceedTime = obj.exceedTime;
        if(exceedTime > dateTime) cnt++;
    })
    return cnt;
};

AC代码

var TimeLimitedCache = function () {
    this.map = new Map();
};

/**
 * @param {number} key
 * @param {number} value
 * @param {number} duration time until expiration in ms
 * @return {boolean} if un-expired key already existed
 */
TimeLimitedCache.prototype.set = function (key, value, duration) {
    let res = true;
    if(this.get(key) == -1) res = false;
    const obj = {
        exceedTime :new Date().getTime() + duration,
        value
    };
    this.map.set(key,obj);
    return res;
};

/**
 * @param {number} key
 * @return {number} value associated with key
 */
TimeLimitedCache.prototype.get = function (key) {
    const obj = this.map.get(key);
    if(!obj) return -1;
    const exceedTime = obj.exceedTime;
    if(exceedTime <= new Date().getTime()) return -1;
    return obj.value;
};

/**
 * @return {number} count of non-expired keys
 */
TimeLimitedCache.prototype.count = function () {
    let cnt = 0;
    const dateTime = new Date().getTime();
    this.map.forEach(obj=>{
        const exceedTime = obj.exceedTime;
        if(exceedTime > dateTime) cnt++;
    })
    return cnt;
};

/**
 * const timeLimitedCache = new TimeLimitedCache()
 * timeLimitedCache.set(1, 42, 1000); // false
 * timeLimitedCache.get(1) // 42
 * timeLimitedCache.count() // 1
 */

说在后面

到此这篇关于JavaScrip实现一个有时间限制的缓存类的方式的文章就介绍到这了,更多相关JavaScrip缓存类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文