Cache VS Session VS cookies?

前端 未结 8 777
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 04:09

What are the do\'s and don\'ts about Cache VS Session VS Cookies?

For example:
I\'m using Session variables a lot and sometimes have problems in a booking-applic

8条回答
  •  没有蜡笔的小新
    2020-12-02 04:46

    I was always confused between LocalStorage, SessionStorage and Cookie, but not anymore.

    Just link the words are self explainable what they suppose to do.

    LocalStorage: Local Storage, what does that mean, just thing you don't know anything about technology, but by the itself you can guess. It is some storage which stores data locally.

    that what it is.

    IT stores data in Browser without any expiration until user clear it through JavaScript code or Clear browser cache.

    Session Storage: It seems like it also stores data but related to a session then how different it is from localStorage?

    The main difference is your session storage data will be deleted once the session is finish or browser tab is closed or the browser is closed.

    You can just try in browser console by setting

    localStorage.setItem('name' , 'alex')
    sessionStorage.setItem('session','seesion value')
    

    and then close tab and open again, you can still find localStorage data but not sessionStorage data.

    Cookie: So this is totally different from the above two. A cookie generally used for the server-side purpose.

    • Stores data that has to be sent back to the server with subsequent requests.
    • Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side).
    • Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side.
    • Size must be less than 4KB.
    • Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie

提交回复
热议问题