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

offer option to store data at localstorage #26

Closed atian25 closed 11 years ago

jmdobry commented 11 years ago

What is your use case? How would you expect angular-cache to behave if the browser doesn't support localStorage?

atian25 commented 11 years ago

I'm writing hybrid app which support localstorage. I don't think angular-cache must embed localstorage code, but just offer some interface to sync between cache and localstorage, such as:

  1. cache add/remove/clear/timeout event
  2. or offer a setCache() interface
atian25 commented 11 years ago

my use case is :

jmdobry commented 11 years ago

Sounds reasonable enough, and local storage isn't actually that much code. How would you expect angular-cache to behave if a user hits your app in a browser that doesn't support local storage?

atian25 commented 11 years ago

I don't care about that...

because my use case is mobile app, it's well support ( http://caniuse.com/#feat=namevalue-storage). and it's hybrid app using webview, so doesn't use private browsing mode. and I don't think fall back to cookie is a good idea, it can only store 4k but slow down the request.

If browser doesn't support, then just using current memory cache or just offer some interface for developers to provide a special cache factory. and maybe some add/remove/clear/timeout event

jmdobry commented 11 years ago

Alright, this weekend I should have some time to work on this. If the browser doesn't support localStorage then I'll let the dev provide a localStorage polyfill to angular-cache, or rely on in-memory storage only.

jmdobry commented 11 years ago
jmdobry commented 11 years ago

@atian25 I've got the implementation done and the unit tests written. If you'd like, I would appreciate it if you tried out the version of angular-cache in the feature-localStorage branch to see how the localStorage feature works for you. I just need to update the docs and it's done.

Usage:

$angularCacheFactory('myCache', { storageMode: 'localStorage' });

Note It's the developer's responsibility to provide a polyfill for the global localStorage and sessionStorage objects if the browser does not support them.

atian25 commented 11 years ago

thanks for your work! I will try out it.

another new question: could I use specail storageMode without override gloabal localStorage?

this issue due to a bad news: one of my workmate, he said there's some localstorage compatibility issues with android webview (such as can't share localstorage between webviews, or lost data...)

so if it is true, I'm afraid we had to write a java interface to provide a specail storageMode, then export it to a javascript interface JSBridge.cache.setItem() ( it will provide functions as same as localstorage), but will not override the gloabal localStorage.

atian25 commented 11 years ago

after a quick code review, could we just set config.storage to my JSBridge.cache ?

jmdobry commented 11 years ago

I will allow the following:

$angularCacheFactory('newCache', {
  storageMode: 'localStorage',
  localStorage: myLocalStorageImpl // Can be set to your own localStorage. Defaults to window.localStorage
  sessionStorage: mySessionStorageImpl // Can be set to your own sessionStorage. Defaults to window.sessionStorage
});

Your implementations will of course have to implement to same interface as the global localStorage and sessionStorage variables.

I will work on it tomorrow, as it is bedtime here.