add: LRU缓存机制

This commit is contained in:
2020-05-25 22:27:39 +08:00
parent 06d357b002
commit 3770b30040
3 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import LRUCache from '../../src/array/lru-cache'
test('LRU缓存机制', () => {
const cache = new LRUCache(2 /* 缓存容量 */)
cache.put(1, 1)
cache.put(2, 2)
expect(cache.get(1)).toBe(1) // 返回 1
cache.put(3, 3) // 该操作会使得密钥 2 作废
expect(cache.get(2)).toBe(-1) // 返回 -1 (未找到)
cache.put(4, 4) // 该操作会使得密钥 1 作废
expect(cache.get(1)).toBe(-1) // 返回 -1 (未找到)
expect(cache.get(3)).toBe(3) // 返回 3
expect(cache.get(4)).toBe(4) // 返回 4
})