Very very confused about CacheService ...

214 views
Skip to first unread message

ChiefChippy2 is awesome

unread,
Apr 5, 2019, 4:00:38 AM4/5/19
to Google Apps Script Community
This is very weird, as I can't tell why this happens.
Basically :
On my PC I have 2 accounts, a gSuite account and a personal one.
My PERSONAL account is my PRIMARY ( default ) account.
It is the opposite for my friend.
I made a script where CacheService.getUserCache() as well as OAuth Login are required.
Since we are in the same gSuite, we can both use the Oauth Login which is limited to our domain only.
The Login had no problem, BUT when I tried to put CacheService.getUserCache().put("Example","Good")
and retrieve it just after ( in the next line )
console.log(CacheService.getUserCache().get("Example"))
It logged null for me, but "Good" for my friend.
What is happening?
I don't understand...
(Background info - My gSuite account owns the script, my friend and my personal account has editing access)

Also - I can't share the code publicly, sorry.

Adam Morris

unread,
Apr 5, 2019, 4:29:13 AM4/5/19
to google-apps-sc...@googlegroups.com
So you are both logging in as the same user, and seeing different behaviour with the user cache service? 

If you are expecting the data to always return "Good" no matter what, then you should be using PropertiesService, not cache. Or maybe you're just trying to understand how this can happen?

Caches by concept cannot be assumed to return the information sent via put. It can expire at any time (return null). Google may be taking advantage of that specification by queuing the put request which hasn't been acted upon yet, which is why it's returning null. The backend servers haven't stored it yet in time for the next statement. (This is just an informed guess, though.)

Test by changing to PropertiesService and see if you get the expected result every time. If so, then hopefully the explanation above holds some water. :-)

Adam

————————————————————————————

Adam Morris | IT Systems & English Teacher | IGB International School
Jalan Sierramas Utama, Sierramas,
47000 Sungai Buloh, Selangor DE, Malaysia

t    +60 3 6145 4688
f    +60 3 6145 4600
w   www.igbis.edu.my
e    adam....@igbis.edu.my

————————————————————————————


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/92ac7c15-4026-4d1f-b83a-207b6d34401d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Morris

unread,
Apr 5, 2019, 4:49:55 AM4/5/19
to google-apps-sc...@googlegroups.com
Hmmm, well I just tested by looping:

function myFunction() {
  var i, store, prop = 'weird';
  i = 1;
  store = CacheService.getUserCache();  
  store.put(prop, 'wow');

  while (store.get(prop) === null) {
    Logger.log(i);
    i += 1;
  }
  Logger.log(i);
  Logger.log(store.get(prop));
}

Given what I said just now I was expecting the loop counter to be in the single digits but alas it returned "wow" on the first attempt. Can't help without your code.


————————————————————————————

Adam Morris | IT Systems & English Teacher | IGB International School
Jalan Sierramas Utama, Sierramas,
47000 Sungai Buloh, Selangor DE, Malaysia

t    +60 3 6145 4688
f    +60 3 6145 4600
w   www.igbis.edu.my
e    adam....@igbis.edu.my

————————————————————————————

Adam Morris

unread,
Apr 5, 2019, 6:23:16 AM4/5/19
to google-apps-sc...@googlegroups.com
Finally, you have to be running as the same user *and* the same script. As the documentation states, it is scoped to the script/user. In other words, if you're running as the same user but different scripts, not even PropertyServices shares that state.

Riël Notermans

unread,
Apr 6, 2019, 6:53:39 AM4/6/19
to Google Apps Script Community
Be sure you are -really- running the same accounts here.

Op vr 5 apr. 2019 12:23 schreef Adam Morris <adam....@igbis.edu.my>:
Reply all
Reply to author
Forward
0 new messages