-
Notifications
You must be signed in to change notification settings - Fork 0
/
store function.gs
115 lines (101 loc) · 3.29 KB
/
store function.gs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Update and return data from cache and proerty store effeciently.
* obj - if update = true, then key/pair values will be updated in cache and properties
- if update =false, then store will be search for keys. values passed will be returned when stored == null
- special case: string 'ALL' returns all values in prop store
* update - bool. should store be updated with passed values
* props - required. property store to use. document, user, script
* cache - optional. cache to use. document, user, script
* secs - number of seconds to cache data for
* lock - optional. lock to use. document,user,script
*/
function store(obj,update,props,cache,secs,lock){
function tryParse(obj){
var keys = Object.keys(obj);
for(var i = 0;i<keys.length;i++){
try{
var val = obj[keys[i]];
try{
val = JSON.parse(obj[keys[i]]);
}catch(e){}
obj[keys[i]] = val;
}catch(err){
throw new Error("TP"+i);
}
}
return obj;
}
secs = secs && !isNaN(secs) ? Math.max(1,Math.min(21600,secs)) : 21600;
if(lock!=null && lock!=undefined && lock!="" && lock !=false){
var success = lock.tryLock(secs); //lock.waitLock(secs);
}
if(obj=="ALL"){
var stored = props.getProperties();
update==false;
if(cache){
CacheService.getDocumentCache().putAll(stored,secs);
}
if(lock!=null && lock!=undefined && lock!="" && lock !=false){
try{
if(lock.hasLock()){
lock.releaseLock();
}
}catch(e){}
}
return tryParse(stored);
}else if(obj && typeof obj=="object"){
update = update && ( (!isNaN(update) && update>=1) || update==true ) ? true : false;
if(update){
var allProps = props.getProperties(),
updatedKeys = Object.keys(obj);
for(var i in updatedKeys){
var nKey = updatedKeys[i];
allProps[nKey] = JSON.stringify(obj[nKey]);
}
if(cache){
cache.putAll(allProps,secs);
}
props.setProperties(allProps,false);
if(lock!=null && lock!=undefined && lock!="" && lock !=false){
try{
if(lock.hasLock()){
lock.releaseLock();
}
}catch(e){}
}
return tryParse(obj);
}else{
var rKeys = obj ? Object.keys(obj) : [];
var cached = cache ? cache.getAll(rKeys) : {};
if(Object.keys(cached).sort().join() == Object.keys(rKeys).sort().join()){
if(lock!=null && lock!=undefined && lock!="" && lock !=false){
try{
if(lock.hasLock()){
lock.releaseLock();
}
}catch(e){}
}
return cached;
}else{
var allProps = props.getProperties();
if(cache){
cache.putAll(allProps,secs);
}
for(var j in rKeys){
var key = rKeys[j];
obj[key] = allProps[key]!=null ? allProps[key] : obj[key];
}
if(lock!=null && lock!=undefined && lock!="" && lock !=false){
try{
if(lock.hasLock()){
lock.releaseLock();
}
}catch(e){}
}
return tryParse(obj);
}
}
}else{
throw new Error("object must be defined with type object");
}
}