DEV Community

Adam Crockett 🌀
Adam Crockett 🌀

Posted on

Is writing to JSON an acceptable cache

So I'm making a request to an API with a rate limit, I only need to do a request daily and then serve some cached data to my clients, does a simple JSON cache suit this application?

Top comments (7)

Collapse
 
rhymes profile image
rhymes • Edited

It seems what you're asking is a normal cache. Get the response, put it in a Redis instance and set a 24h expiration time.

From your web app get the cached data, if the cache is empty, go back to the API, get the response, fill the cache and serve it to the client.

It could be as simple as that.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Sounds about right, I have implemented something close to this idea in a JSON cache where file names are dated.

Collapse
 
rhymes profile image
rhymes

You can also use the disk if you don't want to setup Redis and if you have access to the file system.

A cache is a cache. There are advantages and disadvantages on each cache but you can totally start with a json file on disk and then move on something more scalable if you need to.

The principle is the same, replace Redis with the file. Keep in mind that you need to cleanup the cache manually at some point, though one file per day isn't too much

Collapse
 
tchaffee profile image
Todd Chaffee

What do you mean by a 'simple JSON cache'? How would you implement it? What makes it simple vs. a "complex" JSON cache?

Based on the very limited description you gave, sure, storing the results of the API request in JSON and serving that cached data to your clients sounds like a reasonable solution.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

I could say it's complex or simple or intermediate, that doesn't matter, nor does what language or platform in all honesty. Perhaps I'm asking about JSON to disk vs redis or equivalent. What are the implications around perf and security. Assuming the file will be stored on a web server.

Collapse
 
tchaffee profile image
Todd Chaffee

There isn't enough information to make a decision. I don't know how big the file is, how many users will access it, what the security requirements are, etc. Do the simplest thing you can based on your specific requirements. When it doesn't work anymore due to increased traffic or some other new requirement, you cross that bridge when you get there. Premature optimization is just going to make things harder for you to debug and maintain: en.wikipedia.org/wiki/Program_opti...

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Your question does answer my question though.. I trickled my question and requirements because people take a lot for granted and read the requirements as they want. If I give you less information gradually, you have actually helped, think of it like counciling for developers.

How big are the files? this was unlimited until you mentioned it.

Do I need to cache at all? Yes probably, I need to get around the rate limit of 10 per minute