{ "data": { "question": { "questionId": "2762", "questionFrontendId": "2622", "categoryTitle": "JavaScript", "boundTopicId": 2222270, "title": "Cache With Time Limit", "titleSlug": "cache-with-time-limit", "content": "
Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
\n\nThe class has three public methods:
\n\nset(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.
\n
Example 1:
\n\n\nInput: \nactions = ["TimeLimitedCache", "set", "get", "count", "get"]\nvalues = [[], [1, 42, 100], [1], [], [1]]\ntimeDelays = [0, 0, 50, 50, 150]\nOutput: [null, false, 42, 1, -1]\nExplanation:\nAt t=0, the cache is constructed.\nAt 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.\nAt t=50, key=1 is requested and the value of 42 is returned.\nAt t=50, count() is called and there is one active key in the cache.\nAt t=100, key=1 expires.\nAt t=150, get(1) is called but -1 is returned because the cache is empty.\n\n\n
Example 2:
\n\n\nInput: \nactions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]\nvalues = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\ntimeDelays = [0, 0, 40, 50, 120, 200, 250]\nOutput: [null, false, true, 50, 50, -1, 0]\nExplanation:\nAt t=0, the cache is constructed.\nAt 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.\nAt 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.\nAt t=50, get(1) is called which returned 50.\nAt t=120, get(1) is called which returned 50.\nAt t=140, key=1 expires.\nAt t=200, get(1) is called but the cache is empty so -1 is returned.\nAt t=250, count() returns 0 because the cache is empty.\n\n\n
\n
Constraints:
\n\n0 <= key, value <= 109
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"编写一个类,它允许获取和设置键-值对,并且每个键都有一个 过期时间 。
\n\n该类有三个公共方法:
\n\nset(key, value, duration)
:接收参数为整型键 key
、整型值 value
和以毫秒为单位的持续时间 duration
。一旦 duration
到期后,这个键就无法访问。如果相同的未过期键已经存在,该方法将返回 true
,否则返回 false
。如果该键已经存在,则它的值和持续时间都应该被覆盖。
get(key)
:如果存在一个未过期的键,它应该返回这个键相关的值。否则返回 -1
。
count()
:返回未过期键的总数。
\n\n
示例 1:
\n\n\n输入: \nactions = [\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\nvalues = [[], [1, 42, 100], [1], [], [1]]\ntimeDeays = [0, 0, 50, 50, 150]\n输出: [null, false, 42, 1, -1]\n解释:\n在 t=0 时,缓存被构造。\n在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 100ms 。因为该值不存在,因此返回false。\n在 t=50 时,请求 key=1 并返回值 42。\n在 t=50 时,调用 count() ,缓存中有一个未过期的键。\n在 t=100 时,key=1 到期。\n在 t=150 时,调用 get(1) ,返回 -1,因为缓存是空的。\n\n\n
示例 2:
\n\n\n输入:\nactions = [\"TimeLimitedCache\", \"set\", \"set\", \"get\", \"get\", \"get\", \"count\"]\nvalues = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\ntimeDelays = [0, 0, 40, 50, 120, 200, 250]\n输出: [null, false, true, 50, 50, -1]\n解释:\n在 t=0 时,缓存被构造。\n在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 50ms。因为该值不存在,因此返回false。\n当 t=40 时,添加一个键值对 (1: 50) ,过期时间为 100ms。因为一个未过期的键已经存在,返回 true 并覆盖这个键的旧值。\n在 t=50 时,调用 get(1) ,返回 50。\n在 t=120 时,调用 get(1) ,返回 50。\n在 t=140 时,key=1 过期。\n在 t=200 时,调用 get(1) ,但缓存为空,因此返回 -1。\n在 t=250 时,count() 返回0 ,因为缓存是空的,没有未过期的键。\n\n\n
\n\n
提示:
\n\n0 <= key, value <= 109
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\" 中的一个。\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]\n[\"TimeLimitedCache\", \"set\", \"set\", \"get\", \"get\", \"get\", \"count\"]\n[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\n[0, 0, 40, 50, 120, 200, 250]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n