简体   繁体   English

HTML5 本地存储与会话存储

[英]HTML5 Local storage vs. Session storage

除了非持久性和仅限于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能、数据访问等)?

localStorage and sessionStorage both extend Storage . localStoragesessionStorage都扩展了Storage There is no difference between them except for the intended "non-persistence" of sessionStorage .除了sessionStorage的预期“非持久性”之外,它们之间没有区别。

That is, the data stored in localStorage persists until explicitly deleted .也就是说,存储在localStorage的数据一直存在,直到显式删除 Changes made are saved and available for all current and future visits to the site.所做的更改将被保存并可供所有当前和将来访问该站点时使用。

For sessionStorage , changes are only available per tab.对于sessionStorage更改仅适用于每个选项卡。 Changes made are saved and available for the current page in that tab until it is closed.所做的更改将被保存并可用于该选项卡中的当前页面直到它关闭。 Once it is closed, the stored data is deleted.一旦关闭,存储的数据将被删除。

The only difference is that localStorage has a different expiration time, sessionStorage will only be accessible while and by the window that created it is open.唯一的区别是 localStorage 有不同的到期时间, sessionStorage只能在创建它的窗口打开时访问。
localStorage lasts until you delete it or the user deletes it. localStorage一直持续到您删除它或用户删除它为止。
Lets say that you wanted to save a login username and password you would want to use sessionStorage over localStorage for security reasons (ie. another person accessing their account at a later time).假设您想保存登录用户名和密码,出于安全原因,您希望在localStorage上使用sessionStorage (即,稍后另一个人访问他们的帐户)。
But if you wanted to save a user's settings on their machine you would probably want localStorage .但是如果你想在他们的机器上保存用户的设置,你可能需要localStorage All in all:总而言之:

localStorage - use for long term use. localStorage - 用于长期使用。
sessionStorage - use when you need to store somthing that changes or somthing temporary sessionStorage - 当您需要存储更改或临时存储的东西时使用

Few other points which might be helpful to understand differences between local and session storage可能有助于理解本地和会话存储之间差异的其他几点

  1. Both local storage and session storage are scoped to document origin, so本地存储和会话存储都限于文档来源,因此

    https://mydomain.com/ https://mydomain.com/
    http://mydomain.com/ http://mydomain.com/
    https://mydomain.com:8080/ https://mydomain.com:8080/

    All of the above URL's will not share the same storage.以上所有 URL不会共享相同的存储。 (Notice path of the web page does not affect the web storage) (注意网页路径不影响网页存储)

  2. Session storage is different even for the document with same origin policy open in different tabs, so same web page open in two different tabs cannot share the same session storage.即使在不同标签页中打开具有同源策略的文档,会话存储也是不同的,因此在两个不同标签页中打开的同一个网页不能共享同一个会话存储。

  3. Both local and session storage are also scoped by browser vendors .本地和会话存储也受浏览器供应商的限制 So storage data saved by IE cannot be read by Chrome or FF.所以IE保存的存储数据不能被Chrome或FF读取。

Hope this helps.希望这可以帮助。

The main difference between localStorage and sessionStorage is that sessionStorage is unique per tab. localStoragesessionStorage之间的主要区别在于sessionStorage每个选项卡都是唯一的。 If you close the tab the sessionStorage gets deleted, localStorage does not.如果您关闭sessionStorage将被删除的选项卡, localStorage不会。 Also you cannot communicate between tabs :)您也无法在选项卡之间进行通信:)

Another subtle difference is that for example on Safari (8.0.3) localStorage has a limit of 2551 k characters but sessionStorage has unlimited storage另一个细微的区别是,例如在 Safari (8.0.3) 上, localStorage的限制为 2551 k 个字符,但sessionStorage存储空间不受限制

On Chrome (v43) both localStorage and sessionStorage are limited to 5101 k characters (no difference between normal / incognito mode)在 Chrome (v43) 上, localStoragesessionStorage都被限制为 5101 k 个字符(正常/隐身模式之间没有区别)

On Firefox both localStorage and sessionStorage are limited to 5120 k characters (no difference between normal / private mode )在 Firefox 上localStoragesessionStorage都被限制为 5120 k 个字符(正常/私有模式之间没有区别)

No difference in speed whatsoever :)速度没有任何区别:)

There's also a problem with Mobile Safari and Mobile Chrome, Private Mode Safari & Chrome have a maximum space of 0KB Mobile Safari 和 Mobile Chrome 也有问题,Private Mode Safari & Chrome 的最大空间为 0KB

sessionStoragelocalStorage相同,不同之处在于它只存储一个会话的数据,当用户关闭创建它的浏览器窗口时,它将被删除。

performance wise, my (crude) measurements found no difference on 1000 writes and reads性能方面,我的(粗略)测量发现 1000 次写入和读取没有差异

security wise, intuitively it would seem the localStore might be shut down before the sessionStore, but have no concrete evidence - maybe someone else does?安全明智,直觉上似乎 localStore 可能在 sessionStore 之前关闭,但没有具体证据 - 也许其他人这样做了?

functional wise, concur with digitalFresh above功能明智,同意上面的digitalFresh

会话存储和本地存储在行为上是相同的,除了本地存储将存储数据直到并且除非用户删除缓存和 cookie 和会话存储数据将保留在系统中,直到我们关闭会话,即直到我们关闭会话存储创建窗口。

The advantage of the session storage over local storage, in my opinion, is that it has unlimited capacity in Firefox, and won't persist longer than the session.在我看来,会话存储相对于本地存储的优势在于它在 Firefox 中具有无限容量,并且持续时间不会超过会话。 (Of course it depends on what your goal is.) (当然这取决于你的目标是什么。)

  • sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long as the browser is open, including page reloads and restores) sessionStorage为每个给定的源维护一个单独的存储区域,在页面会话期间可用(只要浏览器打开,包括页面重新加载和恢复)

  • localStorage does the same thing, but persists even when the browser is closed and reopened. localStorage做同样的事情,但即使在浏览器关闭并重新打开时仍然存在。

I took this from Web Storage API我从Web Storage API了这个

Local storage: It keeps store the user information data without expiration date this data will not be deleted when user closed the browser windows it will be available for day, week, month and year.本地存储:保存用户信息数据,无过期日期,用户关闭浏览器窗口时不会删除该数据,日、周、月、年均可使用。

//Set the value in a local storage object
localStorage.setItem('name', myName);

//Get the value from storage object
localStorage.getItem('name');

//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege

Session Storage: It is same like local storage date except it will delete all windows when browser windows closed by a web user.会话存储:它与本地存储日期相同,但它会在网络用户关闭浏览器窗口时删除所有窗口。

//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";

Read More Click阅读更多点击

Late answer but felt to add some points here.迟到的答案,但觉得在这里补充几点。

Session storage will be available for specific tab where as we can use Local storage through out the browser.会话存储可用于特定选项卡,因为我们可以在浏览器中使用本地存储。 Both are default to same origin and we can also store values manually with key, value pairs (value must be string).两者默认为同源,我们也可以使用键值对手动存储值(值必须是字符串)。

Once tab (session) of the browser is closed then Session storage will be cleared on that tab, where as in case of Local storage we need to clear it explicitly .关闭浏览器的选项卡(会话)后,该选项卡上的会话存储将被清除,在本地存储的情况下,我们需要明确清除它 Maximum storage limit respectively 5MB and 10MB .最大存储限制分别为5MB10MB

We can save and retrieve the data like below,我们可以像下面这样保存和检索数据,

To Save:保存:

sessionStorage.setItem('id', noOfClicks);   // localStorage.setItem('id', noOfClicks);

sessionStorage.setItem('userDetails', JSON.stringify(userDetails));   // if it's object

To Get:要得到:

sessionStorage.getItem('id');    // localStorage.getItem('id');

User user = JSON.parse(sessionStorage.getItem("userDetails")) as User;  // if it's object

To Modify:修改:

sessionStorage.removeItem('id');    // localStorage.removeItem('id');

sessionStorage.clear();   // localStorage.clear();

PS: getItem() also return back the data as string and we need convert it into JSON format to access if it's object. PS: getItem()也将数据作为字符串返回,如果它是对象,我们需要将其转换JSON 格式才能访问。

You can read more about Browser Storages here..您可以在此处阅读有关浏览器存储的更多信息。

  1. Difference between localStorage, sessionStorage and cookies localStorage、sessionStorage 和 cookie 的区别

  2. localstorage-vs-sessionstorage 本地存储与会话存储

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM