Allow importing and exporting to JSON#83
Conversation
|
I was going to submit a PR for the same thing — I think this would be extremely helpful. I have an app that relies heavily on memory-cache, and deploys wipe the cache, obviously. I'm also sure that some apps running under supervisor or nodemon occasionally crash on some unexpected data, and could use this to keep things moving along smoothly. |
|
@ptarjan is this something you'd consider merging? Let me know if there's anything I can improve. |
|
Yeah this sounds reasonable. I might debate on the names and API format
once I see it but the overall shape seems broadly useful.
…On Mon, Feb 27, 2017 at 1:20 PM Kevin Cooper ***@***.***> wrote:
@ptarjan <https://github.com/ptarjan> is this something you'd consider
merging? Let me know if there's anything I can improve.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#83 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACcz7ANhU1XA0V19xwgJCoSDK-JyKyyks5rgz43gaJpZM4MCVAu>
.
|
| var record = cacheToImport[key]; | ||
|
|
||
| // This could possibly be `'NaN'` if there's no expiry set. | ||
| var remainingTime = record.expire - currTime; |
There was a problem hiding this comment.
It was unintuitive to me that 'Nan' - Date.now() == Nan? Either add a separate if or a comment
There was a problem hiding this comment.
Agreed, I'll make this more clear.
|
|
||
| if (remainingTime <= 0) { | ||
| // Delete any record that might exist with the same key. | ||
| exports.del(key); |
There was a problem hiding this comment.
this seems weird to silently ignore things that already exist. I'd throw by default and add a tri-state option if people want to skip duplicates, overwrite, or the default of throw.
There was a problem hiding this comment.
Actually this is deleting the key rather than ignoring it. If you import a key that has expired, it will cause the key to be removed from the existing cache, as would have happened automatically.
It's a good idea to add an option to skip duplicates instead of overwriting -- I'll do that soon. I don't think throwing makes sense though, since it's a perfectly valid key, it's just supposed to have expired. Thoughts?
|
@ptarjan thanks for the feedback, it's updated now. |
|
@ptarjan ping :) |
|
👍 |
|
Sorry to ask this, but #85 submitted before you. Can you rebase on it and match the style they introduced? Thanks. |
|
@ptarjan done! |

Awesome library, love the unit tests! I'm using it for caching web response data in a mobile app, and I find import/export to be valuable for saving to disk and restoring the cache later (for example, if you kill the app and then open it again, restore the offline data in case there's no internet anymore).
exportJson = function()
importJson = function(json: string, options: { skipDuplicates: boolean })
exportinto the cacheimportwill remain in the cacheskipDuplicatesistrueoptions:skipDuplicates: Iftrue, any duplicate keys will be ignored when importing them. Defaults tofalse.