2622. 有时间限制的缓存
2622. 有时间限制的缓存
题目
Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
The class has three public methods:
set(key, value, duration)
: accepts an integer key
, an integer value
, and a duration
in milliseconds. Once the duration
has elapsed, the key should be inaccessible. The method should return true
if the same un-expired key already exists and false
otherwise. Both the value and duration should be overwritten if the key already exists.
get(key)
: if an un-expired key exists, it should return the associated value. Otherwise it should return -1
.
count()
: returns the count of un-expired keys.
Example 1:
Input:
actions = ["TimeLimitedCache", "set", "get", "count", "get"]
values = [[], [1, 42, 100], [1], [], [1]]
timeDelays = [0, 0, 50, 50, 150]
Output: [null, false, 42, 1, -1]
Explanation:
At t=0, the cache is constructed.
At t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.
At t=50, key=1 is requested and the value of 42 is returned.
At t=50, count() is called and there is one active key in the cache.
At t=100, key=1 expires.
At t=150, get(1) is called but -1 is returned because the cache is empty.
Example 2:
Input:
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]
Output: [null, false, true, 50, 50, -1, 0]
Explanation:
At t=0, the cache is constructed.
At t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.
At t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.
At t=50, get(1) is called which returned 50.
At t=120, get(1) is called which returned 50.
At t=140, key=1 expires.
At t=200, get(1) is called but the cache is empty so -1 is returned.
At t=250, count() returns 0 because the cache is empty.
Constraints:
0 <= key, value <= 10^9
0 <= duration <= 1000
1 <= actions.length <= 100
actions.length === values.length
actions.length === timeDelays.length
0 <= timeDelays[i] <= 1450
actions[i]
is one of "TimeLimitedCache", "set", "get" and "count"- First action is always "TimeLimitedCache" and must be executed immediately, with a 0-millisecond delay
题目大意
编写一个类,它允许获取和设置键-值对,并且每个键都有一个 过期时间 。
该类有三个公共方法:
set(key, value, duration)
:接收参数为整型键 key
、整型值 value
和以毫秒为单位的持续时间 duration
。一旦 duration
到期后,这个键就无法访问。如果相同的未过期键已经存在,该方法将返回 true
,否则返回 false
。如果该键已经存在,则它的值和持续时间都应该被覆盖。
get(key)
:如果存在一个未过期的键,它应该返回这个键相关的值。否则返回 -1
。
count()
:返回未过期键的总数。
提示:
0 <= key, value <= 10^9
0 <= duration <= 1000
1 <= actions.length <= 100
actions.length === values.length
actions.length === timeDelays.length
0 <= timeDelays[i] <= 1450
actions[i]
是 "TimeLimitedCache"、"set"、"get" 和 "count" 中的一个。- 第一个操作始终是 "TimeLimitedCache" 而且一定会以 0 毫秒的延迟立即执行
解题思路
constructor
:- 使用
Map
数据结构来存储键值对,其中每个键对应的值为一个数组[value, timer]
,其中value
是存储的值,timer
是该键的定时器,用于删除过期键。
- 使用
set(key, value, duration)
:- 该方法将给定的键
key
和值value
存入缓存,并设置该键值对的有效期duration
毫秒。 - 如果相同键已经存在且未过期,则返回
true
,否则返回false
。 - 使用
setTimeout
定时函数,在到期时间后自动删除该键值对。 - 如果键已经存在且定时器没有到期,首先清除之前的定时器,以确保新设置的
duration
覆盖旧的时间。
- 该方法将给定的键
get(key)
:- 查询缓存中是否存在未过期的键
key
。 - 如果键存在且未过期,则返回其对应的值,否则返回
-1
。
- 查询缓存中是否存在未过期的键
count()
:- 返回当前缓存中未过期的键值对数量,直接通过
Map
对象的size
属性来获取当前键的数量。
- 返回当前缓存中未过期的键值对数量,直接通过
复杂度分析
- 时间复杂度:
set
:每次调用set
,查找和插入操作的时间复杂度为O(1)
,删除旧的定时器和设置新的定时器也为常数时间操作。因此set
的时间复杂度为O(1)
。get
:查找键的时间复杂度为O(1)
。count
:Map.size
的访问是常数时间操作,时间复杂度为O(1)
。
- 空间复杂度:
O(n)
,其中n
为缓存中存储的键值对数量,使用了Map
来存储键值对。
代码
var TimeLimitedCache = function () {
this.cache = 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) {
const timer = setTimeout(() => this.cache.delete(key), duration);
const exist = this.cache.has(key);
if (exist) {
const oldTimer = this.cache.get(key)[1];
clearTimeout(oldTimer);
}
this.cache.set(key, [value, timer]);
return exist;
};
/**
* @param {number} key
* @return {number} value associated with key
*/
TimeLimitedCache.prototype.get = function (key) {
if (this.cache.has(key)) return this.cache.get(key)[0];
return -1;
};
/**
* @return {number} count of non-expired keys
*/
TimeLimitedCache.prototype.count = function () {
return this.cache.size;
};
/**
* const timeLimitedCache = new TimeLimitedCache()
* timeLimitedCache.set(1, 42, 1000); // false
* timeLimitedCache.get(1) // 42
* timeLimitedCache.count() // 1
*/
相关题目
题号 | 标题 | 题解 | 标签 | 难度 |
---|---|---|---|---|
2627 | 函数防抖 | [✓] | ||
2636 | Promise 对象池 🔒 | [✓] | ||
2637 | 有时间限制的 Promise 对象 | [✓] |