question

John Peterson avatar image
John Peterson asked

Use of cached objects in CloudScript?

Title ID: 44B1

Dear PlayFab community,

I am wondering how some of the Title-wide data management server API calls are managed. For example, the GetTitleData() or GetCatalogItems() will return relatively static data (in our case). Would it be worthwhile to cache this data with some sort of expiry? Or maybe the backing objects are already cached in some way (and refreshed when changes are made)? I had thought to potentially cache the data locally, and avoid the API call except to refresh.

Thoughts?

apisCloudScript
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

·
brendan avatar image
brendan answered

Yes, all title-level data is both sharded and cached, for efficiency. If you additionally want to cache that data for Cloud Script calls, there are two options:

1. Just add it into the script as static data. If it's a static value in the script, you don't even need to call GetTitleData.

2. Use a global cache to store the data. We spin up and down servers to manage your Cloud Script calls based upon usage. If you're checking a globally defined data store, you would only make the call to GetTitleData once after any given logic server spins up, saving time on all subsequent calls (though that first call will always have to be made).

4 comments
10 |1200

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

John Peterson avatar image John Peterson commented ·

Thanks, Brendan -- that's great to know! Do you happen to have any code examples of #2 handy (preferably in TypeScript) that I could copy?

0 Likes 0 ·
brendan avatar image brendan John Peterson commented ·

Sure, it could be something as simple as this:

function dataCache() {
  if( typeof dataCache.staticValue == 'undefined' ) {
    var request = {
      "Keys": [ "foo" ]
    };
    dataCache.staticValue = JSON.parse( server.GetTitleData( request ).Data[ "foo" ] );
  }
}


handlers.Test = function (args, context) {
  var data = dataCache();
};

If you put logging in before and after the dataCache() call, what you'll see is that when you're re-using the same engine that previously ran (and which hasn't shut down yet), the value of dataCache.staticValue is still there.

2 Likes 2 ·
John Peterson avatar image John Peterson brendan commented ·

Thanks, Brendan! Now I'm on the lookout for a simple in-memory cache that does a time-to-live so that I can quickly expire my entries while testing. ;-)

Surprisingly, that's hard to find in TypeScript. :-(

0 Likes 0 ·
Show more comments

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.