Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/configuration/common/configurationCache.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IConfigurationCache, ConfigurationKey } from './configuration.js';
7
import { URI } from '../../../../base/common/uri.js';
8
import { FileOperationError, FileOperationResult, IFileService } from '../../../../platform/files/common/files.js';
9
import { joinPath } from '../../../../base/common/resources.js';
10
import { VSBuffer } from '../../../../base/common/buffer.js';
11
import { Queue } from '../../../../base/common/async.js';
12
import { IEnvironmentService } from '../../../../platform/environment/common/environment.js';
13
14
export class ConfigurationCache implements IConfigurationCache {
15
16
private readonly cacheHome: URI;
17
private readonly cachedConfigurations: Map<string, CachedConfiguration> = new Map<string, CachedConfiguration>();
18
19
constructor(
20
private readonly donotCacheResourcesWithSchemes: string[],
21
environmentService: IEnvironmentService,
22
private readonly fileService: IFileService
23
) {
24
this.cacheHome = environmentService.cacheHome;
25
}
26
27
needsCaching(resource: URI): boolean {
28
// Cache all non native resources
29
return !this.donotCacheResourcesWithSchemes.includes(resource.scheme);
30
}
31
32
read(key: ConfigurationKey): Promise<string> {
33
return this.getCachedConfiguration(key).read();
34
}
35
36
write(key: ConfigurationKey, content: string): Promise<void> {
37
return this.getCachedConfiguration(key).save(content);
38
}
39
40
remove(key: ConfigurationKey): Promise<void> {
41
return this.getCachedConfiguration(key).remove();
42
}
43
44
private getCachedConfiguration({ type, key }: ConfigurationKey): CachedConfiguration {
45
const k = `${type}:${key}`;
46
let cachedConfiguration = this.cachedConfigurations.get(k);
47
if (!cachedConfiguration) {
48
cachedConfiguration = new CachedConfiguration({ type, key }, this.cacheHome, this.fileService);
49
this.cachedConfigurations.set(k, cachedConfiguration);
50
}
51
return cachedConfiguration;
52
}
53
}
54
55
class CachedConfiguration {
56
57
private queue: Queue<void>;
58
private cachedConfigurationFolderResource: URI;
59
private cachedConfigurationFileResource: URI;
60
61
constructor(
62
{ type, key }: ConfigurationKey,
63
cacheHome: URI,
64
private readonly fileService: IFileService
65
) {
66
this.cachedConfigurationFolderResource = joinPath(cacheHome, 'CachedConfigurations', type, key);
67
this.cachedConfigurationFileResource = joinPath(this.cachedConfigurationFolderResource, type === 'workspaces' ? 'workspace.json' : 'configuration.json');
68
this.queue = new Queue<void>();
69
}
70
71
async read(): Promise<string> {
72
try {
73
const content = await this.fileService.readFile(this.cachedConfigurationFileResource);
74
return content.value.toString();
75
} catch (e) {
76
return '';
77
}
78
}
79
80
async save(content: string): Promise<void> {
81
const created = await this.createCachedFolder();
82
if (created) {
83
await this.queue.queue(async () => {
84
await this.fileService.writeFile(this.cachedConfigurationFileResource, VSBuffer.fromString(content));
85
});
86
}
87
}
88
89
async remove(): Promise<void> {
90
try {
91
await this.queue.queue(() => this.fileService.del(this.cachedConfigurationFolderResource, { recursive: true, useTrash: false }));
92
} catch (error) {
93
if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
94
throw error;
95
}
96
}
97
}
98
99
private async createCachedFolder(): Promise<boolean> {
100
if (await this.fileService.exists(this.cachedConfigurationFolderResource)) {
101
return true;
102
}
103
try {
104
await this.fileService.createFolder(this.cachedConfigurationFolderResource);
105
return true;
106
} catch (error) {
107
return false;
108
}
109
}
110
}
111
112
113