fix some cachign

This commit is contained in:
Barrett Ruth 2025-10-01 17:08:36 -04:00
parent a925686a17
commit e6c09a4897
5 changed files with 42 additions and 25 deletions

View file

@ -31,6 +31,7 @@
local M = {}
local logger = require('cp.log')
local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json'
local cache_data = {}
local loaded = false
@ -41,7 +42,7 @@ function M.load()
end
if vim.fn.filereadable(cache_file) == 0 then
cache_data = {}
vim.fn.writefile({}, cache_file)
loaded = true
return
end
@ -57,28 +58,19 @@ function M.load()
if ok then
cache_data = decoded
else
cache_data = {}
logger.log('Could not decode json in cache file', vim.log.levels.ERROR)
end
loaded = true
end
function M.save()
local ok, _ = pcall(vim.fn.mkdir, vim.fn.fnamemodify(cache_file, ':h'), 'p')
if not ok then
vim.schedule(function()
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p')
end)
return
end
vim.schedule(function()
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p')
local encoded = vim.json.encode(cache_data)
local lines = vim.split(encoded, '\n')
local write_ok, _ = pcall(vim.fn.writefile, lines, cache_file)
if not write_ok then
vim.schedule(function()
vim.fn.writefile(lines, cache_file)
end)
end
local encoded = vim.json.encode(cache_data)
local lines = vim.split(encoded, '\n')
vim.fn.writefile(lines, cache_file)
end)
end
---@param platform string

View file

@ -7,7 +7,6 @@ local logger = require('cp.log')
local platforms = constants.PLATFORMS
function M.handle_cache_command(cmd)
cmd.platform = cmd.platform:lower()
if cmd.subcommand == 'clear' then
cache.load()
if cmd.platform then

View file

@ -128,11 +128,10 @@ function M.scrape_problem_tests(platform, contest_id, problem_id, callback)
expected_file
)
end
if type(callback) == 'function' then
callback(result.data)
end
end)
if type(callback) == 'function' then
callback(result.data)
end
end,
})
end

View file

@ -45,7 +45,25 @@ local function scrape_contest_problems(platform, contest_id, problems)
end
for _, prob in ipairs(missing_problems) do
scraper.scrape_problem_tests(platform, contest_id, prob.id)
scraper.scrape_problem_tests(platform, contest_id, prob.id, function(result)
local cached_tests = {}
for i, test_case in ipairs(result.tests) do
table.insert(cached_tests, {
index = i,
input = test_case.input,
expected = test_case.expected,
})
end
cache.set_test_cases(
platform,
contest_id,
state.get_problem_id(),
cached_tests,
result.timeout_ms,
result.memory_mb
)
end)
end
end
@ -63,12 +81,15 @@ function M.setup_contest(platform, contest_id, problem_id, language)
end
state.set_contest_id(contest_id)
-- TODO: should check cache here, & other uses of gt_contest_data validate them
logger.log('Fetching contests problems...', vim.log.levels.INFO, true)
scraper.scrape_contest_metadata(platform, contest_id, function(result)
local problems = result.problems
logger.log(('found %d problems'):format(#problems))
cache.set_contest_data(platform, contest_id, problems)
logger.log(('Found %d problems for %s contest %s'):format(#problems, platform, contest_id))
local target_problem = problem_id or problems[1].id
@ -166,7 +187,12 @@ function M.navigate_problem(direction, language)
return
end
M.setup_problem(contest_id, problems[new_index].id, language)
local cp = require('cp')
local args = { platform, contest_id, problems[new_index].id }
if language then
vim.list_extend(args, { '--lang', language })
end
cp.handle_command({ fargs = args })
end
return M

View file

@ -62,6 +62,7 @@ function M.toggle_interactive()
local cache = require('cp.cache')
cache.load()
local contest_data = cache.get_contest_data(platform, contest_id)
vim.print('checking cache - contes_data (DLETE ME): ', contest_data)
if contest_data and not contest_data.interactive then
logger.log(
'This is NOT an interactive problem. Use :CP run instead - aborting.',