Cache Service expiration

452 views
Skip to first unread message

soloui...@gmail.com

unread,
May 5, 2019, 3:42:34 PM5/5/19
to Google Apps Script Community
So if I use cache service :

CacheService.getUserCache().put('example','hello',600)


That cache will expire in 600 seconds, or 10 minutes.
But what about this situation?
//Current time is 18:30
CacheService.getUserCache().put('example','hello',600)
//Cache is gonna expire in 18:40
Utilities.sleep(60000)
//1 minute of sleep later ( about 18:31 )
CacheService.getUserCache().put('example','hello')

Will this cache expire in 18:40 as expected or will it expire 1 minute later since it got updated 1 minute after?

(Basically does the expiration date resets every time you update cache by using .put()?)

aj.addons

unread,
May 5, 2019, 4:06:54 PM5/5/19
to google-apps-sc...@googlegroups.com
The expirationInSeconds is reset every time that a "put" method is used.  Any subsequent "put" executions cancel the previous setting and make a new setting.  The following code shows a property being set, and then reset with a subsequent "put" execution.  If you don't enter an expirationInSeconds then the default is 600 seconds, or 10 minutes.  So, in your example there would be a setting in cache just before 11 minutes after the first setting was made.  But it's not the same setting. Time was not added, the default of 600 seconds was used 1 minute after the first setting, and you saved the same value to the same property name.

function testCache() {
 
var scriptCache;
 
  scriptCache
= CacheService.getScriptCache();
  scriptCache
.put('example','hello',600);

 
Logger.log(scriptCache.get('example'));
 
  scriptCache
.put('example','goodbye',1);//save a different value to the same key but with only 1 second
 
Utilities.sleep(1100);////1.1 second of sleep later
 
 
Logger.log(scriptCache.get('example'));

}

[19-05-05 15:58:20:369 EDT] hello
[19-05-05 15:58:21:559 EDT] null




Reply all
Reply to author
Forward
0 new messages