jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

fix(Local Storage) Catch and manage exception when local storage size is reached #139

Closed bennekrouf closed 8 years ago

bennekrouf commented 9 years ago

"capacity" does not prevent from having a maximum size reached exception with the local storage.

Here, the idea is to catch this exception (around setItem), compute the size of the item that is provoking the exception. Then, removing from the cache oldest item until there are enough space for adding this new item.

jmdobry commented 9 years ago

I will review this.

I also wanted to point out that this may not work in all situations, as localStorage my have been filled up by something other than angular-cache. In addition, if you've created multiple cache instances, this solution can only prevent a single instance from filling up localStorage. During the attempt to free up space, only that instance will be removing old items. If other cache instance have put a bunch of stuff in localStorage, their items will remain.

bennekrouf commented 9 years ago

I agree.

Thanks for the review.

jmdobry commented 9 years ago

I can't merge this PR, I think it needs to be updated with master.

marlonpp commented 9 years ago

+1 for this. Could you explain how is the behavior when localStorage explodes? Data will not be cached anymore? It will use memory?

bennekrouf commented 9 years ago

With this PR, if the max LS size is reached during a put, I remove 30% of the cache and do the put.

We can imagine relying on memory in this case, but I think it would confuse the user by having some data in memory (flushable by F5) and others in LS... only based on technical considerations.

bdkent commented 8 years ago

was this issue ever resolved?

jmdobry commented 8 years ago

@bdkent https://github.com/jmdobry/angular-cache/issues/180#issuecomment-159095766