diff --git a/lib/cache.js b/lib/cache.js index 42efad0..dc10d52 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -9,6 +9,12 @@ cache.init = function() { file.mkdir(file.cacheDir()); }; +cache.deleteAll = function () { + cache.list().forEach(value => { + cache.del(value.name); + }) +}; + cache.get = function(k) { const fullpath = file.cacheFile(k); if (!file.exist(fullpath)) return null; diff --git a/lib/helper.js b/lib/helper.js index d256103..9c5ff43 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -52,11 +52,12 @@ const LANGS = [ const h = {}; h.KEYS = { - user: '../user', - stat: '../stat', - plugins: '../../plugins', - problems: 'problems', - problem: p => p.fid + '.' + p.slug + '.' + p.category + user: '../user', + stat: '../stat', + plugins: '../../plugins', + problems: 'problems', + translation: 'translationConfig', + problem: p => p.fid + '.' + p.slug + '.' + p.category }; h.prettyState = function(state) { diff --git a/lib/plugins/cache.js b/lib/plugins/cache.js index e3a132c..d631686 100644 --- a/lib/plugins/cache.js +++ b/lib/plugins/cache.js @@ -9,7 +9,21 @@ var session = require('../session'); const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.'); -plugin.getProblems = function(needTranslation, cb) { +// this function will clear all caches if needTranslation is different than stored +// it will also store the new needTranslation into cache automatically +function clearCacheIfTchanged(needTranslation) { + const translationConfig = cache.get(h.KEYS.translation); + if (!translationConfig || translationConfig['useEndpointTranslation'] != needTranslation) { + // cache doesn't have the key => old cache version, need to update + // or cache does have the key but it contains a different value + cache.deleteAll(); + cache.set(h.KEYS.translation, { useEndpointTranslation: needTranslation }); + log.debug('cache cleared: -T option changed'); + } +} + +plugin.getProblems = function (needTranslation, cb) { + clearCacheIfTchanged(needTranslation); const problems = cache.get(h.KEYS.problems); if (problems) { log.debug('cache hit: problems.json'); @@ -24,7 +38,8 @@ plugin.getProblems = function(needTranslation, cb) { }); }; -plugin.getProblem = function(problem, needTranslation, cb) { +plugin.getProblem = function (problem, needTranslation, cb) { + clearCacheIfTchanged(needTranslation); const k = h.KEYS.problem(problem); const _problem = cache.get(k); if (_problem) {