Create a simple data caching system with no external dependencies
Greetings! If you're in need of a quick and dirty cache that doesn't need anything other than the current version of N8N, boy do I have a dodgy script for you to try! Presenting a simple template for a cache layer usi...
Template notes
Greetings! If you're in need of a quick and dirty cache that doesn't need anything other than the current version of N8N, boy do I have a dodgy script for you to try!
Presenting a simple template for a cache layer using the new N8N Tables Beta!
This flow can easily be adapted to meet your home lab or enterprise needs by swapping out the n8n tables nodes with a external database like redis or even a google sheet if one was so inclined.
Why Would You Want This? It's simple really, ever had your flow crash because one of the API's you're using has a rate limit less than the 10,000 GET requests you're throwing at it per second (yawn). Well caching can help by adding a small buffer on your side if the data is not likely to change between requests. Simply add a call to this flow to check the cache and if there's nothing there it will throw a error which you can detect and respond to by grabbing fresh data from the API.
How This Flow Works This flow does three simple steps in order. 1. Check what you want to do to the cache table, read or write. 2. If Reading, lookup the data | If writing, write the data. 3. If Reading, validate and return | If writing, just return data written for chaining.
This subflow will return the JSON.parse() representation of the string currently stored at the key in the cache table. This will be the same value written by the cache write input to this node if it has not expired.
If no value is found in the cache table for the input key, then a error is thrown. Listen for this error by setting your error response mode to be {On Error: "Continue (Using Error Output)"} in the node settings. This is your signal to "refresh" the cache by writing a new value to the cache.
Inputs Action Read Cache "cacheKey": {any string}