cacheJS icon indicating copy to clipboard operation
cacheJS copied to clipboard

Gracefully exceeding localStorage limit

Open devinivy opened this issue 10 years ago • 4 comments
trafficstars

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?

devinivy avatar Feb 03 '15 15:02 devinivy

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?

hoangnd25 avatar Feb 03 '15 22:02 hoangnd25

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?

devinivy avatar Feb 04 '15 15:02 devinivy

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
}

ghost avatar Jun 19 '16 18:06 ghost