@@ -9,22 +9,37 @@ var session = require('../session');
9
9
10
10
const plugin = new Plugin ( 50 , 'cache' , '' , 'Plugin to provide local cache.' ) ;
11
11
12
- plugin . getProblems = function ( 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 ) ;
13
27
const problems = cache . get ( h . KEYS . problems ) ;
14
28
if ( problems ) {
15
29
log . debug ( 'cache hit: problems.json' ) ;
16
30
return cb ( null , problems ) ;
17
31
}
18
32
19
- plugin . next . getProblems ( function ( e , problems ) {
33
+ plugin . next . getProblems ( needTranslation , function ( e , problems ) {
20
34
if ( e ) return cb ( e ) ;
21
35
22
36
cache . set ( h . KEYS . problems , problems ) ;
23
37
return cb ( null , problems ) ;
24
38
} ) ;
25
39
} ;
26
40
27
- plugin . getProblem = function ( problem , cb ) {
41
+ plugin . getProblem = function ( problem , needTranslation , cb ) {
42
+ clearCacheIfTchanged ( needTranslation ) ;
28
43
const k = h . KEYS . problem ( problem ) ;
29
44
const _problem = cache . get ( k ) ;
30
45
if ( _problem ) {
@@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
42
57
}
43
58
}
44
59
45
- plugin . next . getProblem ( problem , function ( e , _problem ) {
60
+ plugin . next . getProblem ( problem , needTranslation , function ( e , _problem ) {
46
61
if ( e ) return cb ( e ) ;
47
62
48
63
plugin . saveProblem ( _problem ) ;
0 commit comments