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.
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).
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()
Local storage will be available via the window.localstorage property.
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
Data stored in Local Storage can be easily accessed by third party individuals.
So its important to know that any sensitive data must not sorted in Local Storage.
Local Storage can help in storing temporary data before it is pushed to the server.
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 ?
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.