2021-05-04 21:13:24 +00:00
|
|
|
--[[
|
|
|
|
"Global" LRU cache used by Document & friends.
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local Cache = require("cache")
|
|
|
|
local CanvasContext = require("document/canvascontext")
|
|
|
|
local DataStorage = require("datastorage")
|
2021-06-17 15:08:21 +00:00
|
|
|
local logger = require("logger")
|
2021-05-04 21:13:24 +00:00
|
|
|
|
|
|
|
local function calcCacheMemSize()
|
|
|
|
local min = DGLOBAL_CACHE_SIZE_MINIMUM
|
|
|
|
local max = DGLOBAL_CACHE_SIZE_MAXIMUM
|
|
|
|
local calc = Cache:_calcFreeMem() * (DGLOBAL_CACHE_FREE_PROPORTION or 0)
|
2021-09-09 23:07:04 +00:00
|
|
|
return math.min(max, math.max(min, calc))
|
|
|
|
end
|
2021-09-11 18:00:06 +00:00
|
|
|
local cache_size = calcCacheMemSize()
|
|
|
|
|
2021-09-09 23:07:04 +00:00
|
|
|
|
|
|
|
local function computeCacheSize()
|
2021-09-11 18:00:06 +00:00
|
|
|
local mb_size = cache_size / 1024 / 1024
|
2021-09-09 23:07:04 +00:00
|
|
|
|
|
|
|
-- If we end up with a not entirely ridiculous cache size, use that...
|
|
|
|
if mb_size >= 8 then
|
|
|
|
logger.dbg(string.format("Allocating a %dMB budget for the global document cache", mb_size))
|
2021-09-11 18:00:06 +00:00
|
|
|
return cache_size
|
2021-09-09 23:07:04 +00:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function computeCacheSlots()
|
2021-09-11 18:00:06 +00:00
|
|
|
local mb_size = cache_size / 1024 / 1024
|
2021-06-17 15:08:21 +00:00
|
|
|
|
2021-09-09 23:07:04 +00:00
|
|
|
--- ...otherwise, effectively disable the cache by making it single slot...
|
|
|
|
if mb_size < 8 then
|
|
|
|
logger.dbg(string.format("Setting up a minimal single slot global document cache"))
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2021-05-04 21:13:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local DocCache = Cache:new{
|
2021-09-09 23:07:04 +00:00
|
|
|
slots = computeCacheSlots(),
|
|
|
|
size = computeCacheSize(),
|
2021-05-04 21:13:24 +00:00
|
|
|
-- Average item size is a screen's worth of bitmap, mixed with a few much smaller tables (pgdim, pglinks, etc.), hence the / 3
|
|
|
|
avg_itemsize = math.floor(CanvasContext:getWidth() * CanvasContext:getHeight() * (CanvasContext.is_color_rendering_enabled and 4 or 1) / 3),
|
2021-05-07 01:59:27 +00:00
|
|
|
-- Rely on CacheItem's eviction callback to free resources *immediately* on eviction.
|
|
|
|
enable_eviction_cb = true,
|
2021-05-04 21:13:24 +00:00
|
|
|
disk_cache = true,
|
|
|
|
cache_path = DataStorage:getDataDir() .. "/cache/",
|
|
|
|
}
|
|
|
|
|
|
|
|
return DocCache
|