Commit a first draft of caching service for redis and memcache
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@49707 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
5a1ac15f50
commit
c668c7368b
|
@ -0,0 +1,51 @@
|
|||
'use strict'
|
||||
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var mcache = require('memory-cache');
|
||||
const request = require('superagent');
|
||||
|
||||
var cache = (duration) => {
|
||||
return (req, res, next) => {
|
||||
let key = '__express__' + req.originalUrl || req.url
|
||||
let cachedBody = mcache.get(key)
|
||||
if (cachedBody) {
|
||||
res.send(cachedBody)
|
||||
return
|
||||
} else {
|
||||
res.sendResponse = res.send
|
||||
res.send = (body) => {
|
||||
mcache.put(key, body, duration * 1000);
|
||||
res.sendResponse(body)
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
app.get('/get',cache(10), (req, res) => {
|
||||
setTimeout(() => {
|
||||
const url = (req.query)?req.query.url:null;
|
||||
if (!url){
|
||||
res.status(404).send('Not Found'); //not found
|
||||
|
||||
}else{
|
||||
request.get(url, function (err, response) {
|
||||
if (err) throw err;
|
||||
|
||||
// response.body contains an array of public repositories
|
||||
// var repoNumber = response.body.length;
|
||||
res.send(response);
|
||||
})
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(404).send('Not Found') //not found
|
||||
})
|
||||
|
||||
app.listen(process.env.PORT, function () {
|
||||
console.log(`Example app listening on port ${process.env.PORT}!`)
|
||||
})
|
|
@ -0,0 +1,45 @@
|
|||
0 info it worked if it ends with ok
|
||||
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
|
||||
2 info using npm@4.1.2
|
||||
3 info using node@v7.6.0
|
||||
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
|
||||
5 info lifecycle server-side-cache-with-express@~prestart: server-side-cache-with-express@
|
||||
6 silly lifecycle server-side-cache-with-express@~prestart: no script for prestart, continuing
|
||||
7 info lifecycle server-side-cache-with-express@~start: server-side-cache-with-express@
|
||||
8 verbose lifecycle server-side-cache-with-express@~start: unsafe-perm in lifecycle true
|
||||
9 verbose lifecycle server-side-cache-with-express@~start: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache/node_modules/.bin:/home/argirok/bin:/home/argirok/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
|
||||
10 verbose lifecycle server-side-cache-with-express@~start: CWD: /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache
|
||||
11 silly lifecycle server-side-cache-with-express@~start: Args: [ '-c', 'node cache.js' ]
|
||||
12 silly lifecycle server-side-cache-with-express@~start: Returned: code: 1 signal: null
|
||||
13 info lifecycle server-side-cache-with-express@~start: Failed to exec start script
|
||||
14 verbose stack Error: server-side-cache-with-express@ start: `node cache.js`
|
||||
14 verbose stack Exit status 1
|
||||
14 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16)
|
||||
14 verbose stack at emitTwo (events.js:106:13)
|
||||
14 verbose stack at EventEmitter.emit (events.js:192:7)
|
||||
14 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
|
||||
14 verbose stack at emitTwo (events.js:106:13)
|
||||
14 verbose stack at ChildProcess.emit (events.js:192:7)
|
||||
14 verbose stack at maybeClose (internal/child_process.js:890:16)
|
||||
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
|
||||
15 verbose pkgid server-side-cache-with-express@
|
||||
16 verbose cwd /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache
|
||||
17 error Linux 4.4.0-97-generic
|
||||
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
|
||||
19 error node v7.6.0
|
||||
20 error npm v4.1.2
|
||||
21 error code ELIFECYCLE
|
||||
22 error server-side-cache-with-express@ start: `node cache.js`
|
||||
22 error Exit status 1
|
||||
23 error Failed at the server-side-cache-with-express@ start script 'node cache.js'.
|
||||
23 error Make sure you have the latest version of node.js and npm installed.
|
||||
23 error If you do, this is most likely a problem with the server-side-cache-with-express package,
|
||||
23 error not with npm itself.
|
||||
23 error Tell the author that this fails on your system:
|
||||
23 error node cache.js
|
||||
23 error You can get information on how to open an issue for this project with:
|
||||
23 error npm bugs server-side-cache-with-express
|
||||
23 error Or if that isn't available, you can get their info via:
|
||||
23 error npm owner ls server-side-cache-with-express
|
||||
23 error There is likely additional logging output above.
|
||||
24 verbose exit [ 1, true ]
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "server-side-cache-with-express",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node cache.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.15.3",
|
||||
"memory-cache": "^0.2.0",
|
||||
"superagent": "^3.8.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "8.1.x"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
const redis = require('redis');
|
||||
const express = require('express');
|
||||
const request = require('superagent');
|
||||
const PORT = process.env.PORT;
|
||||
const REDIS_PORT = process.env.REDIS_PORT;
|
||||
|
||||
const app = express();
|
||||
const client = redis.createClient(REDIS_PORT);
|
||||
|
||||
function respond(org, numberOfRepos) {
|
||||
return `Organization "${org}" has ${numberOfRepos} public repositories.`;
|
||||
}
|
||||
|
||||
function cache(req, res, next) {
|
||||
const url = req.query.url;
|
||||
client.get(url, function (err, data) {
|
||||
if (data != null) {
|
||||
res.send(respond(url, data));
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// function getNumberOfRepos(req, res, next) {
|
||||
// const org = req.query.org;
|
||||
// request.get(`https://api.github.com/orgs/${org}/repos`, function (err, response) {
|
||||
// if (err) {
|
||||
// throw err;
|
||||
// }
|
||||
//
|
||||
// var repoNumber = 0;
|
||||
// if (response && response.body) {
|
||||
// repoNumber = response.body.length;
|
||||
// }
|
||||
// client.setex(org, 5, repoNumber);
|
||||
// res.send(respond(org, repoNumber));
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// app.get('/repos', cache, getNumberOfRepos);
|
||||
app.get('/get',cache, (req, res) => {
|
||||
setTimeout(() => {
|
||||
const url = (req.query)?req.query.url:null;
|
||||
if (!url){
|
||||
res.status(404).send('Not Found'); //not found
|
||||
|
||||
}else{
|
||||
request.get(url, function (err, response) {
|
||||
if (err) throw err;
|
||||
|
||||
// response.body contains an array of public repositories
|
||||
// var repoNumber = response.body.length;
|
||||
res.send(response);
|
||||
})
|
||||
}
|
||||
})
|
||||
});
|
||||
app.listen(PORT, function () {
|
||||
console.log('app listening on port', PORT);
|
||||
});
|
|
@ -0,0 +1,45 @@
|
|||
0 info it worked if it ends with ok
|
||||
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
|
||||
2 info using npm@4.1.2
|
||||
3 info using node@v7.6.0
|
||||
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
|
||||
5 info lifecycle redis-caching-tutorial@1.0.0~prestart: redis-caching-tutorial@1.0.0
|
||||
6 silly lifecycle redis-caching-tutorial@1.0.0~prestart: no script for prestart, continuing
|
||||
7 info lifecycle redis-caching-tutorial@1.0.0~start: redis-caching-tutorial@1.0.0
|
||||
8 verbose lifecycle redis-caching-tutorial@1.0.0~start: unsafe-perm in lifecycle true
|
||||
9 verbose lifecycle redis-caching-tutorial@1.0.0~start: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/redis/node_modules/.bin:/home/argirok/bin:/home/argirok/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
|
||||
10 verbose lifecycle redis-caching-tutorial@1.0.0~start: CWD: /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/redis
|
||||
11 silly lifecycle redis-caching-tutorial@1.0.0~start: Args: [ '-c', 'PORT=3000 REDIS_PORT=7777 node cache.js' ]
|
||||
12 silly lifecycle redis-caching-tutorial@1.0.0~start: Returned: code: 1 signal: null
|
||||
13 info lifecycle redis-caching-tutorial@1.0.0~start: Failed to exec start script
|
||||
14 verbose stack Error: redis-caching-tutorial@1.0.0 start: `PORT=3000 REDIS_PORT=7777 node cache.js`
|
||||
14 verbose stack Exit status 1
|
||||
14 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16)
|
||||
14 verbose stack at emitTwo (events.js:106:13)
|
||||
14 verbose stack at EventEmitter.emit (events.js:192:7)
|
||||
14 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
|
||||
14 verbose stack at emitTwo (events.js:106:13)
|
||||
14 verbose stack at ChildProcess.emit (events.js:192:7)
|
||||
14 verbose stack at maybeClose (internal/child_process.js:890:16)
|
||||
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
|
||||
15 verbose pkgid redis-caching-tutorial@1.0.0
|
||||
16 verbose cwd /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/redis
|
||||
17 error Linux 4.4.0-97-generic
|
||||
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
|
||||
19 error node v7.6.0
|
||||
20 error npm v4.1.2
|
||||
21 error code ELIFECYCLE
|
||||
22 error redis-caching-tutorial@1.0.0 start: `PORT=3000 REDIS_PORT=7777 node cache.js`
|
||||
22 error Exit status 1
|
||||
23 error Failed at the redis-caching-tutorial@1.0.0 start script 'PORT=3000 REDIS_PORT=7777 node cache.js'.
|
||||
23 error Make sure you have the latest version of node.js and npm installed.
|
||||
23 error If you do, this is most likely a problem with the redis-caching-tutorial package,
|
||||
23 error not with npm itself.
|
||||
23 error Tell the author that this fails on your system:
|
||||
23 error PORT=3000 REDIS_PORT=7777 node cache.js
|
||||
23 error You can get information on how to open an issue for this project with:
|
||||
23 error npm bugs redis-caching-tutorial
|
||||
23 error Or if that isn't available, you can get their info via:
|
||||
23 error npm owner ls redis-caching-tutorial
|
||||
23 error There is likely additional logging output above.
|
||||
24 verbose exit [ 1, true ]
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "redis-caching-tutorial",
|
||||
"version": "1.0.0",
|
||||
"description": "Introduction to caching with Redis",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "PORT=3000 REDIS_PORT=7777 node cache.js",
|
||||
"dev": "PORT=3000 REDIS_PORT=7777 node --debug=5858 index.js"
|
||||
},
|
||||
"author": "Argiro Kokogiannaki <argirok@di.uoa.gr>",
|
||||
"license": "NKUA",
|
||||
"dependencies": {
|
||||
"express": "^4.14.0",
|
||||
"redis": "^2.6.3",
|
||||
"superagent": "^2.3.0"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue