Skip to content

Commit 46a76d9

Browse files
committed
feature: Automatically clear cache when -T changed
1 parent cdf819e commit 46a76d9

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/cache.js

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ cache.init = function() {
99
file.mkdir(file.cacheDir());
1010
};
1111

12+
cache.deleteAll = function () {
13+
cache.list().forEach(value => {
14+
cache.del(value.name);
15+
})
16+
};
17+
1218
cache.get = function(k) {
1319
const fullpath = file.cacheFile(k);
1420
if (!file.exist(fullpath)) return null;

lib/helper.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const LANGS = [
5252
const h = {};
5353

5454
h.KEYS = {
55-
user: '../user',
56-
stat: '../stat',
57-
plugins: '../../plugins',
58-
problems: 'problems',
59-
problem: p => p.fid + '.' + p.slug + '.' + p.category
55+
user: '../user',
56+
stat: '../stat',
57+
plugins: '../../plugins',
58+
problems: 'problems',
59+
translation: 'translationConfig',
60+
problem: p => p.fid + '.' + p.slug + '.' + p.category
6061
};
6162

6263
h.prettyState = function(state) {

lib/plugins/cache.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ var session = require('../session');
99

1010
const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.');
1111

12-
plugin.getProblems = function(needTranslation, cb) {
12+
// this function will clear all caches if needTranslation is different than stored
13+
// it will also store the new needTranslation into cache automatically
14+
function clearCacheIfTchanged(needTranslation) {
15+
const translationConfig = cache.get(h.KEYS.translation);
16+
if (!translationConfig || translationConfig['useEndpointTranslation'] != needTranslation) {
17+
// cache doesn't have the key => old cache version, need to update
18+
// or cache does have the key but it contains a different value
19+
cache.deleteAll();
20+
cache.set(h.KEYS.translation, { useEndpointTranslation: needTranslation });
21+
log.debug('cache cleared: -T option changed');
22+
}
23+
}
24+
25+
plugin.getProblems = function (needTranslation, cb) {
26+
clearCacheIfTchanged(needTranslation);
1327
const problems = cache.get(h.KEYS.problems);
1428
if (problems) {
1529
log.debug('cache hit: problems.json');
@@ -24,7 +38,8 @@ plugin.getProblems = function(needTranslation, cb) {
2438
});
2539
};
2640

27-
plugin.getProblem = function(problem, needTranslation, cb) {
41+
plugin.getProblem = function (problem, needTranslation, cb) {
42+
clearCacheIfTchanged(needTranslation);
2843
const k = h.KEYS.problem(problem);
2944
const _problem = cache.get(k);
3045
if (_problem) {

0 commit comments

Comments
 (0)