Skip to content

Commit 86c829e

Browse files
committed
[Cache][ProblemSet] use new style cache file name.
* NOTE: not compatible with existing cache! Signed-off-by: Eric Wang <[email protected]>
1 parent e5d0ff1 commit 86c829e

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

lib/core.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,32 @@ var config = require('./config');
1010
var client = require('./leetcode_client');
1111
var h = require('./helper');
1212

13+
var KEY_USER = '.user';
14+
var KEY_PROBLEMS = 'problems';
15+
16+
function getkey(problem) {
17+
return problem.id + '.' + problem.slug + '.' + problem.category;
18+
}
19+
1320
function saveProblem(problem) {
1421
// it would be better to leave specific problem cache being user
1522
// independent, thus try to reuse existing cache as much as possible
1623
// after changing user.
1724
var p = _.omit(problem, ['locked', 'state', 'starred']);
18-
return cache.set(p.slug, p);
25+
return cache.set(getkey(p), p);
1926
}
2027

2128
function saveUser(user) {
2229
// when auto login enabled, have to save password to re-login later
2330
// otherwise don't dump password for the sake of security.
2431
var u = _.omit(user, config.AUTO_LOGIN ? [] : ['pass']);
25-
cache.set('.user', u);
32+
cache.set(KEY_USER, u);
2633
}
2734

2835
var core = {};
2936

3037
core.getProblems = function(cb) {
31-
var problems = cache.get('all');
38+
var problems = cache.get(KEY_PROBLEMS);
3239
if (problems) {
3340
log.debug('loading from all.json');
3441
return cb(null, problems);
@@ -40,7 +47,7 @@ core.getProblems = function(cb) {
4047
if (e) return cb(e);
4148

4249
saveUser(user);
43-
cache.set('all', problems);
50+
cache.set(KEY_PROBLEMS, problems);
4451
return cb(null, problems);
4552
});
4653
};
@@ -59,9 +66,10 @@ core.getProblem = function(keyword, cb) {
5966
if (!problem)
6067
return cb('Problem not found!');
6168

62-
var problemDetail = cache.get(problem.slug);
69+
var cachekey = getkey(problem);
70+
var problemDetail = cache.get(cachekey);
6371
if (problemDetail) {
64-
log.debug('loading from ' + problem.slug + '.json');
72+
log.debug('loading from ' + cachekey + '.json');
6573
_.extendOwn(problem, problemDetail);
6674
return cb(null, problem);
6775
}
@@ -97,7 +105,7 @@ core.submitProblem = function(problem, cb) {
97105
};
98106

99107
core.updateProblem = function(problem, kv) {
100-
var problems = cache.get('all');
108+
var problems = cache.get(KEY_PROBLEMS);
101109
if (!problems) return false;
102110

103111
var oldProblem = _.find(problems, function(x) {
@@ -109,7 +117,7 @@ core.updateProblem = function(problem, kv) {
109117
_.extend(problem, kv);
110118

111119
var singleUpdated = saveProblem(problem);
112-
var allUpdated = cache.set('all', problems);
120+
var allUpdated = cache.set(KEY_PROBLEMS, problems);
113121
return singleUpdated && allUpdated;
114122
};
115123

@@ -176,14 +184,14 @@ core.login = function(user, cb) {
176184

177185
core.logout = function(purge) {
178186
var user = this.getUser();
179-
if (purge) cache.del('.user');
187+
if (purge) cache.del(KEY_USER);
180188
// NOTE: need invalidate any user related cache
181-
cache.del('all');
189+
cache.del(KEY_PROBLEMS);
182190
return user;
183191
};
184192

185193
core.getUser = function() {
186-
return cache.get('.user');
194+
return cache.get(KEY_USER);
187195
};
188196

189197
core.isLogin = function() {

lib/leetcode_client.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,16 @@ leetcodeClient.getProblems = function(category, user, cb) {
123123
})
124124
.map(function(p) {
125125
return {
126-
state: p.status || 'None',
127-
id: p.stat.question_id,
128-
name: p.stat.question__title,
129-
slug: p.stat.question__title_slug,
130-
link: config.URL_PROBLEM.replace('$slug', p.stat.question__title_slug),
131-
locked: p.paid_only,
132-
percent: p.stat.total_acs * 100 / p.stat.total_submitted,
133-
level: h.levelToName(p.difficulty.level),
134-
starred: p.is_favor
126+
state: p.status || 'None',
127+
id: p.stat.question_id,
128+
name: p.stat.question__title,
129+
slug: p.stat.question__title_slug,
130+
link: config.URL_PROBLEM.replace('$slug', p.stat.question__title_slug),
131+
locked: p.paid_only,
132+
percent: p.stat.total_acs * 100 / p.stat.total_submitted,
133+
level: h.levelToName(p.difficulty.level),
134+
starred: p.is_favor,
135+
category: json.category_slug
135136
};
136137
});
137138

test/test_core.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ describe('core', function() {
111111

112112
describe('#problems', function() {
113113
var PROBLEMS = [
114-
{id: 0, name: 'name0', slug: 'slug0', starred: false},
115-
{id: 1, name: 'name1', slug: 'slug1', starred: true}
114+
{id: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
115+
{id: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
116116
];
117117
var RESULTS = [
118118
{name: 'result0'},
@@ -121,7 +121,7 @@ describe('core', function() {
121121

122122
describe('#getProblems', function() {
123123
it('should getProblems w/ cache ok', function(done) {
124-
cache.set('all', PROBLEMS);
124+
cache.set('problems', PROBLEMS);
125125

126126
core.getProblems(function(e, problems) {
127127
assert.equal(e, null);
@@ -131,7 +131,7 @@ describe('core', function() {
131131
});
132132

133133
it('should getProblems w/o cache ok', function(done) {
134-
cache.del('all');
134+
cache.del('problems');
135135

136136
client.getProblems = function(category, user, cb) {
137137
return cb(null, PROBLEMS);
@@ -145,7 +145,7 @@ describe('core', function() {
145145
});
146146

147147
it('should getProblems w/o cache fail if client error', function(done) {
148-
cache.del('all');
148+
cache.del('problems');
149149

150150
client.getProblems = function(category, user, cb) {
151151
return cb('client getProblems error');
@@ -160,8 +160,8 @@ describe('core', function() {
160160

161161
describe('#getProblem', function() {
162162
it('should getProblem by id w/ cache ok', function(done) {
163-
cache.set('all', PROBLEMS);
164-
cache.set('slug0', PROBLEMS[0]);
163+
cache.set('problems', PROBLEMS);
164+
cache.set('0.slug0.algorithms', PROBLEMS[0]);
165165

166166
core.getProblem(0, function(e, problem) {
167167
assert.equal(e, null);
@@ -171,8 +171,8 @@ describe('core', function() {
171171
});
172172

173173
it('should getProblem by name w/ cache ok', function(done) {
174-
cache.set('all', PROBLEMS);
175-
cache.set('slug0', PROBLEMS[0]);
174+
cache.set('problems', PROBLEMS);
175+
cache.set('0.slug0.algorithms', PROBLEMS[0]);
176176

177177
core.getProblem('name0', function(e, problem) {
178178
assert.equal(e, null);
@@ -182,8 +182,8 @@ describe('core', function() {
182182
});
183183

184184
it('should getProblem by key w/ cache ok', function(done) {
185-
cache.set('all', PROBLEMS);
186-
cache.set('slug0', PROBLEMS[0]);
185+
cache.set('problems', PROBLEMS);
186+
cache.set('0.slug0.algorithms', PROBLEMS[0]);
187187

188188
core.getProblem('slug0', function(e, problem) {
189189
assert.equal(e, null);
@@ -193,8 +193,8 @@ describe('core', function() {
193193
});
194194

195195
it('should getProblem by id w/o cache ok', function(done) {
196-
cache.set('all', PROBLEMS);
197-
cache.del('slug0');
196+
cache.set('problems', PROBLEMS);
197+
cache.del('0.slug0.algorithms');
198198

199199
client.getProblem = function(user, problem, cb) {
200200
return cb(null, problem);
@@ -208,7 +208,7 @@ describe('core', function() {
208208
});
209209

210210
it('should getProblem error if not found', function(done) {
211-
cache.set('all', PROBLEMS);
211+
cache.set('problems', PROBLEMS);
212212

213213
core.getProblem(3, function(e, problem) {
214214
assert.equal(e, 'Problem not found!');
@@ -217,8 +217,8 @@ describe('core', function() {
217217
});
218218

219219
it('should getProblem fail if client error', function(done) {
220-
cache.set('all', PROBLEMS);
221-
cache.del('slug0');
220+
cache.set('problems', PROBLEMS);
221+
cache.del('0.slug0.algorithms');
222222

223223
client.getProblem = function(user, problem, cb) {
224224
return cb('client getProblem error');
@@ -231,7 +231,7 @@ describe('core', function() {
231231
});
232232

233233
it('should getProblem fail if getProblems error', function(done) {
234-
cache.del('all');
234+
cache.del('problems');
235235
client.getProblems = function(category, user, cb) {
236236
return cb('getProblems error');
237237
};
@@ -245,8 +245,8 @@ describe('core', function() {
245245

246246
describe('#updateProblem', function() {
247247
it('should updateProblem ok', function(done) {
248-
cache.set('all', PROBLEMS);
249-
cache.del('slug0');
248+
cache.set('problems', PROBLEMS);
249+
cache.del('0.slug0.algorithms');
250250

251251
var kv = {value: 'value00'};
252252
var ret = core.updateProblem(PROBLEMS[0], kv);
@@ -255,19 +255,19 @@ describe('core', function() {
255255
core.getProblem(0, function(e, problem) {
256256
assert.equal(e, null);
257257
assert.deepEqual(problem,
258-
{id: 0, name: 'name0', slug: 'slug0', value: 'value00', starred: false});
258+
{id: 0, name: 'name0', slug: 'slug0', value: 'value00', starred: false, category: 'algorithms'});
259259
done();
260260
});
261261
});
262262

263263
it('should updateProblem fail if no problems found', function() {
264-
cache.del('all');
264+
cache.del('problems');
265265
var ret = core.updateProblem(PROBLEMS[0], {});
266266
assert.equal(ret, false);
267267
});
268268

269269
it('should updateProblem fail if unknown problem', function() {
270-
cache.set('all', [PROBLEMS[1]]);
270+
cache.set('problems', [PROBLEMS[1]]);
271271
var ret = core.updateProblem(PROBLEMS[0], {});
272272
assert.equal(ret, false);
273273
});

0 commit comments

Comments
 (0)