tu2-atmanand / Task-Board

An Obsidian plugin to view and manage all your task in a much more efficient Kanban Board format. Easily manage your tasks throught your vault.
https://tu2-atmanand.github.io/task-board-docs/
GNU General Public License v3.0
5 stars 0 forks source link

sessionStorage to load tasks.json in RAM #39

Closed tu2-atmanand closed 1 month ago

tu2-atmanand commented 1 month ago

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.

Recently i came to know about the plugin.settings method, where i can load the data from the disk whenever required, write it back whenever required using the inbuit functions. And access any values from RAM, all handled by Obsidian. So the sessionStorage is not required for data.json.

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.

tu2-atmanand commented 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:

How localStorage Works:

Where 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.

Performance Considerations:

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.

Storing Data in RAM (In-Memory Caching):

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:

tu2-atmanand commented 1 month ago

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:

Storing Data in 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();

Accessing Data in Other Files

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;
}

Usage in Components

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:

In any component or file:

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.');
}

Important Points:

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.

Key Points:

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!

tu2-atmanand commented 1 month ago

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.