当前位置:网站首页>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 ");
}
};
});
边栏推荐
- 上手报告|今天聊聊腾讯目前在用的微服务架构
- China traffic sign detection data set
- 架构师必须了解的 5 种最佳软件架构模式
- 8A 同步降压稳压器 TPS568230RJER_规格信息
- Adding database driver to sqoop of cdh6
- Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
- JSON serialization and parsing
- 浏览器node事件循环
- Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
猜你喜欢
MySQL and PostgreSQL methods to grab slow SQL
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
2.6 using recursion and stack - [tower of Hanoi problem]
arcgis js 4. Add pictures to x map
Embedded Software Engineer career planning
VLAN experiment
ArrayList与LinkedList效率的对比
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Is the neural network (pinn) with embedded physical knowledge a pit?
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
随机推荐
线性DP AcWing 902. 最短编辑距离
In development, why do you find someone who is paid more than you but doesn't write any code?
百款拿来就能用的网页特效,不来看看吗?
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
spfa AcWing 851. SPFA finding the shortest path
模块化 CommonJS ES Module
8A 同步降压稳压器 TPS568230RJER_规格信息
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Find the common ancestor of any two numbers in a binary tree
Introduction to CPU instruction set
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
计数类DP AcWing 900. 整数划分
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
China traffic sign detection data set
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
线性DP AcWing 897. 最长公共子序列
Input box assembly of the shutter package
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area