Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way use caching data in js on client side?

My application receives data from the another server, using API with limited number of requests. Data changing rarely, but may be necessary even after refresh page.

  1. What's the best solution this problem, using cookie or HTML5 WebStorage?
  2. And may be have other way to solve this task?
like image 503
Artem Zubkov Avatar asked Oct 07 '12 15:10

Artem Zubkov


People also ask

How do you implement caching on client-side?

Open the application. Settings Select the category. System -> Performance Type a number of minutes into the setting. Cache files (minutes) Save the settings.

Is caching server-side or client-side?

The browser request caching is the oldest and most common type of client-side caching. It's built into the HTTP protocol standard. It lets the webmaster or developer control how often the browser requests a new copy of files from the server.

Which can be used for client-side user data?

There are four main methods for storing large amounts of client-side data today: Web SQL, IndexedDB, Web Storage and Application Cache.

Can JavaScript store data on the client system and retrieve it?

It consists of JavaScript APIs that allow you to store data on the client (i.e. on the user's machine) and then retrieve it when needed.

When would you use server-side caching?

Server side web caching typically involves utilizing a web proxy which retains web responses from the web servers it sits in front of, effectively reducing their load and latency. Client side web caching can include browser based caching which retains a cached version of the previously visited web content.


1 Answers

As much as cross browser compatibility matters, cookie is the only choice rather than web storage.

But the question really depends on what kind of data you are caching?

For what you are trying, cookie and web-storage might not be needed at all.

  • Cookies are used to store configuration related information, rather than actual data itself.
  • Web storage supports persistent data storage, similar to cookies but with a greatly enhanced capacity and no information stored in the HTTP request header. [1]

I would rather say, it would be stupid to cache the entire page as cookie or web-storage both. For these purposes, server-side caching options might be the better way.

Update:

Quoting:

data about user activity in some social networks (fb, vk, google+)

Detect the web-storage features, using libraries like mordernizr and if does not exists fall back to cookie method. A simple example

if (Modernizr.localstorage) {
    // browser supports local storage
    // Use this method
} else {
    // browser doesn't support local storage
    // Use Cookie Method
}

[1]: http://en.wikipedia.org/wiki/Web_storage

like image 166
Starx Avatar answered Oct 27 '22 03:10

Starx