Where should cache the logic in the thread application?

In the previous question, I asked who is responsible for sending updates to the server in the Flux application. People told me that Actions should do this. Therefore, I suppose the same applies to retrieving data from the server; you have a FetchData action that retrieves data and sends data to the store. But then, how will the caching logic work?

I think I needed to save the last time the list was requested, and the TTL list in the StreamsStore stream and the fetchStreams action will receive the TTL and the latest fetch time to determine whether to consult the server.

Is it correct? It seems strange to me to distribute the caching logic between the repository and the action, but I cannot think of a better way to do this.

+7
caching flux
source share
1 answer

This is a great question and one that I have encountered before.

Remember that the most important thing about Flux is that data is transferred one way, always. You already know this - I bring it because one of these statements has many explanatory powers and almost completely answers any question that may arise regarding Flux.

Actions send data to storages, so if you add logic to your actions that check the value of something in your store, you send data in the wrong direction, against the stream.

So, what part of the Flux app gets data from stores? Kinds. There is your answer.

The idea of ​​your views containing caching logic may seem strange, but think about what caching is:

  • I need data.
  • Do I already have this data? If not...
  • Let's go to.

Viewer # 1. This is pretty simple. And No. 3 is obviously being handled by your actions. But it turns out that No. 2, at least in the Flux application, also has something that needs to be considered in your views, or rather, your view controllers. Controller views are often a forgotten part of Flux, probably because the idea of ​​controllers is so strongly linked to MVC. But Flux has them too! On the Flux website:

Controllers exist in the Flux application, but they are controller views β€” views that are often found at the top of the hierarchy that retrieve data from storage and pass that data on to their children.

Assuming you are using React, this idea should sound familiar. Higher-level reaction components are -y controllers, and lower-level components are more "pure".

Another way to think about it is to note that actions are simply dispatcher assistants. (If I remember correctly when Facebook first introduced Flux, they didn't even mention the actions.) By the time you called the action, you had already decided to send: the only question is what, not if.

Taking this back, I understand that all this may seem like a difference without differences, but the main conclusion is that no, actions cannot check the state of the store. They can communicate with them only through the dispatcher. You may find a way to make it work in practice (which should not be discounted!), But this is not an idiomatic flow.

Hope this makes sense!

+5
source share

All Articles