forked from Thanapon99/inventory-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (118 loc) · 3.58 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
const path = require("path");
const express = require("express");
const redis = require("redis");
const mcache = require("memory-cache");
const flatCache = require("flat-cache");
const Memcached = require("memcached");
const sqlite3 = require("sqlite3").verbose();
const PORT = process.env.PORT || 3128;
const app = express();
// --------------------------------------------------------
// Configuring In-Memory Cache
// --------------------------------------------------------
let memCache = new mcache.Cache();
let cacheMiddleware = duration => {
return (req, res, next) => {
let key = "__express__" + req.originalUrl || req.url;
let cacheContent = memCache.get(key);
if (cacheContent) {
res.send(cacheContent);
// console.log('reading from cache');
return;
} else {
res.sendResponse = res.send;
res.send = body => {
memCache.put(key, body, duration * 1000);
res.sendResponse(body);
};
// console.log('storing in cache');
next();
}
};
};
// --------------------------------------------------------
// Configuring Flat Cache Middleware for File Caching
// --------------------------------------------------------
let cache = flatCache.load("productsCache", path.resolve("./cache"));
let flatCacheMiddleware = (req, res, next) => {
let key = "__express__" + req.originalUrl || req.url;
let cacheContent = cache.getKey(key);
if (cacheContent) {
res.send(cacheContent);
return;
} else {
res.sendResponse = res.send;
res.send = body => {
cache.setKey(key, body);
cache.save();
res.sendResponse(body);
};
next();
}
};
// --------------------------------------------------------
// Using the MemCached Service for File Caching
// --------------------------------------------------------
let memcached = new Memcached("127.0.0.1:11211");
let memcachedMiddleware = duration => {
return (req, res, next) => {
let key = "__expreeedddddss__" + req.originalUrl || req.url;
memcached.get(key, function(err, data) {
if (data) {
res.send(data);
return;
} else {
res.sendResponse = res.send;
res.send = body => {
memcached.set(key, body, duration * 60, function(err) {
//
});
res.sendResponse(body);
};
next();
}
});
};
};
// --------------------------------------------------------
// Configuring Redis Middleware
// --------------------------------------------------------
const client = redis.createClient();
let redisMiddleware = (req, res, next) => {
let key = "__express__" + req.originalUrl || req.url;
client.get(key, function(err, reply) {
if (reply) {
res.send(reply);
return;
} else {
res.sendResponse = res.send;
res.send = body => {
client.set(key, JSON.stringify(body));
res.sendResponse(body);
};
next();
}
});
};
// --------------------------------------------------------
// create app routes
// --------------------------------------------------------
app.get("/products", memcachedMiddleware(20), function(req, res) {
setTimeout(() => {
let db = new sqlite3.Database("./NodeInventory.db");
let sql = `SELECT * FROM products`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
db.close();
res.send(rows);
});
}, 3000);
});
// --------------------------------------------------------
// Set appliication port
// --------------------------------------------------------
app.listen(PORT, function() {
console.log(`App running on port ${PORT}`);
});