超过localStorage配额(localStorage大小!=文件下载大小)&如何查看本地存储大小
我想不出一个好的标题,所以希望可以。
我正在做的是创建一个离线 HTML5 web 应用程序。 “出于某些原因”,我不希望将某些文件放置在缓存清单中,而是希望将内容放置在 localStorage 中。
所有测试都是在 Mac/Mac OS 10.6.6 版 Google Chrome 中完成的。
我有 4 个 .JSON 文件,总大小恰好为 3,120,792
字节。第一次(在线)访问应用程序时,我会拉下此 JSON 内容并将其放入一个字符串变量中,例如 str
。在存储 JSON 字符串之前,我将删除所有 \n
和 \t
,并将 \"
替换为 '
,以最小化字符串长度。
以下是字符串长度(Chrome 控制台中的 str.length):
original str : 3,117,764 (this is a concatenation of the contents of the 4 JSON files)
reformatted JSON: 2,732,249 (not too shabby - should save me a few bytes)
但是,现在当我尝试将此字符串存储在 localStorage 中时,出现错误,提示我已超出缓存大小(Error : QUOTA_EXCEEDED_ERR:DOM 异常 22
),但此页面没有其他内容缓存(我已手动删除所有 Google Chrome 缓存文件),
为什么会发生这种情况
?
如何最小化此大小以充分利用 localStorage
编辑添加: 我尝试仅将字符串的 1,000,000 个字符保存到 localStorage (localStorage.setItem('x',str.substr(1,1000000)
) 中。当我检查 Chrome 缓存文件夹中的 localStorage 文件大小时,是 2MB。它看起来像浏览器使用的是实际的 localStorage 数据库大小,而不是其中存储的数据的大小。这当然很糟糕,但我猜你可以在 localStorage 中存储的最长字符串大约是 2,500,000 个字符?
I couldn't think of a good title, so hopefully that will do.
What I'm doing is creating an offline HTML5 webapp. "For certain reasons" I don't want some files to be placed in the cache manifest, but instead I want to place the content in localStorage.
All testing is done in Google Chrome for Mac/Mac OS 10.6.6
I have 4 .JSON files that are exactly 3,120,792
bytes in size total. Upon first visit (online) to the app, I am pulling down this JSON content and placing it in one string variable, say str
. Before storing the JSON string, I am stripping all \n
and \t
, and replacing \"
with '
, to minimize the string length.
Here are the string lengths (str.length in Chrome console):
original str : 3,117,764 (this is a concatenation of the contents of the 4 JSON files)
reformatted JSON: 2,732,249 (not too shabby - should save me a few bytes)
HOWEVER, now when I try to store this string in localStorage, I get an error that I have exceeded the cache size (Error: QUOTA_EXCEEDED_ERR: DOM Exception 22
). BUT there is NOTHING else cached for this page (I have manually deleted all Google Chrome cache files).
Why is this happening?
What is the size of the string when stored in the cache?
How can I minimize this size to make best use of localStorage?
edited to add:
I have tried saving only 1,000,000 characters of the string into localStorage (localStorage.setItem('x',str.substr(1,1000000)
). When I checked the localStorage file size in Chrome's cache folder it is 2MB. It's an sqlite db. It looks like the browser is using the actual localStorage database size, not the size of the data stored inside it. Which of course sucks, but is probably standard behavior. I guess the longest string you can store in localStorage is somewhere around 2,500,000 characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
了解本地存储中可以存储多少字符的一个好方法是通过该链接进行测试:http:// arty.name/localstorage.html。
大小取决于浏览器 HTML5 实现
我的最后一个测试:
Chrome 13/Safari 5/ IPAD 2:2.600.000 个字符
火狐 4:5.200.000
OPERA 11:无限制
A good way to know how many charactere can be store in the localstorage is to test it thanks to that link : http://arty.name/localstorage.html.
Size dépend on Browser HTML5 implementation
My last test :
Chrome 13/Safari 5/ IPAD 2 :2.600.000 chr
FIREFOX 4 : 5.200.000
OPERA 11 : unlimited
http://jsfiddle.net/brucealdridge/j6KdJ/
2,500,000 个字符适用于我的(“5.0(X11;Linux i686)AppleWebKit/534.30(KHTML,如 Gecko)Ubuntu/10.10 Chromium/12.0.742.9 Chrome/12.0.742.9 Safari/534.30”)
3,000,000 则不然。我在想这可能是编码问题,但我刚刚检查过,它不是
http://jsfiddle.net/brucealdridge/j6KdJ/
2,500,000 chars works on mine ("5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/10.10 Chromium/12.0.742.9 Chrome/12.0.742.9 Safari/534.30")
3,000,000 doesn't. I was thinking it might me an encoding thing, but i've just checked, and its not
奇怪的是,说明最大金额的答案会被接受。难道您不应该验证所有 localStorage 键/值对的当前大小减去浏览器实现的最大限制吗?
这是一个 jsfiddle 来说明这种用法。对于那些想要简单答案的人,请使用以下 js...
更新:此 snippit更加准确。
Weird that an answer illustrating max amount would be accepted. Shouldn't you be verifying the current size of all localStorage key/value pairs minus the max limits implemented by browsers?
Here is a jsfiddle to illustrate this usage. And for those that want a simple answer use the following bit of js...
UPDATE: This snippit is far more accurate.