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

Force invalidation of one item #188

Closed JuanitoB closed 9 years ago

JuanitoB commented 9 years ago

Hello, nice work! I would like to ask you if there's any way to force the removal or refresh of one individual item. I don't know an exact expire time, but I know when the item becomes obsolete, so I want to remove it then.

Sorry, I didn't found another way to communicate with you. Thanks in advance

jmdobry commented 9 years ago

The answers to your questions should be found here. The mailing list can be found here.

Sounds like you may be looking for Cache#remove(key) or Cache#touch(key). Setting deleteOnExpire to "aggressive" will remove items from the cache when they expire.


Closing because I’m not sure this is an issue, if you are convinced that this is really a bug, please feel free to re-open the issue and add more information:

Otherwise support is done via the mailing list.

AndrewCraswell commented 8 years ago

I would like some clarification on this as well. It's currently the only question preventing me from switching to angular-cache.

When I use angular-cache with $http, I might want to force a cache refresh. How do I tell angular-cache or $http that regardless of whether a value is already cached, grab a new item and overwrite the cache?

I could definitely just call Cache.remove(), but I'm not quite sure what the key $http creates...

jmdobry commented 8 years ago

but I'm not quite sure what the key $http creates...

From here: https://docs.angularjs.org/api/ng/service/$http#caching

The cache key is the request URL including search parameters; headers are not considered.

So, just call remove on the cache, using "the request URL including search parameters" as the key.

AndrewCraswell commented 8 years ago

Awesome! I tried looking in the Angular $http docs, but I guess I'm blind. Now I'm curious how to configure $http to provide a hash for the key rather than the URL... I don't want my API endpoints to be so visible through dev tools.

Might also be worth showing an example in the documentation of of refreshing the cache before executing a request with $http. I know others have this question too. In my case, I almost passed up on angular-cache because I couldn't find anything mentioned.

jmdobry commented 8 years ago

@AndrewCraswell Go to the Readme of this repo and look for this section

Do your own caching while using the $http service:

It shows adding results to the cache yourself (as opposed to letting $http add stuff to the cache). This lets you control the keys, since you don't want your endpoints exposed in your keys.

If you have other questions, suggestions, or PRs you'd like to submit, please open a separate issue, as starting new divergent conversations on old closed issues isn't the best.