❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Maya : 5mb Storage on browser

17 August 2024 at 16:45

Maya, a developer working on a new website, was frustrated with slow load times caused by using cookies to store user data. One night, she discovered the Web Storage API, specifically Local Storage, which allowed her to store data directly in the browser without needing constant communication with the server.

She starts to explore,

The localStorage is property of the window (browser window object) interface allows you to access a storage object for the Document’s origin; the stored data is saved across browser sessions.

  1. Data is kept for a longtime in local storage (with no expiration date.). This could be one day, one week, or even one year as per the developer preference ( Data in local storage maintained even if the browser is closed).
  2. Local storage only stores strings. So, if you intend to store objects, lists or arrays, you must convert them into a string using JSON.stringfy()
  3. Local storage will be available via the window.localstorage property.
  4. What’s interesting about them is that the data survives a page refresh (for sessionStorage) and even a full browser restart (for localStorage).

Excited with characteristics, she decides to learn more on the localStorage methods,

setItem(key, value)

Functionality:

  • Add key and value to localStorage.
  • setItem returns undefined.
  • Every key, value pair stored is converted to a string. So it’s better to convert an object to string using JSON.stringify()

Examples:

  • For a simple key, value strings.
// setItem normal strings
window.localStorage.setItem("name", "maya");
  • Inorder to store objects, we need to convert them to JSON strings using JSON.stringify()
// Storing an Object without JSON stringify
const data = {
  "commodity":"apple",
  "price":43
};
window.localStorage.setItem('commodity', data);
var result = window.localStorage.getItem('commodity');
console.log("Retrived data without jsonified, "+ result);

getItem(key)

Functionality: This is how you get items from localStorage. If the given key is not present then it will return null.

Examples
Get a simple key value pair

// setItem normal strings
window.localStorage.setItem("name", "goku");

// getItem 
const name = window.localStorage.getItem("name");
console.log("name from localstorage, "+name);

removeItem(key)

Functionality: Remove an item by key from localStorage. If the given key is not present then it wont do anything.

Examples:

  • After removing the value will be null.
// remove item 
window.localStorage.removeItem("commodity");
var result = window.localStorage.getItem('commodity');
console.log("Data after removing the key "+ result);

clear()

Functionality: Clears the entire localStorage
Examples:

  • Simple clear of localStorage
// clear
window.localStorage.clear();
console.log("length of local storage - after clear " + window.localStorage.length);

length

Functionality: We can use this like the property access. It returns number of items stored.
**Examples: **

//length
console.log("length of local storage " + window.localStorage.length);

When to use Local Storage

  1. Data stored in Local Storage can be easily accessed by third party individuals.
  2. So its important to know that any sensitive data must not sorted in Local Storage.
  3. Local Storage can help in storing temporary data before it is pushed to the server.
  4. Always clear local storage once the operation is completed.

localStorage browser support

localStorage as a type of web storage is an HTML5 specification. It is supported by major browsers including IE8. To be sure the browser supports localStorage, you can check using the following snippet:

if (typeof(Storage) !== "undefined") {
    // Code for localStorage
    } else {
    // No web storage Support.
}

Maya is also curious to know where it gets stored ?

Where the local storage is saved ?

Windows

  • **Firefox: **C:\Users\\AppData\Roaming\Mozilla\Firefox\Profiles\\webappsstore.sqlite, %APPDATA%\Mozilla\Firefox\Profiles\\webappsstore.sqlite
  • **Chrome: **%LocalAppData%\Google\Chrome\User Data\Default\Local Storage\

Linux

  • Firefox: ~/.mozilla/firefox//webappsstore.sqlite
  • Chrome: ~/.config/google-chrome/Default/Local Storage/

Mac

  • Firefox: ~/Library/Application Support/Firefox/Profiles//webappsstore.sqlite, ~/Library/Mozilla/Firefox/Profiles//webappsstore.sqlite
  • Chrome: ~/Library/Application Support/Google/Chrome//Local Storage/, ~/Library/Application Support/Google/Chrome/Default/Local Storage/

Simple Hands-On

https://syedjafer.github.io/localStrorage/index.html

Limitations:

  1. Do not store sensitive user information in localStorage
  2. It is not a substitute for a server based database as information is only stored on the browser
  3. localStorage is limited to 5MB across all major browsers
  4. localStorage is quite insecure as it has no form of data protection and can be accessed by any code on your web page
  5. localStorage is synchronous, meaning each operation called would only execute one after the other

Now, Maya is able to resolve her quest of making her website faster, with the awareness of limitations !!!

Linux Partition: Static Partition Scaling without any data loss

1 January 2023 at 05:18

In this blog, we are going to see how to increase or decrease the size of the static partition in Linux without compromising any data loss and how to do that in Online mode without unmounting.

I already explained the basic concepts of partition in very detail in my previous blog. You can refer to that blog by clicking here.

In this practical, the Oracle VirtualBox is used for hosting the Redhat Enterprise Linux (RHEL8) Virtual Machine (VM).

The first step is to attach one hard disk. So, I attached one virtual hard disk with the size of 40GiB. That disk is named β€œ/dev/sdc”. You can check the disk name and all other disks present in your VM by running the following command.

fdisk -l

Then, we have to do partition by using β€œfdisk” command.

fdisk /dev/sdc

Then, enter β€œn” in order to create a new partition. Then enter the partition number and specify the number of sectors or GiB. Here, we entered 20 GiB in order to utilize that much storage unless we do partition, we can’t utilize any storage.

We had created one partition named as β€œ/dev/sdc1”. Next step is to format the partition. Here, we used Ext4 filesystem(format) to create an inode table.

Next step is to create one directory using β€œmkdir” command and mount that partition in that directory since we can’t directly use the hardware device no matter it is either real or virtual.

One file should be created inside that directory in order to check the data loss after Live scaling of static partition.

Ok, now the size of the static partition is 20 GiB, we are going to do scaling up to 30GiB without unmounting the partition. For this, again we have to run the following command.

fdisk /dev/sdc

Then delete the partition. Don’t bother about the data, it won’t lose.

Then enter β€œn” to create the new partition and specify your desired size. Here, I like to scale up to 30GiB. And then one warning will come and it says that β€œPartition 1 contains an ext4 signature” and ask us what to do with that either remove the signature or retain.

If you don’t want to lose the data, then enter β€œN”. Then enter β€œw” to save the partition. you can verify your partition size by running β€œfdisk -l” command in terminal. Finally, you increased the size of static partition.

First part is done. Then next step is to format the partition in order to create the file system. But this time, we will not use β€œmkfs” command, since it will delete all the data. We don’t need it. We have to do format without comprising the data. For that we have to run the following command.

resize2fs  /dev/sdc1

Finally, we done format without comprising the data. We can check this by going inside that mount point and check whether the data is here or not.

Yes, data is here. It is not lost even though we created new partition and formatted the partition.

Live Linux Static Partition scaling without any dataΒ loss

In this blog, we are going to see how to increase or decrease the size of the static partition in Linux without compromising any data loss and done in Online mode.

I already explained the basic concepts of partition in very detail in my previous blog. You can refer to that blog by clicking here.

In this practical, the Oracle VirtualBox is used for hosting the Redhat Enterprise Linux (RHEL8) Virtual Machine (VM).

The first step is to attach one hard disk. So, I attached one virtual hard disk with the size of 40GiB. That disk is named β€œ/dev/sdc”. You can check the disk name and all other disks present in your VM by running the following command.

fdisk -l

Then, we have to do partition by using β€œfdisk” command.

fdisk /dev/sdc

Then, enter β€œn” in order to create a new partition. Then enter the partition number and specify the number of sectors or GiB. Here, we entered 20 GiB in order to utilize that much storage unless we do partition, we can’t utilize any storage.

We had created one partition named as β€œ/dev/sdc1”. Next step is to format the partition. Here, we used Ext4 filesystem(format) to create an inode table.

Next step is to create one directory using β€œmkdir” command and mount that partition in that directory since we can’t directly use the hardware device no matter it is either real or virtual.

One file should be created inside that directory in order to check the data loss after Live scaling of static partition.

Ok, now the size of the static partition is 20 GiB, we are going to do scaling up to 30GiB without unmounting the partition. For this, again we have to run the following command.

fdisk /dev/sdc

Then delete the partition. Don’t bother about the data, it won’t lose.

Then enter β€œn” to create the new partition and specify your desired size. Here, I like to scale up to 30GiB. And then one warning will come and it says that β€œPartition 1 contains an ext4 signature” and ask us what to do with that either remove the signature or retain.

If you don’t want to lose the data, then enter β€œN”. Then enter β€œw” to save the partition. you can verify your partition size by running β€œfdisk -l” command in terminal. Finally, you increased the size of static partition.

First part is done. Then next step is to format the partition in order to create the file system. But this time, we will not use β€œmkfs” command, since it will delete all the data. We don’t need it. We have to do format without comprising the data. For that we have to run the following command.

resize2fs  /dev/sdc1

Finally, we done format without comprising the data. We can check this by going inside that mount point and check whether the data is here or not.

Yes, data is here. It is not lost even though we created new partition and formatted the partition.

Reduce the size of the Static Partition

You can also reduce the Static Partition size. For this, you have to follow the below steps.

  • Unmount
  • Cleaning bad sectors
  • Format
  • Mount

First step is to unmount your mount point since it is online, somebody will using it.

umount /partition1

Then we have to clean the bad sectors by running the following command

e2fsck -f /dev/sdc1

Then we have to format the size you want. Here we want only 20 GiB and we will reduce the remaining 10 GiB space. This is done by running following command.

resize2fs /dev/sdc1 20G

Then we have to mount the partition.

Finally, we reduced the static partition size.

Above figure shows that Data is also not lost during scaling down.


Thank you all for your reads. Stay tuned for my next article, because it is Endless.

❌
❌