Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who is responsible to fetch data from server in a flux app with caching?

In the flux webchat example application and in the README diagram, it seems like the action creator should retrieve the data from the server.

flux data flow

The problem I see is that no fetch might be required if the data is already in the store. The store is the only one to know, so the action need to actually be dispatched.

I think it's better to fetch dernormalized data when possible, to minimize xhr calls. If the store is denormalized e.g. MessageStore will contain all the data it needs to render Messages. Every message look like

{
 "id": 42
 "message": "Héllo, you tried reactjs-flux too. Awesome isn't it!"
 "user": {id: 1337, username: "amirouche", bio: "maker"},
 "likes": [{id: 2600, username: "NinjaTurtle"}, {id: 2601, username: "Peer"}
}

The store might have the responsability (through events?) to update the UserStore with the partial user models.

Another way I think of, is to have some normalized stores, and create a specific stores with a schema the view expects.

In this situation, it seems to me the action creator is only useful to dispatch the payload ie. it's useless.

What do you think?

like image 690
amirouche Avatar asked Feb 06 '15 22:02

amirouche


People also ask

What is the purpose of caching in an application?

Whenever your application has to read data it should first try to retrieve the data from the cache. Only if it’s not found in the cache then it should try to get the data from the data store. Caching improves latency and can reduce the load on your servers and databases.

How does the cache work in mobile apps?

In this strategy, the cache is sitting aside the database. The application will first request the data from the cache. If the data exists (we call this a ‘cache hit’), the app will retrieve the data directly.

How do I combine data fetch and cache strategies?

The solution is to combine data fetch and cache strategies. To make the page aware of whether it needs to fetch the latest data or just read from the cache, we can set a cache’s max-age. When data exceeds its max-age, we fetch a new one and replace it. The picture below shows the logic of this strategy.

What is the use of a cache in a database?

Caching is a buffering technique that stores frequently-queried data in a temporary memory. It makes data easier to be accessed and reduces workloads for databases. For example, you need to retrieve a user’s profile from the database and you need to go from a server to server.


1 Answers

I usually have my store dealing with fetching data. Doing it this way make sure that all the logic can be managed by the store itself, and you have total control on when/how/why you fetch data.

The fact that you can also have stores communicating with each other is also another pro for why the stores should be in charge of handling/fetching data.

Also, as the Flux pattern describes: "Stores contain the application state and logic. Their role is somewhat similar to a model in a traditional MVC, but they manage the state of many objects.." It does make sense to have the stores managing the fetching of data from APIs/server-side.

like image 61
Jeremy D Avatar answered Oct 22 '22 20:10

Jeremy D