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

Do not cache empty responses #113

Closed JobaDiniz closed 10 years ago

JobaDiniz commented 10 years ago

How can I tell angular cache to not cache empty responses?

jmdobry commented 10 years ago

Example with DS.find:

DS.find('post', 5, { cacheResponse: false}).then(function (post) { if (!angular.isEmpty(post)) { DS.inject('post', post); // do something with post } else { // do something with empty response } });

Is your API returning an empty response when angular-data is expecting a 404?

On Tue, Jun 3, 2014 at 7:04 AM, Joberto Diniz notifications@github.com wrote:

How can I tell angular cache to not cache empty responses?

— Reply to this email directly or view it on GitHub https://github.com/jmdobry/angular-cache/issues/113.

Jason Dobry 801.369.6248 jason.dobry@gmail.com

NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

JobaDiniz commented 10 years ago

I'm not using the new DS version. I liked the simplicity of the old angular-cache.

var contentCache = $angularCacheFactory('contentCache', {
        maxAge: 2 * 60 * 1000,
        onExpire: function (key, value) {
            $http.get(key).success(function (data) {
                contentCache.put(key, data);
            });
        }
    });
$http.get(url, {cache:contentCache}).success(function (data, status) {
                deferred.resolve(data);
            }).error(function (data, status) {
                deferred.reject(data, status);
            })

Sometimes, the response will be empty because I pass a query which returns no result. But I'd like to not cache it, because caching empty results is not a good practice.. may lead to some bugs real hard to catch later on.

jmdobry commented 10 years ago

To get more control over what gets cached and what doesn't, you would need to not pass the cache option to the $http request, but instead manually decide whether to cache the item as you inspect the response.

The first and second examples found here are what you're doing now, the third example shows how you can have more control over what gets cached.

jmdobry commented 10 years ago

p.s. This question is probably better suited for the mailing list.