javascript
8个JavaScript库可更好地处理本地存储
來源:https://medium.com/javascript-in-plain-english
作者:Amy J. Andrews
我為當(dāng)前項目測試了一些本地存儲庫。想知道他們有什么很棒的功能嗎?繼續(xù)閱讀。
Local Storage Bridge
https://github.com/krasimir/lsbridge
如果你必須在同一個瀏覽器中從一個標(biāo)簽頁發(fā)送消息到另一個標(biāo)簽頁,你不必用艱難的方式。Local storage bridge在這里讓任務(wù)變得更簡單。
基本使用:
// 發(fā)送 lsbridge.send(‘a(chǎn)pp.message.error’, { error: ‘Out of memory’ });// 監(jiān)聽 lsbridge.subscribe(‘a(chǎn)pp.message.error’, function(data) {console.log(data); // { error: ‘Out of memory’ } });Basil.js
http://wisembly.github.io/basil.js/Basil.js統(tǒng)一了session、localStorage和cookie,為你提供了一種處理數(shù)據(jù)的直接方法。
基本使用:
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像其他東西一樣處理數(shù)據(jù)存儲。但還有更多的功能,它的一個高級特性是讓你更深入地訪問瀏覽器支持。
基本使用:
store.set(‘book’, { title: ‘JavaScript’ }); // Store a book store.get(‘book’); // Get stored book store.remove(‘book’); // Remove stored book store.clearAll(); // Clear all keyslscache
https://github.com/pamelafox/lscache
它與localStorage API類似。事實上,它是localStorage的一個封裝器,并使用HTML5模擬memcaches函數(shù)。在上面的文檔中發(fā)現(xiàn)更多的功能。
基本使用:
lscache.set(‘name’, ‘Amy’, 5); // 數(shù)據(jù)將在5分鐘后過期 lscache.get(‘name’);Lockr
https://github.com/tsironis/lockrLockr建立在localStorage API之上。它提供了一些有用的方法來更輕松地處理本地數(shù)據(jù)。
是什么讓你要使用此庫而不是localStorage API?
好吧,localStorage API僅允許你存儲字符串。如果要存儲數(shù)字,則需要先將該數(shù)字轉(zhuǎn)換為字符串。在Lockr中不會發(fā)生這種情況,因為Lockr允許你存儲更多的數(shù)據(jù)類型甚至對象。
基本使用:
Lockr.set(‘name’, ‘Amy’); Lockr.set(‘a(chǎn)ge’, 28); Lockr.set(‘books’, [{title: ‘JavaScript’, price: 11.0}, {title: ‘Python’, price: 9.0}]);Barn
https://github.com/arokor/barn
Barn在localStorage之上提供了一個類似Redis的API。如果持久性很重要,那么你將需要這個庫來保持?jǐn)?shù)據(jù)狀態(tài),以防發(fā)生錯誤。
基本使用:
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’); // JameslocalForage
https://github.com/localForage/localForage
這個簡單而快速的庫將通過IndexedDB或WebSQL使用異步存儲來改善Web的脫機體驗。它類似于localStorage,但具有回調(diào)功能。
基本使用:
localforage.setItem(‘name’, ‘Amy’, function(error, value) {// Do something });localforage.getItem(‘name’, function(error, value) {if (error) {console.log(‘a(chǎn)n error occurs’);} else {// Do something with the value} });很神奇的是它提供中文文檔
crypt.io
https://github.com/jas-/crypt.io
crypt.io使用標(biāo)準(zhǔn)JavaScript加密庫實現(xiàn)安全的瀏覽器存儲。使用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 });你還知道其他本地存儲庫嗎?為什么使用它?在下面的評論中讓我知道!
IT技術(shù)分享社區(qū)
個人博客網(wǎng)站:https://programmerblog.xyz
文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠(yuǎn)程辦公:常用的遠(yuǎn)程協(xié)助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎(chǔ)知識硬件:斷路器、接觸器、繼電器基礎(chǔ)知識
總結(jié)
以上是生活随笔為你收集整理的8个JavaScript库可更好地处理本地存储的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QQ MSN 网页互动代码
- 下一篇: jersey spring_教程–带有J