Closed atian25 closed 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:
setCache()
interfacemy use case is :
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?
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
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.
@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.
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.
after a quick code review, could we just set config.storage
to my JSBridge.cache
?
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.
What is your use case? How would you expect angular-cache to behave if the browser doesn't support localStorage?