DEV Community

Elio Struyf
Elio Struyf

Posted on

3 2

#DevHack: Caching data for your VSCode extension

For my Visual Studio Code extension to autocomplete the Microsoft Graph APIs, I wanted to improve the speed of the suggestions by implementing a cache. That way, you would not have to do the same API calls each time.

In-memory cache has its limits

Initially, I started with just an in-memory cache. An in-memory cache is easy to establish, but whenever you close your VSCode session. The cache will be gone. Next time, the cache has to be created all from scratch.

What are the options?

Luckily VSCode provides you a couple of options to cache data for your extension. There are two places where you can cache data:

  • workspaceState: When you want to cache data for the current workspace/project.
  • globalState: When you want to cache data independent of your current project.

Both of these states are a Memento object, which allows you to get and update a value.

Info: There is also a SecretStorage which can be used to set/retrieve/delete secrets.

Using the VSCode state

For my extension, I choose to use the globalState, as I want to cache the Microsoft Graph data independently from the current project. Using the workspaceState is similar to the globalState.

Start by creating the cache as follows:

export async function activate(context: vscode.ExtensionContext) {

  const defaultData: CacheObject = { v1: {}, beta: {} };
  const cacheName = `${EXTENSION_NAME}_cache`;
  const cache = context.globalState.get<CacheObject>(cacheName, defaultData);

  ...
}
Enter fullscreen mode Exit fullscreen mode

Once created, you can put data into the cache:

// Add API response to the right API version of the cache
cache[version][path] = apiData;
await this.context.globalState.update(cacheName, cache);
Enter fullscreen mode Exit fullscreen mode

When you added the data. You can start retrieving it as follows:

cache = context.globalState.get<CacheObject>(cacheName);
Enter fullscreen mode Exit fullscreen mode

Currently, you can only get and update data, you cannot clear/delete the cache. If you can override the cache with a default value.

const clear = () => {
  cache = {};
  await context.globalState.update(cacheName, {});
}
Enter fullscreen mode Exit fullscreen mode

Info: You can find a complete example of using the VSCode state here: CacheProvider.ts.

Article first publish on eliostruyf.com

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay