question

marcospaulis avatar image
marcospaulis asked

Trying to use cached catalog in cloudscript

Hey!

I´m trying to use a cached catalog for some cloudscript calls, buto for some reason is not working. My code to obtain the cached catalog is this:

function getCachedPlayfabCatalog() {
    if (typeof getCachedPlayfabCatalog.catalogItems == 'undefined') {
        var getCatalogItemsResponse = server.GetCatalogItems({CatalogVersion:catalog});
        getCachedPlayfabCatalog.catalogItems = getCatalogItemsResponse.Catalog;
    }
    return getCachedPlayfabCatalog.catalogItems;
};

It´s something similar to explained in this post: https://community.playfab.com/questions/28524/cloud-script-staticvalue-to-improve-speed.html

But, when I log the object "getCachedPlayfabCatalog.catalogItems", it never has a value (after the getCachedPlayfabCatalog function), so it seems that the cache (or the object) is not static between calls.

Do you know what can be the problem?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

There is no definition of the variable catalog that on line 3 in your function, so it would return JavascriptException error because of “catalog is not defined”. The type of CatalogVersion field in the Server API GetCatalogItems should be a string, so you need to define the catalog using a string, such as “MainCatalog”. Or as the API reference – CatalogVersion says, you can let this API use the default CatalogVersion if you don’t pass this field. You can refer to the following testing code.

var catalog = "Main";//Modify it to your catalogVersion
handlers.getCatalogItems = function (args, context) {
  var catalogItems = getCachedPlayfabCatalog();
  return catalogItems;
}

function getCachedPlayfabCatalog() {
  if (typeof getCachedPlayfabCatalog.catalogItems == 'undefined') {
    var getCatalogItemsResponse = server.GetCatalogItems({ CatalogVersion: catalog });
    getCachedPlayfabCatalog.catalogItems = getCatalogItemsResponse.Catalog;
  }
  return getCachedPlayfabCatalog.catalogItems;
};

Besides, we would suggest you use an external REST API testing tool Postman to test PlayFab API. It is a convenient way to check the error details of all APIs. You can use this tool to test the ExecuteCloudScript API to check the error of your CloudScript functions.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

marcospaulis avatar image marcospaulis commented ·

Hey Sarah! Sorry, i didn´t see the answer before.

About the CatalogVersion, i didn´t put in the code snippet, but we are actually using a variable called "catalog" (with the catalog string), to make that call. So, that´s why we don´t have any JavascrptException.

The thing is, as i tested the function multiple times (in our "dev" title, with low/none traffic), not always keeps the cached catalog (like 1/10 times uses a cached catalog, rest of the calls needs to get catalog again). We are trying to reduce cloudscript use of data, so ¿is this a good method to keep cloudscript with low resource consumption? I mean, I don´t know if the cache save makes data consumption or can affect to performance, and that ratio of successful use (1/10) makes it worth.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.