Skip to content

feat: add ability to change location of cache files - #34337

Closed
zcbenz wants to merge 12 commits into
mainfrom
where-is-my-data
Closed

zcbenz wants to merge 12 commits into
mainfrom
where-is-my-data

Conversation

@zcbenz

@zcbenz zcbenz commented May 24, 2022 •

Copy link
Copy Markdown
Contributor

Description of Change

Close #1404.
Close #8124.

This PR does following things:

  1. Add new aliases to system paths paths: cache, userCache, localUserData.
  2. Add a new sessionCache dir which can be used to change where HTTP disk cache and JavaScript code cache are stored.
  3. Add a tutorial on the types of paths and where should users store their data.

Note that we still have a few other kinds of cache left in userData like GPU cache and WebRTC logs, it is hard to find out how to change their locations all at once and I would like to do it in followup PRs.

Release Notes

Notes: Add cache/ userCache/localUserData/sessionCache paths to app.setPath which can be used to customize positions of various cache data.

@zcbenz zcbenz added semver/minor backwards-compatible functionality no-backport labels May 24, 2022
@zcbenz
zcbenz requested review from a team May 24, 2022 03:54

@erickzhao erickzhao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first pass at docs review! :)

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments in that really old issue still stand, without some kind of migration all we're doing is:

  • Putting apps into an unkown state
  • Massively duplicating content on disk
  • Potentially breaking existing backup / storage systems that depend on our existing paths

For very little gain (it's just "semantic correctness"). I like the idea of adding more path customization, I don't like changing the defaults

@erickzhao

Copy link
Copy Markdown
Member

For what it's worth, I believe the tutorial part of this PR could also be standalone. Giving users a blessed guide for data storage techniques is worthwhile even without the additional path customization provided in the feature code.

@zcbenz zcbenz changed the title feat: store http cache and code cache under cache dir feat: add ability to change location of cache files May 25, 2022
@zcbenz

zcbenz commented May 25, 2022

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound That is quite valid concern. I have changed this PR to provide an API to change the cache location, instead of changing the default location.

@zcbenz
zcbenz force-pushed the where-is-my-data branch from 1b32d54 to 4d3dc7d Compare May 25, 2022 11:15
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label May 31, 2022
@jkleinsc
jkleinsc requested a review from MarshallOfSound May 31, 2022 20:14
Comment thread shell/app/electron_main_delegate.cc Resolved
Comment thread docs/api/app.md
Comment on lines +625 to +637
* `userCache` The directory for storing your app's cache files, which
by default is the `cache` directory appended with your app's name. HTTP disk
cache and JavaScript code cache are written into this directory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The userCache name is a little unclear to me since cache is already per-user...?

I feel like cache and appCache might be a little clearer but if there are Good Reasons for using userCache I wouldn't block on this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name follows the name convention of appData/userData paths, which came from the DIR_APP_DATA/DIR_USER_DATA paths in Chromium, and which very likely came from the %APPDATA% env on Windows.

Comment thread docs/api/app.md Resolved
Comment thread docs/api/app.md Resolved
Comment thread docs/api/app.md Resolved
Comment on lines +45 to +48
* On Windows, there is no specialized location to store cache data, and most apps
write cache data to `%LOCALAPPDATA%`, so this path points to `localUserData` by
default. If your app is writing files to both `localUserData` and `userCache`,
you should consider changing the `userCache` path to a subdirectory of
`localUserData` to avoid conflicts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very good description of the nuance here 👍

@@ -0,0 +1,100 @@
# Where to Store App Data

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really love this doc

Comment on lines +4 to +9
let partition;
if (process.argv.length > 3) {
app.setPath(process.argv[2], process.argv[3]);
if (process.argv.length > 4) {
partition = 'persist:' + process.argv[4];
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting to a point where maybe we want to consider arg parsing in this main.js

@zcbenz zcbenz added semver/major incompatible API changes and removed semver/minor backwards-compatible functionality labels Sep 30, 2022
@zcbenz

zcbenz commented Nov 21, 2022

Copy link
Copy Markdown
Contributor Author

/cc @electron/wg-api for API review.

@Neustradamus

Copy link
Copy Markdown

There is a problem in several installers, there is no possibility to select the installation folder.
By default it is in %LOCALAPPDATA%, it must be replaced by %PROGRAMFILES%.

32-bit and 64-bit:

  • "Program Files" on 32-bit or 64-bit OS: %ProgramFiles%
  • "Program Files (x86)" for 32-bit applications on 64-bit OS: %ProgramFiles(x86)%

Important:

  • The user profile must be in %APPDATA%
  • The cache must be in %LOCALAPPDATA%

For example, Mozilla Firefox has:

  • Install folder: %PROGRAMFILES%\Mozilla Firefox
  • Profile folder: %APPDATA%\Mozilla\Firefox
  • Cache folder: %LOCALAPPDATA%\Mozilla\Firefox

More informations about folders in Microsoft Windows:

1/ %PROGRAMFILES% is a folder first found in Microsoft Windows 95 and included in all later versions of Microsoft Windows that stores the software programs installed on the computer.

2/ %USERNAME%\AppData

  • Local: This folder stores device-specific settings and temporary files. There is a separate %LOCALAPPDATA% environment variable for this folder that you can use in your batch files and scripts.
  • LocalLow: Here, Windows mainly stores buffer data generated by various applications (Internet Explorer, Java, Adobe, etc.). It is also used by low-level access systems, for example, for your browser's temporary files when working in Protected View. This directory also cannot be moved to another computer.
  • Roaming: You can transfer files from this folder to another computer. The Roaming folder stores browser data, bookmarks, etc. This directory allows users to always work with a familiar environment on any server in the Remote Desktop Services farm when using roaming profiles or user profile disks. The environment variable for this directory is %APPDATA%.

Thanks in advance.

@Zocker1999NET Zocker1999NET left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m glad to see someone tries to help Electron apps to store their data following approved standards. Thanks for tackling that.

However, while reading through your documentation, I discovered you seem to consider to use CONFIG (shorthand for $XDG_*_HOME) to use for localUserData, where “user data [can take] lots of disk space”. This would manifest issues like #8124, not with cache but with important binary data (like images). I created a comment at those lines with the reasoning.

As a rule of thumb, if some user data takes lots of disk space, it should be written
to `localUserData`. Otherwise, it should instead be written to `userData`.

On Linux and macOS, `localUserData` has no distinctive purpose and is just an alias

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following XDG Base Directory Spec, this should be DATA, maybe STATE, but definitly not CONFIG.

  • $XDG_DATA_HOME is for generic user data, without further specification. Rather than CONFIG, DATA should be bloated with important, but big amounts of data. Some users still synchronize them, but then they know what they are doing.
  • $XDG_CONFIG_HOME is for configuration files, which users may sync between their computers. some even consider CONFIG to be exclusively for text files (so it can be easily included in dotfiles repositories). So this directory should not be bloated with big files on Linux, similar to %APPDATA% on Windows.
  • $XDG_STATE_HOME should contain “data that should persist between (application) restarts, but that is not important or portable enough to the user that is should be stored in $XDG_DATA_HOME”. But mind, STATE is considered similar to CACHE, these are not completely redundant, but it may only a minor inconvenience should these get lost.
Size Comparisons

PS: To add examples from the practice:

$ du -sh $XDG_DATA_HOME $XDG_CONFIG_HOME $XDG_STATE_HOME
46G     /home/zocker/.local/share  # DATA
4,5G    /home/zocker/.config       # CONFIG
64K     /home/zocker/.local/state  # STATE

But my CONFIG just so big because of (Electron) apps not respecting XDG:

$ du -sh .config/* | sort -rh | head -n 6
3,0G    .config/Code         # electron
553M    .config/discord      # electron
207M    .config/chromium     # parent of electron
158M    .config/SchildiChat  # electron
157M    .config/unity3d      # first non-electron
151M    .config/Element      # electron

Excluding those biggest 6 outliers (and IMHO there are more) my CONFIG dir would just use 310 MiB. So real thanks for fixing Electron apps ❤️.

@heroin-moose

Copy link
Copy Markdown

This will create more fragmentation. Some apps will support proper layout, some wont. Electron really should fix bad defaults instead.

@DC7IA

DC7IA commented Sep 8, 2023

Copy link
Copy Markdown

Thanks for working on this!

@erickzhao erickzhao self-assigned this Jul 22, 2024
@samuelmaddock

Copy link
Copy Markdown
Member

The current understanding is that the changes to app.getPath('cache') are breaking changes on Windows. This should default to the existing behavior for now. As a followup, a migration path could be provided to avoid creating the breaking change.

Also, I'm tagging this PR as wip since it's not actively being worked on. If anyone is interested in taking on this work, please let @electron/api-wg know 🙇‍♂️

@Neustradamus

Copy link
Copy Markdown

@zcbenz: Why have you closed your PR?

@zcbenz
zcbenz deleted the where-is-my-data branch August 5, 2024 10:29
@zcbenz

zcbenz commented Aug 5, 2024

Copy link
Copy Markdown
Contributor Author

I'm tired of pushing this.

@codebytere

Copy link
Copy Markdown
Member

@zcbenz would you be ok with someone else taking over the feature?

@zcbenz

zcbenz commented Aug 5, 2024

Copy link
Copy Markdown
Contributor Author

Of course not, please feel free continuing the work.

@Damglador Damglador mentioned this pull request Jul 19, 2026
4 of 8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache data is written to userData dir instead of cache dir Cache directory on windows should be %localappdata% by default