-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (136 loc) · 3.66 KB
/
index.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
/*!
* SQLCacheDB
*
* Copyright(c) 2018 Bradford Knowlton
* MIT Licensed
*
* Version 1.2.1
*/
'use strict';
var sqlite3 = require('sqlite3');
var cacheDb = new sqlite3.Database( __dirname + '/data/database.db');
// default age for active cache entries
var cacheLifetime = '-6 hours';
exports.sqlCacheDb = function(){
// Create the database table if it doesn't exist
cacheDb.serialize(function() {
cacheDb.run('CREATE TABLE IF NOT EXISTS "cache" ( `ID` INTEGER PRIMARY KEY AUTOINCREMENT, `key` TEXT UNIQUE, `data` TEXT, `date_created` datetime default current_timestamp, `date_updated` datetime default current_timestamp );');
});
};
/**
* getCache
* Gets the cache data based on a key.
*
* @param {string} key Value for lookup in database.
* @param {Function} callback function name for callback
*/
exports.getCache = function(key, callback){
// query database for data based on key with date within lifetime
cacheDb.serialize(function() {
cacheDb.get('SELECT `key`, `data` FROM `cache` WHERE `key` == (?) AND date_updated > date("now", ?)', key, cacheLifetime, function(err,row){
if( err || row == undefined ){
callback(err,null);
}else{
callback(err,row.data);
}
});
});
};
/**
* setCache
* Sets the cache data based on a key.
*
* @param {string} key Value for lookup in database.
* @param {string} data Value to be stored in cache.
* @param {Function} callback function name for callback
*/
exports.setCache = function(key, data, callback){
cacheDb.serialize(function() {
var stmt = cacheDb.prepare('INSERT OR REPLACE INTO `cache` (`ID`,`key`,`data`,`date_created`,`date_updated`) VALUES ( (SELECT `ID` FROM `cache` WHERE `key` == (?)), (?), (?), COALESCE((SELECT `date_created` FROM `cache` WHERE `key` == (?)), datetime("now") ), datetime("now") );');
stmt.run( key, key, data, key );
stmt.finalize();
callback();
});
};
/**
* purgeCache
* removes all keys from cache
*
* @param {Function} callback function name for callback
*/
exports.purgeCache = function(callback){
cacheDb.serialize(function() {
cacheDb.run('DELETE FROM `cache`', function(){
callback();
});
});
};
/**
* purgeKey
* removes a key from cache
*
* @param {string} key Value for lookup in database.
* @param {Function} callback function name for callback
*/
exports.purgeKey = function(key,callback){
cacheDb.serialize(function() {
cacheDb.run('DELETE FROM `cache` WHERE `key` == ?', key, function(){
callback();
});
});
};
/**
* cleanCache
* removes all keys from cache which are expired
*
* @param {Function} callback function name for callback
*/
exports.cleanCache = function(callback){
cacheDb.serialize(function() {
cacheDb.run('DELETE FROM `cache` WHERE date_updated < datetime("now", ?)', cacheLifetime, function(){
callback();
});
});
};
/**
* getKeys
* Gets all the keys in the cache.
*
* @param {Function} callback function name for callback
*/
exports.getKeys = function(callback){
var keys = [];
cacheDb.serialize(function() {
cacheDb.all('SELECT key FROM `cache`', function(err, rows) {
if ( err ){
callback(err, null);
}else{
rows.forEach(function(row){
keys.push(row.key);
});
}
callback(err,keys);
});
});
};
/**
* getActiveKeys
* Gets all the keys in the cache recently set.
*
* @param {Function} callback function name for callback
*/
exports.getActiveKeys = function(callback){
var keys = [];
cacheDb.serialize(function() {
cacheDb.all('SELECT key FROM `cache` WHERE date_updated > datetime("now", ?)', cacheLifetime, function(err, rows) {
if ( err ){
callback(err, null);
}else{
rows.forEach(function(row){
keys.push(row.key);
});
}
callback(err,keys);
});
});
};