hoangnd25 / cacheJS

Small lib for storing contextual cache

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gracefully exceeding localStorage limit

devinivy opened this issue · comments

When using localStorage and the storage quota is exceeded, QuotaExceededError is thrown. Instead, perhaps it could fail gracefully or actually empty some items from the cache to fit the new item. Any thoughts?

I think a good solution would be implementing simple LRU. I also think it should be disabled by default as some data might not be used frequently, but is very important and can't not be deleted. Another way could be adding a parameter in add function that specify the cache can't be deleted even it's not used frequently.

Do you have any thought on this?

I agree that LRU would be a good way to do this! I just hope that implementing LRU doesn't require much space in loacalStorage. Perhaps LRU should be implemented per-page-load so that the LRU implementation doesn't require using any localStorage at all. I agree that some items should be able to be marked for non-deletion. Any ideas for a simple temporary fix?

var keys = Object.keys(localStorage),
  zize = 0,
  oneMB = 1024 * 1024 * 1,
  num = localStorage.length;

while (num--) {
  switch (keys[num]) {
    case 'key':
    case 'length':
    case 'getItem':
    case 'setItem':
    case 'removeItem':
    case 'clear':
        continue;
  }
  zize += localStorage[keys[num]].length;
}
if (zize >= oneMB) {
  // note the user
}