This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KillaVanilla's profile picture

HTTP Caching Layer

Started by KillaVanilla, 11 March 2014 - 10:24 PM
KillaVanilla #1
Posted 11 March 2014 - 11:24 PM
Hello again, everyone.


This little API was one inspired by a quick look through the Ideas Exchange in the General Forum.
It provides a simple cache for HTTP requests.


The default http.request function is unchanged, but http.get has been modified to look up a cache before making any network calls.
Both http.get and http.post will write to the cache upon success. Of course, POST requests won't use the cache.

Both functions take the same arguments their unmodified counterparts do, with the addition of an optional "cache timeout" parameter; this allows the programmer to specify how long the data written to the cache should persist.

This argument only matters if data is written back to the cache (i.e if the data in the cache is old or nonexistent).

There are also two other functions: http.clearCache and http.selectiveClearCache.
http.clearCache takes one optional parameter (a url). It will clear the specified site from the cache, and clear the entire cache if a url is not specified.
http.selectiveClearCache takes no parameters and will only clear "old" (timed out) cache entries.

Find it here.
Pastebin Code: qGX5RxDU


pastebin get qGX5RxDU cached_http
Lyqyd #2
Posted 12 March 2014 - 02:53 AM
You should post a link to this in the Idea Exchange topic if you haven't already! Maybe edit it into your last post in that thread.
KillaVanilla #3
Posted 12 March 2014 - 08:47 AM
You should post a link to this in the Idea Exchange topic if you haven't already! Maybe edit it into your last post in that thread.

I just did.
oeed #4
Posted 12 March 2014 - 09:09 AM
Looks good.

Just a question, what's the benefit of not clearing old cache entries, or in other words, what's the point of the selectiveClearCache function?
Edited on 12 March 2014 - 08:10 AM
KillaVanilla #5
Posted 12 March 2014 - 10:59 AM
Looks good.

Just a question, what's the benefit of not clearing old cache entries, or in other words, what's the point of the selectiveClearCache function?

…you know, I have no idea.

The only reason I can think of for selective clearing is to clear storage space, and any scenario where you would have to do that would be unlikely (reading very large websites, having low disk space to start with, etc.).
I think I was mainly using it as a test for the "has an cache entry timed out" check.