DJ-DOO
DJ-DOO

Reputation: 4781

Parse.com Caching Cloud Query

I'm using cloud code to get multiple counts in a single call (see below):

    Parse.Cloud.define("BeerCount", function(request, response){

    var yellowCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Yellow").equalTo("userId", request.params.userid).count();
    var tanCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Tan").equalTo("userId", request.params.userid).count();
    var brownCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Brown").equalTo("userId", request.params.userid).count();
    var blackCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Black").equalTo("userId", request.params.userid).count();
    var redCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Red").equalTo("userId", request.params.userid).count();
    var orangeCount = new Parse.Query("beer_rating").equalTo("beer_colour", "Orange").equalTo("userId", request.params.userid).count();

    Parse.Promise.when(yellowCount, tanCount, brownCount, blackCount, redCount, orangeCount)
    .then(function(countOfYellow, countOfTan, countOfBrown, countOfBlack, countOfRed, countOfOrange){
        response.success({
            yellowCount: countOfYellow,
            tanCount: countOfTan,
            brownCount: countOfBrown,
            blackCount: countOfBlack,
            redCount: countOfRed,
            orangeCount: countOfOrange
        });
    });
});

Although this is quite quick on a fast wi fi connection, it would be almost impossible to carry out this when not connected to fast conneciton. I am wondering is it possible to cache this? I am using a cache policy of cache_then_network on all my android based queries and I hoped to do the same with this...if anyone knows this I'd appreciate it if you could let me know...

Upvotes: 0

Views: 592

Answers (2)

DJ-DOO
DJ-DOO

Reputation: 4781

In case anyone might have the same issues here is how I've implemented foscos suggestion

Parse.Cloud.afterSave("beer_rating", function(request) {
var colour = request.object.get("beer_colour");

var user = Parse.User.current();
var username = user.get("username");

console.log("THIS IS THE BEER COLOUR: " + colour);
console.log("User Name: "+ username);


if (colour === "Black"){
    console.log("BLACK IS MATCHED");
    user.increment("totalBlackBeers");
    user.save();
}else if(colour === "Brown"){
    console.log("BROWN IS MATCHED");
    user.increment("totalBrownBeers");
    user.save();
}else if (colour === "Tan"){
    console.log("TAN IS MATCHED");
    user.increment("totalTanBeers");
    user.save();
}else if (colour === "Yellow"){
    console.log("YELLOW IS MATCHED");
    user.increment("totalYellowBeers");
    user.save();
}else if (colour === "Orange"){
    console.log("RED IS MATCHED");
    user.increment("totalOrangeBeers");
    user.save();
}else if(colour === "Red"){
    console.log("ORANGE IS MATCHED");
    user.increment("totalRedBeers");
    user.save();
}   
});

EDIT*

I just want to add that while this cloud code above does what it's meant to the original logic doesn't work as the current user is cached and stays that way until logged out so it doesn't get any new data that may be added during that session

Upvotes: 0

Fosco
Fosco

Reputation: 38526

This is not the way to manage counts, because it will not scale (and you're already looking for caching mechanisms.) There's a hard limit on the number of count queries that can be going on at the same time, so if a good number of users joined, this would just start failing. The right way to do this is to keep counters, and increment/decrement them when other data changes.

For example, in this case, an afterSave trigger on beer_rating can update a counter on the user for the proper color beer.. an afterDelete trigger can decrement it.

With this, you don't need to count at all, a single fetch on the user object will have the counters (or you can use another object for this, but still, one object.)

Upvotes: 2

Related Questions