Files
koreader-mirror/spec/unit/cachesqlite_spec.lua
Frans de Jonge 63bf9d5bdf Add CacheSQLite (#13131)
Persist is intended for larger objects, which works best for things like cached tiles with some metadata.

For many small to medium entries that you nevertheless don't necessarily want to keep around in memory, that seems a bit inefficient.

SQLite then, is effectively a way of minimizing the amount of writes in this scenario.

Relates to #13061. Depends on #13122.
2025-01-29 21:33:44 +01:00

97 lines
3.0 KiB
Lua

describe("CacheSQLite module", function()
local CacheSQLite
local cache
describe("CacheSQLite on disk", function()
setup(function()
require("commonrequire")
CacheSQLite = require("cachesqlite")
cache = CacheSQLite:new{
db_path = "test.db",
size = 1024 * 1024 * 1024,
}
end)
after_each(function()
cache:clear()
cache.size = 1024 * 1024 * 1024
end)
it("should clear cache", function()
cache:clear()
end)
it("should insert and get", function()
local object = {a = 1, b = 2}
cache:insert("test", object)
local deserialized = cache:get("test")
assert.are.same(object, deserialized)
end)
it("should remove object", function()
local object = {a = 1, b = 2}
cache:insert("test", object)
cache:remove("test")
local deserialized = cache:get("test")
assert.is_nil(deserialized)
end)
it("should accept regular object", function()
assert.is_true(cache:willAccept(100))
cache.size = 1024
assert.is_true(cache:insert("test", {a = 1, b = 2}))
end)
it("should reject giant object", function()
assert.is_false(cache:willAccept(1024 * 1024 * 1024))
cache.size = 10
assert.is_false(cache:insert("test", {a = 1, b = 2}))
end)
end)
describe("CacheSQLite in memory", function()
setup(function()
require("commonrequire")
CacheSQLite = require("cachesqlite")
cache = CacheSQLite:new{
db_path = ":memory:",
auto_close = false,
size = 1024 * 1024 * 1024,
}
end)
after_each(function()
cache:clear()
cache.size = 1024 * 1024 * 1024
end)
it("should clear cache", function()
cache:clear()
end)
it("should insert and get", function()
local object = {a = 1, b = 2}
cache:insert("test", object)
local deserialized = cache:get("test")
assert.are.same(object, deserialized)
end)
it("should remove object", function()
local object = {a = 1, b = 2}
cache:insert("test", object)
cache:remove("test")
local deserialized = cache:get("test")
assert.is_nil(deserialized)
end)
it("should accept regular object", function()
assert.is_true(cache:willAccept(100))
cache.size = 1024
assert.is_true(cache:insert("test", {a = 1, b = 2}))
end)
it("should reject giant object", function()
assert.is_false(cache:willAccept(1024 * 1024 * 1024))
cache.size = 10
assert.is_false(cache:insert("test", {a = 1, b = 2}))
end)
end)
end)