Skip to content

Commit 7124c85

Browse files
committed
Add params to cache key
Fix #85
1 parent 94aa6f8 commit 7124c85

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/api.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class Api {
3737
paramsData[key] = value;
3838
});
3939

40-
const response = await this._request(method, path, paramsData, headers, body);
40+
const response = this._request(method, path, paramsData, headers, body);
4141

4242
return (await response.toPromise()).data;
4343
}
@@ -70,23 +70,28 @@ export default class Api {
7070
return await this.get(method, path, params, headers, body);
7171
}
7272

73-
const rawUrl = this.baseUrl + path;
73+
let cacheKey = this.baseUrl + path;
7474

75-
const force = this.lastCacheDuration !== cacheDurationSeconds;
75+
if (params && Object.keys(params).length > 0) {
76+
cacheKey =
77+
cacheKey +
78+
(cacheKey.search(/\?/) >= 0 ? '&' : '?') +
79+
params.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
80+
}
7681

77-
if (force) {
78-
this.cache.del(rawUrl);
82+
if (this.lastCacheDuration !== cacheDurationSeconds) {
83+
this.cache.del(cacheKey);
7984
}
85+
this.lastCacheDuration = cacheDurationSeconds;
8086

81-
const cachedItem = this.cache.get(rawUrl);
82-
if (cachedItem && !force) {
87+
const cachedItem = this.cache.get(cacheKey);
88+
if (cachedItem) {
8389
return Promise.resolve(cachedItem);
8490
}
85-
this.lastCacheDuration = cacheDurationSeconds;
8691

8792
const result = await this.get(method, path, params, headers, body);
8893

89-
this.cache.put(rawUrl, result, cacheDurationSeconds * 1000);
94+
this.cache.put(cacheKey, result, cacheDurationSeconds * 1000);
9095

9196
return result;
9297
}

0 commit comments

Comments
 (0)