Data cache and lifetime

33 views
Skip to first unread message

Kevin Ingwersen (Ingwie Phoenix)

unread,
Jul 14, 2015, 8:18:44 AM7/14/15
to nod...@googlegroups.com
hey.

I want to implement a cache server that starts empty but can restore itself after an exit from a JSON database. The principe of doing that seems simple, but there is just one problem: the time-to-live parameter.

How can I best introduce a time-to-live, aka. time till the entry gets purged automaticaly, in the best node-ish way? I know that just using a setTimeout() would be a bad idea since the server might exit in the meantime and the key and its value will just be carried over and not be urged at the next run.

Any idea how I could implement this best?

Kind regards, Ingwie.

zladuric

unread,
Jul 15, 2015, 7:54:20 AM7/15/15
to nod...@googlegroups.com
If you want cache to survive reboots, you need to store all of the state externally, including any timeout/expiry conditions. One way to do it is to store the timeout (UTC) times together with the cache entry metadata. So an entry in your (simplified) cache collection would look like:

```
    {
      "data": payload,
      "metadata": {
        "type": "something",
        "timeout": "2015-07-15T07:11:23.079Z"
      }

You'd need a load and store procedures too for the cache server which would recover all this and serialize/deserialize data, I guess.
Reply all
Reply to author
Forward
0 new messages