@@ -9,22 +9,37 @@ var session = require('../session');
99
1010const plugin = new Plugin ( 50 , 'cache' , '' , 'Plugin to provide local cache.' ) ;
1111
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 ) ;
1327 const problems = cache . get ( h . KEYS . problems ) ;
1428 if ( problems ) {
1529 log . debug ( 'cache hit: problems.json' ) ;
1630 return cb ( null , problems ) ;
1731 }
1832
19- plugin . next . getProblems ( function ( e , problems ) {
33+ plugin . next . getProblems ( needTranslation , function ( e , problems ) {
2034 if ( e ) return cb ( e ) ;
2135
2236 cache . set ( h . KEYS . problems , problems ) ;
2337 return cb ( null , problems ) ;
2438 } ) ;
2539} ;
2640
27- plugin . getProblem = function ( problem , 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 ) {
@@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
4257 }
4358 }
4459
45- plugin . next . getProblem ( problem , function ( e , _problem ) {
60+ plugin . next . getProblem ( problem , needTranslation , function ( e , _problem ) {
4661 if ( e ) return cb ( e ) ;
4762
4863 plugin . saveProblem ( _problem ) ;
0 commit comments