Every time someone pastes a link into ramrom.io, my server calls a third-party API to work out what the video is and where the file lives. That call costs quota, and on the free RapidAPI tier quota is the one thing I cannot waste. What surprised me is how often the same link gets resolved twice in the space of a minute — and how cheap it was to stop it.

The double resolve nobody plans for

Here is the pattern I kept seeing in the logs. A visitor pastes a TikTok link, the page resolves it, and the quality list appears. Then they tap a quality option, or they tap the button again because nothing seemed to happen, or their phone double-fires the request. Each of those is a fresh call to resolve.php, and each of those is a fresh call to the provider. Same link, same answer, paid for twice.

The fix: a 60-second flat-file cache

The answer was embarrassingly simple. Inside resolve.php, before calling the provider, I build a cache key from the normalised link — the same normalised URL the rest of the endpoint already produces, with short domains expanded and tracking parameters stripped. Then I look for a flat file named after the hash of that key.

If the file exists and is less than 60 seconds old, I return its contents straight away and never touch the provider. If it is missing or stale, I call the provider as normal, write the fresh response to the file, and then return it.

The code is about twenty lines. No Redis, no database, no extra service — the site already runs on plain shared hosting with no database at all, so a flat file in a small cache folder was the natural fit. The folder lives outside the public web root, the filenames are hashes rather than raw URLs, and the whole thing is just file_get_contents, file_put_contents, and a check on filemtime.

Why 60 seconds and not an hour

Sixty seconds is deliberately short. Video links go stale. A story can expire, a post can be deleted, a provider can change its answer. A long cache would hand visitors yesterday's answer to today's question, and for a downloader that is worse than no cache at all.

But sixty seconds is long enough to cover the real duplicates: the double tap, the impatient second click, the page re-resolving after a quality change, my own copy-paste testing. That is the whole point — the cache is not there to save work over hours. It is there to stop paying twice for the same question asked twice in one breath.

What it actually saved

I will not quote a number I cannot stand behind, but the shape of the saving is clear in the logs: the second call for the same link within the window simply disappears. The visitor sees the same result, only faster, because reading a small file on disk beats a round trip to a provider every time. On a slow mobile connection, that difference is noticeable.

There is one honest trade-off. If the provider's answer changes inside the minute — say a video becomes unavailable — the visitor gets the old, working answer for up to a minute. I decided that was acceptable. A minute of staleness is a small price for not burning quota on duplicates, and the next resolve after the window fetches fresh data anyway.

The lesson I keep relearning

Every caching decision on this site has the same shape: pick the smallest window that covers the real repeat, and no longer. The 60-second resolve cache sits next to the per-IP rate limit and the retry loop as part of the same idea — protect the quota, be honest about what is cached and for how long, and never let a shortcut change the answer the visitor gets.

It is not a clever system. It is a folder of small files and a timestamp check. But it is the kind of boring, invisible win that keeps a free tool free, and I would add it again on day one if I were rebuilding the site from scratch.