我想在Electron(atom-shell)中清除缓存数据。我没有找到类似gui.App.clearCache()(用于清除缓存数据的node-webkit API)的任何API。如果您找到了任何API或其他方法,请告诉我。评论是受欢迎的。
我想在Electron(atom-shell)中清除缓存数据。我没有找到类似gui.App.clearCache()(用于清除缓存数据的node-webkit API)的任何API。如果您找到了任何API或其他方法,请告诉我。评论是受欢迎的。
Electron将其缓存存储在以下文件夹中:
Windows:
C:\Users\<user>\AppData\Roaming\<yourAppName>\Cache
Linux:
/home/<user>/.config/<yourAppName>/Cache
OS X:
/Users/<user>/Library/Application Support/<yourAppName>/Cache
因此,删除这些文件夹也可以帮助您。 当然,这只是一次性解决方案;-)
const process = require('process');
process.env.APPDATA + "\\"+ app.getName() + "\\Cache";
获取缓存路径。我不确定它是否适用于其他平台。 - Nuryagdy Mustapayevvar remote = require('remote');
var win = remote.getCurrentWindow();
win.webContents.session.clearCache(function(){
//some callback.
});
如果您想清除先前登录会话的任何残留,最好使用以下方法:
loginWindow.webContents.session.clearStorageData()
const { app, session } = require('electron');
// ...
session.defaultSession.clearStorageData(null, (error: any) => {
// in our case we need to restart the application
// app.relaunch();
// app.exit();
});
Electron 7 更新:
await session.defaultSession.clearStorageData();
await
也能正常工作。 - openwonk我在Windows系统上尝试了@thegnuu提供的答案和session.defaultSession.clearCache();
,使用的是electron v10.1.5。
选项1:
直接删除缓存路径C:\Users\<username>\AppData\Roaming\<appname>\Cache
:
_deleteFolder(dirPath) {
const fs = require('fs');
// delete directory recursively
try {
fs.rmdirSync(dirPath, {recursive: true});
this._logger.info(`cache clean: ${dirPath} is deleted!`);
} catch (e) {
this._logger.error(`cache clean: could not delete ${dirPath}!`, e);
}
}
选项2:也清除相同的C:\Users\<username>\AppData\Roaming\<appname>\Cache
目录。
const {session} = require('electron');
session.defaultSession.clearCache();
选项1的问题:
this._logger.info(`cache clean: ${dirPath} is deleted!`);
获取了日志,但缓存目录并未被删除。它里面还有5个文件。fs.rmdir
,得到了相同的结果。在选项2上,我没有遇到任何问题。我想这是最好的选择。
奖励内容:session.defaultSession.clearStorageData();
会清除C:\Users\<username>\AppData\Roaming\<app name>\Local Storage
目录
答案:
var remote = require('remote');
var win = remote.getCurrentWindow();
win.WebContents.session.cookies.get(details, callback) // getting cookies
win.WebContents.session.cookies.remove(details, callback) //deleting cookies
更多信息请参考:http://electron.atom.io/docs/v0.29.0/api/browser-window/
该链接提供了有关Electron中BrowserWindow API的详细信息。在开发过程中,进入开发者工具,转到应用程序选项卡,然后清除存储和站点数据。
mainWindow.webContents.clearHistory();
或者删除应用程序缓存文件夹中的内容(在应用程序运行时会重新创建)。你可以使用 app.getPath('userData') + '/Cache'
获取路径。