Skip to content

Commit

Permalink
feat: enhance cache for #1103 (#1189)
Browse files Browse the repository at this point in the history
* feat: #1103

* lint: lint
  • Loading branch information
stone-jin authored Jul 30, 2021
1 parent 7a675eb commit 562236c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/cache/src/service/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class CacheManager {
}

// 获取key
async get(key: string) {
async get<T>(key: string): Promise<T> {
return new Promise((resolve, reject) => {
this.cache.get(key, (err, result) => {
this.cache.get<T>(key, (err, result) => {
if (err) {
reject(err);
return;
Expand All @@ -31,7 +31,11 @@ export class CacheManager {
}

// 设置cache
async set(key: string, value: string, options?: cacheManager.CachingConfig) {
async set<T>(
key: string,
value: T,
options?: cacheManager.CachingConfig
): Promise<T> {
return await this.cache.set(key, value, options);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cache/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe(`test.cache`, ()=>{
assert((await userService.getUser(`name`)) === undefined)
await userService.setUser('name', 'stone-jin')
assert((await userService.getUser(`name`)) === 'stone-jin')
await userService.setUser('name', {name: '123'});
assert(JSON.stringify(await userService.getUser('name'))===JSON.stringify({name: '123'}))
await userService.reset();
assert((await userService.getUser(`name`)) === undefined)
})
Expand Down

0 comments on commit 562236c

Please sign in to comment.