forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99a52e9
commit 64054cb
Showing
9 changed files
with
442 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Gitpod. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
const CACHE_KEY = 'gitpod.cache'; | ||
|
||
interface CacheObject { | ||
value: any; | ||
expiration?: number; | ||
} | ||
|
||
interface CacheMap { [key: string]: CacheObject } | ||
|
||
export class CacheHelper { | ||
constructor(private readonly context: vscode.ExtensionContext) { } | ||
|
||
set(key: string, value: any, expiration?: number) { | ||
let obj = this.context.globalState.get<CacheMap>(CACHE_KEY); | ||
if (!obj) { | ||
obj = {}; | ||
} | ||
const exp = expiration ? (Date.now() / 1000 + expiration) : undefined; | ||
obj[key] = { value, expiration: exp }; | ||
return this.context.globalState.update(CACHE_KEY, obj); | ||
} | ||
|
||
get<T>(key: string): T | undefined { | ||
const value = this.context.globalState.get<CacheMap>(CACHE_KEY); | ||
if (!value || !value[key]) { | ||
return undefined; | ||
} | ||
const data = value[key]; | ||
if (!data.expiration) { | ||
return data.value; | ||
} | ||
const now = Date.now() / 1000; | ||
return now > data.expiration ? undefined : data.value; | ||
} | ||
|
||
async getOrRefresh<T>(key: string, refreshCallback: () => Thenable<{ value: T; ttl?: number }>): Promise<T> { | ||
let value = this.get<T>(key); | ||
if (value === undefined) { | ||
const result = await refreshCallback(); | ||
await this.set(key, result.value, result.ttl); | ||
value = result.value; | ||
} | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.