当前位置:网站首页>浏览器存储方案
浏览器存储方案
2022-07-02 09:43:00 【大海里没有水】
一、存储方案一(localStorage/sessionStorage)
1、localStorage/sessionStorage

2、localStorage/sessionStorage 常用方法
// 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方法
console.log(localStorage.key(0)); //age
// 4、getItem(key)
console.log(localStorage.getItem("age")); // 13
// 5、removeItem
localStorage.removeItem("age");
// 6、clear方法
localStorage.clear();
3、localStorage/sessionStorage封装
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 };
二、存储方案二(indexDB)

了解即可
1、indexeDB的使用过程
// 1、打开数据库(建立连接的过程)
const dbRequest = indexedDB.open("chen"); // 拿到一个对象,我们可以写一些监听的回调函数
dbRequest.onerror = function (err) {
console.log("err打开数据库失败", err);
};
let db = null;
dbRequest.onsuccess = function (event) {
db = event.target.result;
};
// 第一次打开数据库/或者新版本发生更新
dbRequest.onupgradeneeded = function (event) {
const db = event.target.result;
// 创建一些存储对象,相当于mysql中的表, keypath:主键
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),
];
// 获取btns,监听点击
const btns = document.querySelectorAll("button");
// console.log(btns);
btns.forEach((item, index) => {
console.log(item, index);
item.onclick = function () {
// 每次都创建新的事物
const transaction = db.transaction("users", "readwrite");
console.log(transaction);
// 因为上面的transaction是可以传多个的,db.transaction(["users", "students"], 'readwrite')
const store = transaction.objectStore("users");
switch (index) {
case 0:
console.log("点击了新增");
for (const user of users) {
const request1 = store.add(user);
request1.onsuccess = function () {
console.log(`${
user.name}插入成功`);
};
}
// 事物操作成功会回调oncomplete
transaction.oncomplete = function () {
console.log("添加全部成功");
};
break;
case 1:
console.log("点击了查询");
// 1.查询方式一:(知道主键,根据主键查询)
// const request = store.get(1);
// request.onsuccess = function(event){
// console.log(event.target.result);
// }
// 2、查询方式二:(查询所有)
const request = store.openCursor();
request.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
console.log(cursor.key, cursor.value);
// 继续往下
cursor.continue();
} else {
console.log("查询完成");
}
};
break;
case 2:
console.log("点击了删除");
const request3 = store.openCursor();
request3.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
console.log(cursor.key, cursor.value);
// 继续往下
if (cursor.key === 1) {
cursor.delete();
}
cursor.continue();
} else {
console.log("查询完成");
}
};
break;
case 3:
console.log("点击了修改");
}
};
});
边栏推荐
- [I'm a mound pytorch tutorial] learning notes
- Leetcode122 the best time to buy and sell stocks II
- 【C语言】十进制数转换成二进制数
- 字符串回文hash 模板题 O(1)判字符串是否回文
- 高德地图测试用例
- Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
- drools执行String规则或执行某个规则文件
- 子线程获取Request
- AI中台技术调研
- On data preprocessing in sklearn
猜你喜欢

(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

Go学习笔记—多线程

Multiply LCA (nearest common ancestor)

倍增 LCA(最近公共祖先)

PyTorch nn. Full analysis of RNN parameters

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

基于Arduino和ESP8266的连接手机热点实验(成功)

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
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers
随机推荐
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Leetcode739 daily temperature
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
二分刷题记录(洛谷题单)区间的甄别
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
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Go学习笔记—基于Go的进程间通信
mysql索引和事务
arcgis js 4.x 地图中加入图片
堆(優先級隊列)
Drools dynamically add, modify, and delete rules
High performance erasure code coding
Initial JDBC programming
CONDA common command summary
Introduction to CPU instruction set
Heap (priority queue)
计算二叉树的最大路径和
AI mid stage technology research