-
Notifications
You must be signed in to change notification settings - Fork 8
/
ticaching.js
178 lines (133 loc) · 3.8 KB
/
ticaching.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* TiCaching
*
* @author Gustavo Rodriguez Baldera
* @link https://github.com/gbaldera/TiCaching
* @version 1.0
*/
var TiCaching = function() {
//database params
this.dbname = 'cache';
this.database = Titanium.Database.open(this.dbname);
//Controlling backups to iCloud
if ((Ti.Platform.osname === 'iphone') || (Ti.Platform.osname === 'ipad')) {
Ti.API.log('Controlling backups to iCloud.');
this.database.file.setRemoteBackup(false);
}
this.database.execute('PRAGMA read_uncommitted=true');
//create table
this.database.execute('CREATE TABLE IF NOT EXISTS cache (id INTEGER PRIMARY KEY AUTOINCREMENT, key VARCHAR UNIQUE, time INTEGER, data TEXT)');
/**
* Returns the data that was previously cached under the given key. If the key is not found or
* the given time for the data has expired it returns false.
*
* @param {string} key The key that identify the cached data.
*
* @returns {Object|boolean} The cached data.
*/
this.get = function(key) {
if ( typeof key == 'undefined') {
return false;
}
var res = this.database.execute('SELECT * from cache WHERE key = ? LIMIT 1', key), ret = {};
while (res.isValidRow()) {
ret = {
time : res.fieldByName('time'),
data : JSON.parse(res.fieldByName('data'))
};
res.next();
}
res.close();
//check if the time is still valid
if ((typeof ret.time == 'undefined') || (this.time() > ret.time)) {
Ti.API.log('No valid time: ' + ret.time + ' != ' + this.time());
return false;
}
return ret.data;
};
/**
* Cache the given data for the number of seconds specified.
*
* @param {string} key The key to identify the data.
* @param {object|array} data The data to be cached.
* @param {number} time The number of seconds to keep the data cached before expires.
*
* @returns {boolean}
*/
this.save = function(key, data, time) {
if ( typeof key == 'undefined') {
Ti.API.error('Key is undefined.');
return false;
}
data = ( typeof data == 'undefined' ? '' : JSON.stringify(data));
//30 seconds default
time = ( typeof time == 'undefined' ? (30 + this.time()) : (time + this.time() ));
try {
Ti.API.log('Saving Key.');
return this.database.execute('INSERT OR REPLACE INTO cache (key,data,time) VALUES (?,?,?)', key, data, time);
} catch(e) {
Ti.API.error(e);
}
};
/**
* Remove the cached data associated with the given key.
*
* @param {string} key The key that identify the cached data.
*
* @returns {boolean}
*/
this.del = function(key) {
if ( typeof key == 'undefined') {
return false;
}
try {
Ti.API.log('Deleting key: ' + key);
return this.database.execute('DELETE FROM cache WHERE key=?', key);
} catch(e) {
Ti.API.error(e);
}
};
/**
* Remove all cached data, optionally only the expired ones.
*
* @param {boolean} Expired data only.
*
* @returns {boolean}
*/
this.clear = function(expired_only) {
var q = 'DELETE FROM cache';
if(expired_only === true){
q += ' WHERE time < ' + this.time();
}
try {
Ti.API.log('Clearing data..');
return this.database.execute(q);
} catch(e) {
Ti.API.error(e);
}
};
/**
* Checks if the given key exists in the cache.
*
* @param {string} key The key that identify the cached data.
*
* @returns {boolean} Whether the key exists or not.
*/
this.exists = function(key) {
var rows = this.database.execute('SELECT id FROM cache WHERE key=?', key),
no = (rows.rowCount > 0);
rows.close();
return no;
};
/**
* Returns the current Unix timestamp measured in seconds.
*
* Taken from: http://phpjs.org/functions/time
*
* @returns {Number} The current Unix timestamp.
*/
this.time = function() {
return Math.floor(new Date().getTime() / 1000);
};
};
module.exports = new TiCaching();