What is cache?

➡️ In programming, collection of data stored for future use & which can be retrieved faster.

🤔How do you achieve this in your code?

Example in the thread 👇👇
This is a simple class defining Line.

If you see carefully, we have a var distance only accessible via its getter method getDistance.

How is caching happening here?

Thread contd. 👇
➡️ Even if we don't have distance in the class variables, we can get the distance from its getter by calculating it everytime.

➡️ But for faster retrieval of data, we store the distance and calculate only if coordinates changed.

This means we are caching the distance.
The code is C++ pseudocode.

private data cannot be accessed outside the class.

public data can access private data.

public data can be accessed outside the class.
You can follow @divyajyotiuk.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.