implemented LRU cache
This commit is contained in:
@@ -3,7 +3,7 @@ import { openDB, deleteDB } from "../scripts/idb/index.js"
|
||||
|
||||
const MAX_TIMESTAMP_DIFFERENCE = 3
|
||||
|
||||
describe("sanity check", () => {
|
||||
xdescribe("sanity check", () => {
|
||||
it("works", () => {
|
||||
expect(true).toBeTrue()
|
||||
})
|
||||
@@ -38,6 +38,13 @@ describe("SlopDB", () => {
|
||||
describe("version 2", () => {
|
||||
|
||||
let slopdb
|
||||
let mock_clock
|
||||
|
||||
beforeAll(() => {
|
||||
const j_clock = jasmine.clock()
|
||||
mock_clock = j_clock.install()
|
||||
mock_clock.mockDate(new Date(2020, 1, 1))
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
slopdb = new SlopDB(2)
|
||||
@@ -60,17 +67,20 @@ describe("SlopDB", () => {
|
||||
|
||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||
|
||||
await cache.store(slop_url.toString())
|
||||
const new_item = await cache.store(slop_url.toString())
|
||||
const store_time = Date.now()
|
||||
|
||||
mock_clock.tick(1000)
|
||||
const cached_item = await cache.get(slop_url.toString())
|
||||
|
||||
expect(cached_item.url).toEqual(slop_url.toString())
|
||||
expect(cached_item.check_timestamp).toBeLessThanOrEqual(store_time)
|
||||
expect(cached_item.check_timestamp).toBeGreaterThan(store_time - MAX_TIMESTAMP_DIFFERENCE)
|
||||
expect(new_item.check_timestamp).toBeCloseTo(store_time)
|
||||
expect(new_item.check_timestamp).toBeGreaterThan(store_time - MAX_TIMESTAMP_DIFFERENCE)
|
||||
})
|
||||
|
||||
it("updates a cached url's timestamp when it is accessed", async () => {
|
||||
const mock_clock = clock.install()
|
||||
|
||||
|
||||
const cache = slopdb.get_check_cache()
|
||||
|
||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||
@@ -79,8 +89,30 @@ describe("SlopDB", () => {
|
||||
|
||||
mock_clock.tick(1000 * 30)
|
||||
|
||||
const cached_item = cache.get(slop_url.toString())
|
||||
expect(cached_item.check_timestamp).toEqual(store_time + 30)
|
||||
const cached_item = await cache.get(slop_url.toString())
|
||||
expect(cached_item.check_timestamp).toBeGreaterThan(store_time + 10)
|
||||
})
|
||||
|
||||
it("evicts the least recently accessed URL when an item is added to a full cache", async () => {
|
||||
const cache = slopdb.get_check_cache()
|
||||
cache.cache_capacity = 2
|
||||
|
||||
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
|
||||
await cache.store(slop_url.toString())
|
||||
|
||||
mock_clock.tick(1000)
|
||||
|
||||
const slop_url2 = new URL("https://sloppy-slop.com/sloparticle2")
|
||||
await cache.store(slop_url2.toString())
|
||||
|
||||
mock_clock.tick(1000)
|
||||
|
||||
const slop_url3 = new URL("https://sloppy-slop.com/sloparticle3")
|
||||
await cache.store(slop_url3.toString())
|
||||
|
||||
const get_slop1 = await cache.get(slop_url.toString())
|
||||
expect(get_slop1).toBeUndefined()
|
||||
expect(cache.size).toEqual(2)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user