istorage
v1.0.4
Published
缓存应用库,并且可以设置过期时间与数据加解密 适用于 `Nodejs` 和 `Web`端。`Web`端封装 `localStorage | sessionStorage | indexedDB`。
Downloads
7
Maintainers
Readme
istorage
缓存应用库,并且可以设置过期时间与数据加解密
适用于 Nodejs
和 Web
端。Web
端封装 localStorage | sessionStorage | indexedDB
。
Nodejs
const IStorage = require('istorage');
const storage = new IStorage({expires: 6 * 1000});
storage.set('name', 'John');
storage.set('age', '30');
storage.set('fn', function () {});
storage.set('obj', { name: 'John' });
// console.log(storage.keys());
console.log(storage.values());
storage.update('age', '31');
console.log(storage.values());
let i = 0
let x = setInterval(() => {
i++
console.log(i);
if (i > 6) {
console.log(i, storage.all());
clearInterval(x)
}
}, 1000);
localStorage | sessionStorage
const IStorage = require('istorage');
const storage = new IStorage({type: "localStorage", expires: 6 * 1000});
storage.set('name', 'John');
storage.set('age', '30');
storage.set('fn', function () {});
storage.set('obj', { name: 'John' });
// console.log(storage.keys());
console.log(storage.values());
storage.update('age', '31');
console.log(storage.values());
let i = 0
let x = setInterval(() => {
i++
console.log(i);
if (i > 6) {
console.log(i, storage.all());
clearInterval(x)
}
}, 1000);
indexedDB(使用indexedDB的时候要注意它是异步的,它是异步的,它是异步的)
<button id="set">set</button>
<button id="set1">set1</button>
<button id="get">get</button>
<button id="all">all</button>
<button id="remove">remove</button>
<button id="clear">clear</button>
<script>
// const storage = new IStorage({type: "indexedDB", expires: 6 * 1000});
const storage = new IStorage({type: "indexedDB"});
storage.use('11111111111111111111111111111111111111111111')
document.getElementById("set").addEventListener("click", () => {
storage.set("test", {
name: "test",
age: 18
})
});
document.getElementById("set1").addEventListener("click", () => {
storage.set("test1", function() {
return {
name: "test1",
age: 18
}
})
});
document.getElementById("all").addEventListener("click", async () => {
const data = await storage.all()
console.log(data)
});
document.getElementById("clear").addEventListener("click", () => {
storage.clear()
});
document.getElementById('get').addEventListener('click', () => {
console.log(storage.get('test1'))
})
document.getElementById('remove').addEventListener('click', () => {
storage.remove('test1')
})
</script>