Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/src/data-cache.ts
2498 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { injectable } from "inversify";
8
9
/**
10
* A cache that can be used to cache expensive operations, such as retrieving data from the database.
11
*/
12
export interface DataCache {
13
/**
14
* Retrieves the value for the given key from the cache. If the value is not in the cache, the provider is called and the result is stored in the cache.
15
* @param key the key to retrieve the value for. Should be segmented using `:`. For example: `user:123`
16
* @param provider the provider to call if the value is not in the cache
17
*/
18
get<T>(key: string, provider: () => Promise<T | undefined>): Promise<T | undefined>;
19
20
/**
21
* @param keyPattern the key pattern to invalidate. The `*` denotes a wildcard segment. For example: `user:*` invalidates all keys starting with `user:`
22
*/
23
invalidate(keyPattern: string): Promise<void>;
24
}
25
export const DataCache = Symbol("DataCache");
26
27
@injectable()
28
export class DataCacheNoop implements DataCache {
29
get<T>(key: string, provider: () => Promise<T | undefined>): Promise<T | undefined> {
30
return provider();
31
}
32
33
async invalidate(partialKey: string): Promise<void> {
34
// noop
35
}
36
}
37
38