From d84129429a7553211fffd956fc2c05675aa03a06 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 28 Feb 2024 20:36:29 -0300 Subject: feat: always reuse cached files younger than 10 seconds This feature depends on lfs being available. --- src/luarocks/fetch.lua | 27 ++++++++++++++++++++------- src/luarocks/fs/lua.lua | 12 ++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 373a998f..435108ae 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -33,7 +33,21 @@ function fetch.fetch_caching(url, mirroring) local name = repo_url:gsub("[/:]","_") local cache_dir = dir.path(cfg.local_cache, name) local ok = fs.make_dir(cache_dir) - local lock = ok and fs.lock_access(cache_dir) + + local cachefile = dir.path(cache_dir, filename) + local checkfile = cachefile .. ".check" + + if (fs.file_age(checkfile) < 10 or + cfg.aggressive_cache and (not name:match("^manifest"))) and fs.exists(cachefile) + then + return cachefile, nil, nil, true + end + + local lock, errlock + if ok then + lock, errlock = fs.lock_access(cache_dir) + end + if not (ok and lock) then cfg.local_cache = fs.make_temp_dir("local_cache") if not cfg.local_cache then @@ -47,13 +61,12 @@ function fetch.fetch_caching(url, mirroring) lock = fs.lock_access(cache_dir) end - local cachefile = dir.path(cache_dir, filename) - if cfg.aggressive_cache and (not name:match("^manifest")) and fs.exists(cachefile) then - fs.unlock_access(lock) - return cachefile, nil, nil, true - end - local file, err, errcode, from_cache = fetch.fetch_url(url, cachefile, true, mirroring) + + local fd = io.open(checkfile, "wb") + fd:write("!") + fd:close() + fs.unlock_access(lock) if not file then return nil, err or "Failed downloading "..url, errcode diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 55c64b72..f64b0c18 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -291,6 +291,14 @@ end if lfs_ok then +function fs_lua.file_age(filename) + local attr = lfs.attributes(filename) + if attr and attr.change then + return os.difftime(os.time(), attr.change) + end + return math.huge +end + function fs_lua.lock_access(dirname, force) fs.make_dir(dirname) if force then @@ -669,6 +677,10 @@ function fs_lua.exists(file) return util.exists(fs.absolute_name(file)) end +function fs_lua.file_age(_) + return math.huge +end + end --------------------------------------------------------------------- -- cgit v1.2.3-55-g6feb