查看原文
其他

8个JavaScript库可更好地处理本地存储

张张 前端全栈开发者 2022-07-09
点击上方蓝字关注这个脱发、摆摊、卖货、持续学习的程序员

来源:https://medium.com/javascript-in-plain-english
作者:Amy J. Andrews

我为当前项目测试了一些本地存储库。想知道他们有什么很棒的功能吗?继续阅读。

Local Storage Bridge

https://github.com/krasimir/lsbridge

如果你必须在同一个浏览器中从一个标签页发送消息到另一个标签页,你不必用艰难的方式。Local storage bridge在这里让任务变得更简单。

基本使用:

// 发送
lsbridge.send(‘app.message.error’, { error: ‘Out of memory’ });

// 监听
lsbridge.subscribe(‘app.message.error’, function(data) {
console.log(data); // { error: ‘Out of memory’ }
});

Basil.js

http://wisembly.github.io/basil.js/

Basil.js统一了session、localStorage和cookie,为你提供了一种处理数据的直接方法。

基本使用:

let basil = new Basil(options);

basil.set(‘name’, ‘Amy’);
basil.get(‘name’);
basil.remove(‘name’);
basil.reset();

store.js

https://github.com/marcuswestin/store.js

Store.js像其他东西一样处理数据存储。但还有更多的功能,它的一个高级特性是让你更深入地访问浏览器支持。

基本使用:

store.set(‘book’, { title: ‘JavaScript’ }); // Store a book
store.get(‘book’); // Get stored book
store.remove(‘book’); // Remove stored book
store.clearAll(); // Clear all keys

lscache

https://github.com/pamelafox/lscache

它与localStorage API类似。事实上,它是localStorage的一个封装器,并使用HTML5模拟memcaches函数。在上面的文档中发现更多的功能。

基本使用:

lscache.set(‘name’, ‘Amy’, 5); // 数据将在5分钟后过期
lscache.get(‘name’);

Lockr

https://github.com/tsironis/lockr

Lockr建立在localStorage API之上。它提供了一些有用的方法来更轻松地处理本地数据。

是什么让你要使用此库而不是localStorage API?

好吧,localStorage API仅允许你存储字符串。如果要存储数字,则需要先将该数字转换为字符串。在Lockr中不会发生这种情况,因为Lockr允许你存储更多的数据类型甚至对象。

基本使用:

Lockr.set(‘name’, ‘Amy’);
Lockr.set(‘age’, 28);
Lockr.set(‘books’, [{title: ‘JavaScript’, price: 11.0}, {title: ‘Python’, price: 9.0}]);

Barn

https://github.com/arokor/barn

Barn在localStorage之上提供了一个类似Redis的API。如果持久性很重要,那么你将需要这个库来保持数据状态,以防发生错误。

基本使用:

let barn = new Barn(localStorage);

// 原始类型
barn.set(‘name’, ‘Amy’);
let name = barn.get(‘name’); // Amy

// List
barn.lpush(‘names’, ‘Amy’);
barn.lpush(‘names’, ‘James’);
let name1 = barn.rpop(‘names’); // Amy
let name2 = barn.rpop(‘names’); // James

localForage

https://github.com/localForage/localForage

这个简单而快速的库将通过IndexedDB或WebSQL使用异步存储来改善Web的脱机体验。它类似于localStorage,但具有回调功能。

基本使用:

localforage.setItem(‘name’, ‘Amy’, function(error, value) {
// Do something
});

localforage.getItem(‘name’, function(error, value) {
if (error) {
console.log(‘an error occurs’);
} else {
// Do something with the value
}
});

很神奇的是它提供中文文档

crypt.io

https://github.com/jas-/crypt.io

crypt.io使用标准JavaScript加密库实现安全的浏览器存储。使用crypto.io时,有三个存储选项:sessionStorage,localStorage或cookie。

基本使用:

let storage = crypto;
let book = { title: ‘JavaScript’, price: 13 };

storage.set(‘book’, book, function(error, results) {
if (error) {
throw error;
}

// Do something
});

storage.get(‘book’, function(error, results) {
if (error) {
throw error;
}
// Do something
});

你还知道其他本地存储库吗?为什么使用它?在下面的评论中让我知道!

更多文章

粉丝福利
临走前留下,今天的福利
  • 福利1:某慕网课程《React Hooks重构去哪网购票》获取地址和密码请在公众号中回复关键字:002
  • 福利2:在看+留言,张张会在留言区随机抽取一位认真留言的小伙伴,给他发一个红包奖励



- END -

走心的分享更容易被抽中~
开奖时间 下期文末

点赞 + 在看 + 留言
下一个幸运儿就是你

👇

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存