Closed tu2-atmanand closed 1 month ago
For this the first optoin i had was locaStorage, but i found out that localStorag uses database and hence it is also stored on Disk, althoug much faster than Disk read/write operation, but still Disk is getting involved here. As explaned below :
In Electron, localStorage
works similarly to how it does in regular web applications. Here's a breakdown of how it functions and how it applies to your case:
localStorage
Works:Persistent Data Storage: localStorage
stores key-value pairs as strings and persists across browser sessions. In Electron, localStorage
is tied to the BrowserWindow context, meaning that it will persist across multiple launches of your application.
Storage Location: localStorage
is stored on disk, not in memory (RAM). The data is typically stored in a SQLite database or in files specific to the browser (or Electron in this case), usually in the user data directory.
Storage Size Limit: There's a size limitation (about 5-10MB depending on the platform/browser). For larger data, using a database or the filesystem might be better.
localStorage
Data Gets Stored:When you use setItem
in localStorage
, the data is written to disk in the local storage area allocated by Electron (which, in turn, uses Chromium's implementation). The data persists even if the app is closed and reopened, as long as it isn't explicitly cleared.
Because localStorage
stores data on disk, while it avoids frequent JSON file reads, it's not as fast as RAM-based storage. Each access to localStorage
still involves reading from disk, albeit a more optimized disk location.
If you want to store your data in RAM for even faster access while your Electron app is running (without writing to disk every time), you could use in-memory storage. Here's how:
Using an In-Memory Object: You can create an object in your Electron main or renderer process that holds your data. This object will reside in RAM as long as the application is active. For example:
let appSettings = {}; // In-memory cache
function loadSettings() {
// Load from JSON on startup and store in appSettings
const data = fs.readFileSync('data.json', 'utf-8');
appSettings = JSON.parse(data);
}
function getSetting(key) {
return appSettings[key];
}
function setSetting(key, value) {
appSettings[key] = value;
// Optionally, save back to disk later
}
loadSettings(); // Call this during app initialization
Benefit: Accessing data from this object will be faster than reading from localStorage
because the object is in RAM, and there are no disk I/O operations involved.
Downside: The data is lost once the application is closed, so you'll need to ensure that any critical data is written back to disk when necessary.
Later I found there is something called as sessionStorage in electron framework or React.js. As explained below :
sessionStorage
is similar to localStorage
, but it is designed to persist data only for the duration of a page session (or in your case, the session of your Electron application). Once the application or window is closed, the data stored in sessionStorage
is cleared.
Here’s how you can work with sessionStorage
in your Electron app across different files:
sessionStorage
When your Electron app loads (for example, in main.ts
or a file loaded during boot), you can load your data.json
into sessionStorage
. Here's an example of how to do it:
main.ts
(or your initialization file):import * as fs from 'fs';
// Function to load data into sessionStorage
function loadSettingsIntoSessionStorage() {
const data = fs.readFileSync('path_to/data.json', 'utf8');
const settings = JSON.parse(data);
// Save the settings into sessionStorage
sessionStorage.setItem('appSettings', JSON.stringify(settings));
}
// Call the function when the application loads
loadSettingsIntoSessionStorage();
To access the sessionStorage
data in another file within your utils/
folder or other components, you can use the getItem()
method to retrieve the stored data. Here's how you can do that:
utils/yourFile.ts
:export function getSettingsFromSessionStorage() {
const settings = sessionStorage.getItem('appSettings');
return settings ? JSON.parse(settings) : null;
}
You can now call the function from your utils file in your components or other parts of your app to get the settings stored in sessionStorage
:
import { getSettingsFromSessionStorage } from './utils/yourFile';
// Access settings
const settings = getSettingsFromSessionStorage();
if (settings) {
console.log('Settings loaded from sessionStorage:', settings);
} else {
console.error('No settings found in sessionStorage.');
}
sessionStorage
stores data only for the duration of the session, and it's cleared when the window or app is closed.sessionStorage
is scoped to the current window or process, so you cannot share sessionStorage
between multiple windows (unlike some other storage solutions).When you load data into sessionStorage
, the data is stored in RAM (volatile memory) rather than on the disk. This means that the data will only persist for the lifetime of the application session or the browser window (in a web context). Once the application is closed or the session ends, the data is cleared from sessionStorage
and cannot be recovered unless it is reloaded from another source, such as a file or API.
sessionStorage
is stored in RAM, not on the disk.sessionStorage
is deleted.Since sessionStorage
is stored in RAM, it's very fast for read/write operations, but it's also volatile, meaning it will be lost when the app closes or reloads. If you want the data to persist beyond a session (e.g., across app restarts), you’d need to use a more persistent storage solution like localStorage
(which is saved to disk) or a database.
Let me know if you'd like further details!
This feature has been successfully implemented.
All the functionalities with this new changes seems to be working. Closing this issue, for any new problem you will get, create a issue for that.
I want to optimize my code in terms of reducing read/write operation for plugin settings values. So the idea is load the data from data.json when the plugin loads in RAM and use this data from RAM instead of reading it everytime from Disk.
The most important optimization I can do is to reduce read-write operations for tasks.json, because in realTimeScanning, this is the file modified a lot.