pamelafox / lscache

A localStorage-based memcache-inspired client-side caching library.
Other
1.46k stars 160 forks source link

Bad return result #77

Closed BarbiBimbo closed 6 years ago

BarbiBimbo commented 6 years ago

When I attempt to retrieve an item that has not been set, the return result is 'undefined'. This is not a good emulation of memcache, it should instead return 'false'.

Additionally, your own demo file gives a javascript error when attempting to retrieve a value that has not been set:

Uncaught TypeError: Cannot read property 'name' of null at eval (eval at run (index.html:4), :4:33) at run (index.html:20) at HTMLButtonElement.onclick (index.html:53) (anonymous) @ VM49:4 run @ index.html:20 onclick @ index.html:53

I suggest at the least that you modify the demo file to handle this situatuion without a javascript error.

pamelafox commented 6 years ago

Hmm. In the demo, it returns "null", which is what the lscache documentation states. That's because the library calls localStorage.getItem, which defaults to returning null: https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem

Returning false would be a better replication of memcache.get(), but at this point, that'd be a potentially breaking backwards-incompatible change, so it's too late to make that change for this library.

Re the demo page: I'm not too bothered that the demo page causes an error if you get before you set, as it demonstrates the need for good data checking in your own code. You're welcome to send a pull request that adds a specific note in that section like "If not set, these values will return null beware" or some wording like that. I'll close for now, but feel free to send a PR for the demo page.