当前位置:网站首页>Browser storage scheme
Browser storage scheme
2022-07-02 12:44:00 【There is no water in the sea】
One 、 Storage scheme I (localStorage/sessionStorage)
1、localStorage/sessionStorage

2、localStorage/sessionStorage Common methods
// 1、setItem
localStorage.setItem("name", "chen123");
localStorage.setItem("age", 13);
// 2、length
console.log(localStorage.length);
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
console.log(localStorage.getItem(key));
}
// 3、key Method
console.log(localStorage.key(0)); //age
// 4、getItem(key)
console.log(localStorage.getItem("age")); // 13
// 5、removeItem
localStorage.removeItem("age");
// 6、clear Method
localStorage.clear();
3、localStorage/sessionStorage encapsulation
class Cache {
constructor(isLocal = true) {
this.storage = isLocal ? localStorage : sessionStorage;
}
setCache(key, value) {
if (value) this.storage.setItem(key, JSON.stringify(value));
}
getCache(key) {
let value = this.storage.getItem(key);
if (value) value = JSON.parse(value);
}
removeItem(key) {
this.storage.removeItem(key);
}
clear() {
this.storage.clear();
}
key(index) {
return this.storage.key(index);
}
length() {
return this.storage.length;
}
}
const localCache = new Cache();
const sessionCache = new Cache(false);
export {
localCache, sessionCache };
Two 、 Storage scheme II (indexDB)

Understanding can
1、indexeDB Process of use
// 1、 Open database ( The process of establishing a connection )
const dbRequest = indexedDB.open("chen"); // Get an object , We can write some callback functions for listening
dbRequest.onerror = function (err) {
console.log("err Failed to open database ", err);
};
let db = null;
dbRequest.onsuccess = function (event) {
db = event.target.result;
};
// Open the database for the first time / Or the new version is updated
dbRequest.onupgradeneeded = function (event) {
const db = event.target.result;
// Create some storage objects , amount to mysql In the table , keypath: Primary key
db.createObjectStore("users", {
keyPath: "id" });
};
class User {
constructor(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
}
}
const users = [
new User(1, "zhangsan", 21),
new User(2, "lisi", 22),
new User(3, "wangwu", 34),
];
// obtain btns, Monitor click
const btns = document.querySelectorAll("button");
// console.log(btns);
btns.forEach((item, index) => {
console.log(item, index);
item.onclick = function () {
// Create new things every time
const transaction = db.transaction("users", "readwrite");
console.log(transaction);
// Because of the above transaction It can be transmitted to multiple ,db.transaction(["users", "students"], 'readwrite')
const store = transaction.objectStore("users");
switch (index) {
case 0:
console.log(" Click Add ");
for (const user of users) {
const request1 = store.add(user);
request1.onsuccess = function () {
console.log(`${
user.name} Insert the success `);
};
}
// The successful operation of things will call back oncomplete
transaction.oncomplete = function () {
console.log(" Add all successfully ");
};
break;
case 1:
console.log(" Click query ");
// 1. Query method 1 :( Know primary key , Query according to the primary key )
// const request = store.get(1);
// request.onsuccess = function(event){
// console.log(event.target.result);
// }
// 2、 Query method 2 :( Query all )
const request = store.openCursor();
request.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
console.log(cursor.key, cursor.value);
// Keep going
cursor.continue();
} else {
console.log(" Query complete ");
}
};
break;
case 2:
console.log(" Click delete ");
const request3 = store.openCursor();
request3.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
console.log(cursor.key, cursor.value);
// Keep going
if (cursor.key === 1) {
cursor.delete();
}
cursor.continue();
} else {
console.log(" Query complete ");
}
};
break;
case 3:
console.log(" Click Modify ");
}
};
});
边栏推荐
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
- Heap acwing 838 Heap sort
- 应用LNK306GN-TL 转换器、非隔离电源
- 分布式机器学习框架与高维实时推荐系统
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- bellman-ford AcWing 853. 有边数限制的最短路
- Use MySQL events to regularly perform post seven world line tasks
- VLAN experiment
- Enhance network security of kubernetes with cilium
- Dijkstra AcWing 850. Dijkstra求最短路 II
猜你喜欢

Is the neural network (pinn) with embedded physical knowledge a pit?
![2.6 using recursion and stack - [tower of Hanoi problem]](/img/fc/45038170dafd104691c93716b103cf.jpg)
2.6 using recursion and stack - [tower of Hanoi problem]

Writing method of then part in drools

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

spfa AcWing 851. SPFA finding the shortest path
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

堆 AcWing 838. 堆排序

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)

JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
随机推荐
JDBC prevent SQL injection problems and solutions [preparedstatement]
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
高性能纠删码编码
Adding database driver to sqoop of cdh6
arcgis js 4. Add pictures to x map
Go learning notes - multithreading
防抖 节流
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
VLAN experiment
Dijkstra AcWing 850. Dijkstra求最短路 II
Is the neural network (pinn) with embedded physical knowledge a pit?
Calculate the maximum path sum of binary tree
软件测试面试题-2022年大厂面试题合集
3 A VTT端接 稳压器 NCP51200MNTXG资料
Leetcode - Sword finger offer 51 Reverse pairs in an array
ArrayList与LinkedList效率的对比
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
上手报告|今天聊聊腾讯目前在用的微服务架构
IPhone 6 plus is listed in Apple's "retro products" list
Simple use of drools decision table