Some questions about Cacheservice

322 views
Skip to first unread message

Jose Pe Linares

unread,
Mar 25, 2019, 6:22:40 AM3/25/19
to Google Apps Script Community
Hi All!!

I am making a quiz on a web app script (executing like me option) and i am storing user credentials (a custom login-pass) on cacheservice with start time and the idtest (i can get it with a doget function like a parameter).
Question is this..., the quiz can be long, sometines more than two hours and i am afraid the cache could be removed "The specified expiration time is only a suggestion; cached data may be removed before this time if a lot of data is cached."
I could make a second user login to retrieve user credentials and the start time could be retrieved storing it in the html client side (this could be hacked but not so important), but idtest info (got by doget function) is more important and dont want to expose it to the hack possibility.

Some thougths about this?. In your experience... is cacheservice often removed after expiration date? ( i am no storing so much data)

Thank for your help!!

Alan Wells

unread,
Mar 25, 2019, 8:21:22 AM3/25/19
to Google Apps Script Community
I went to the documentation and found the quote about data being removed from cache if there is a lot of data.  That is a very strange statement.  And it has no supporting information about what a "lot" of data is.  I think that it may be the case that if you try to put more data into a property than the limit, that your data could be truncated without any warning or indication of what happened.  But, you stated that you aren't storing much data, although you didn't quantify what "much data" is.

The maximum allowed expiration is 21600 seconds (6 hours), so you should be able to cache data for two and a half hours for your 2 hour test.  But time isn't the only consideration.  Like Properties Service, Cache Service has
  • Document
  • Script
  • User
Cache.  You'd need to do a simple calculation to estimate the total cache that you'll be using:

Max Number of users times amount of data per user = Total amount that will be stored

There are multiple different possible situations:
  • All using ONE document - Every student using the same Form
  • All using ONE script - The script is bound to the Form, and all users are using the same Form
  • Each user using a different Form - You generate an new copy of the Form for every student and every student using a different Form
  • Each user using their own Script (Add-on) - The student installs an Add-on and the add-on script uses quota from their account
If you are using User cache, then the quotas go against each users account.  But that would mean that the user would need to be logged in with a Google account.  If the Form is being used by an anonymous user, then I'm assuming that User Cache isn't available.

If every student must log in with a Google account, then I'd store the data in User Cache, and set the expiration to 9000 seconds.

Eric Koleda

unread,
Mar 25, 2019, 8:36:43 AM3/25/19
to google-apps-sc...@googlegroups.com
To step back from the particulars, the CacheService should be used to store a temporary copy of data that is permanently stored elsewhere. The CacheService makes it easier and faster to retrieve data, but it does not guarantee data integrity. Use PropertiesService or an external data store (like Firebase) to store a permanent copy of your data.

- Eric

--
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/1d41d3b2-d53b-4fce-a4cb-0bdb9a1f1b40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alan Wells

unread,
Mar 25, 2019, 8:58:47 AM3/25/19
to Google Apps Script Community
Good point.  Basically, I use cache to reduce the number of service calls, which can increase speed, and reduce the chances for hitting a short term quota limit.  If you want to make sure that student answers to questions get saved, don't store them in cache for long periods of time.

Alan Wells

unread,
Mar 25, 2019, 9:13:18 AM3/25/19
to Google Apps Script Community
I just realized that it seems like you are using a Web App and not a Google Form.  So, in that case, there is no Document properties for Cache.  I'm not sure if the way that the Web App is published affects how the Script Cache is allocated to what account.  Even if the script is executing as the user, then the Script properties must be saved to the project that you own, because the user doesn't have a copy of the project file.

Adam Morris

unread,
Mar 25, 2019, 9:41:02 AM3/25/19
to google-apps-sc...@googlegroups.com
It's worth taking a high-altitude look at what a cache is. When you understand what it is, you won't be surprised by that statement.

A cache is a bank of memory that is higher performing than persistent storage. Similar in concept to RAM vs hard drive. But the trade off with that high performance, is that there is less space available. That is why a cache can be flushed (removed) at any time. If you put in more data than it can hold (given various quotas), it has to freed elsewhere. Your code should assume that it might be flushed, and then retrieved if it was:

// pseudocode:
result = checkCacheForDataWhichIsFast(id);
if (result) return result;
return checkStoreForDataWhichIsSlow(id);

So, if your use case for storing data is inconsistent with being flushed, then don't use a cache; use PropertyService instead. The most common use case for cache is when there is a store somewhere, but is kinda slow, and having the cache available speeds it up.

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

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

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


--
Reply all
Reply to author
Forward
0 new messages