当前位置:网站首页>浏览器存储方案
浏览器存储方案
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("点击了修改");
}
};
});
边栏推荐
- Fastdateformat why thread safe
- Tas (file d'attente prioritaire)
- In development, why do you find someone who is paid more than you but doesn't write any code?
- String palindrome hash template question o (1) judge whether the string is palindrome
- AI mid stage technology research
- BOM DOM
- 二分刷题记录(洛谷题单)区间的甄别
- Leetcode739 daily temperature
- Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
猜你喜欢
China traffic sign detection data set
mysql数据库基础
Use sqoop to export ads layer data to MySQL
SparkContext: Error initializing SparkContext解决方法
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
分布式机器学习框架与高维实时推荐系统
Natural language processing series (I) -- RNN Foundation
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Drools dynamically add, modify, and delete rules
Brush questions --- binary tree --2
随机推荐
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
字符串回文hash 模板题 O(1)判字符串是否回文
FastDateFormat为什么线程安全
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Full link voltage measurement
Leetcode - Sword finger offer 51 Reverse pairs in an array
Find the common ancestor of any two numbers in a binary tree
Input a three digit number and output its single digit, ten digit and hundred digit.
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
High performance erasure code coding
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
H5 to app
【C语言】杨辉三角,自定义三角的行数
LeetCode—剑指 Offer 37、38
ES集群中节点与分片的区别
Day12 control flow if switch while do While guessing numbers game
drools决策表的简单使用
MySQL与PostgreSQL抓取慢sql的方法
SparkContext: Error initializing SparkContext解决方法