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

Put operation does not seem to be synchronous #232

Closed krystalcode closed 8 years ago

krystalcode commented 8 years ago

I'm using the following piece of code when a user signs in on the application.

cache.put('/user', user);
$state.go('home');

The home page requires the user object and in order to avoid an extra API request for an object that I already have, I'm taking the user from the cache in the HomeController.

$scope.user = cache.get('/user');

However, even though I read that angular-cache is synchronous, it seems that the put operation is not. Angular directs to the new state, but the user is not available in the cache yet - the get operation returns 'undefined'. If I refresh the page, the user is available. That indicates that writing the object in the cache had not finished before the state change.

Why is that? Is there any way to make the put operation really synchronous? e.g. the code does not further execute until the object is put in the cache. Otherwise, would using a promise work, based on something like https://github.com/jmdobry/angular-cache/issues/123.

krystalcode commented 8 years ago

I have tried adding a few seconds of delay before redirecting to the new state, but still the user is undefined in the cache. So I don't think it's a matter of redirecting before it's written in the cache, something else is going on. I'll investigate a bit more.

krystalcode commented 8 years ago

Apologies, my bad, something to do with Satellizer returning the data in different format than I was expecting and the user was not found and never really put into the cache.