当前位置:网站首页>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 ");
}
};
});
边栏推荐
- js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
- spfa AcWing 851. SPFA finding the shortest path
- Floyd AcWing 854. Floyd求最短路
- 软件测试面试题-2022年大厂面试题合集
- [ybtoj advanced training guide] similar string [string] [simulation]
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Mui WebView down refresh pull-up load implementation
- Record the range of data that MySQL update will lock
- AI mid stage technology research
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
猜你喜欢
Adding database driver to sqoop of cdh6
哈希表 AcWing 841. 字符串哈希
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
Deep copy event bus
Hash table acwing 841 String hash
堆 AcWing 838. 堆排序
BOM DOM
async/await 异步函数
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
随机推荐
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
Drools dynamically add, modify, and delete rules
[ybtoj advanced training guidance] cross the river [BFS]
Openssh remote enumeration username vulnerability (cve-2018-15473)
Calculate the maximum path sum of binary tree
Mui WebView down refresh pull-up load implementation
Is the neural network (pinn) with embedded physical knowledge a pit?
绕过ObRegisterCallbacks需要驱动签名方法
线性DP AcWing 895. 最长上升子序列
深拷貝 事件總線
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Deep copy event bus
Simple understanding of ThreadLocal
BOM DOM
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
线性DP AcWing 897. 最长公共子序列
区间DP AcWing 282. 石子合并
C#运算符
架构师必须了解的 5 种最佳软件架构模式