-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachePro.gs
More file actions
95 lines (75 loc) · 3.18 KB
/
Copy pathCachePro.gs
File metadata and controls
95 lines (75 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var CachePro_ = function(cacheName,expirationInSeconds,propertiesService_) {
this.id;
this.log = false;
this.name = "cache-"+cacheName;
this.expirationInSeconds = expirationInSeconds || (60 * 60); // default cache 3600s
this.propertiesService = propertiesService_;
}
CachePro_.prototype.reset = function() {
var keys = this.propertiesService.getKeys();
for (var i in keys) {
if (keys[i].indexOf("cache-") > -1) {
var obj = JSON.parse( this.propertiesService.getProperty(keys[i]) );
var file = DriveApp.getFileById(obj.fileId);
DriveApp.removeFile(file)
this.propertiesService.deleteProperty(keys[i]);
if (this.log) Logger.log("Deleted %s", keys[i]);
}
}
}
CachePro_.prototype.setLog = function() {
this.log = true;
return this;
}
CachePro_.prototype.init = function(file) {
this.id = file.getId();
var date = (new Date()).getTime() + (this.expirationInSeconds * 1000);
var json = JSON.stringify({ expiredDate : date, fileId : this.id });
this.propertiesService.setProperty(this.name, json);
}
CachePro_.prototype.getData = function(function_) {
var properties = this.propertiesService.getProperty(this.name);
if (properties === null) {
if (this.log) Logger.log("Fetch fresh data to %s and new cache file is created", this.name);
var content = function_();
var file = DriveApp.createFile(this.name, content);
this.init(file);
return file.getBlob();
} else {
var obj = JSON.parse(properties);
var remain = Math.round((new Date(+obj.expiredDate) - new Date()) / 1000);
if (remain > 0) {
if (this.log) Logger.log("Load data from cache %s , which will expire in %s seconds", this.name,remain);
try {
var file = DriveApp.getFileById(obj.fileId);
} catch (e) {
var content = function_();
var file = DriveApp.createFile(this.name, content);
this.init(file);
}
return file.getBlob();
} else {
if (this.log) Logger.log("Fetch fresh data to %s and rewrite cache file", this.name);
var content = function_();
try {
var file = DriveApp.getFileById(obj.fileId);
file.setContent(content);
} catch (e) {
var file = DriveApp.createFile(this.name, content);
}
this.init(file);
return file.getBlob();
}
}
}
/**
* Caching content into static Drive files
*
* @param {String} cacheName The cache name, which have to be unique like ID for Properties, Cache and File name
* @param {Number} expirationInSeconds How long cache will be in memory
* @param {PropertiesService} propertiesService Insert which type Properties Service do you want to use (User / Document / Script)
* @return {Blob} Returns Binary large object
*/
function cache(cacheName,expirationInSeconds,propertiesService) {
return new CachePro_(cacheName,expirationInSeconds,propertiesService);
}