Revision 49714
Added by Argiro Kokogiannaki over 6 years ago
modules/uoa-services-portal/trunk/services/cache/mecache/package.json | ||
---|---|---|
1 | 1 |
{ |
2 |
"name": "server-side-cache-with-express", |
|
3 |
"main": "server.js", |
|
2 |
"name": "memcache-cache", |
|
3 |
"version": "1.0.0", |
|
4 |
"description": "Caching in memory", |
|
5 |
"main": "cache.js", |
|
4 | 6 |
"scripts": { |
5 |
"start": "node cache.js" |
|
7 |
"start": "PORT=3000 node cache.js"
|
|
6 | 8 |
}, |
7 | 9 |
"dependencies": { |
10 |
"cors": "^2.8.4", |
|
8 | 11 |
"express": "^4.15.3", |
9 | 12 |
"memory-cache": "^0.2.0", |
10 | 13 |
"superagent": "^3.8.0" |
... | ... | |
12 | 15 |
"engines": { |
13 | 16 |
"node": "8.1.x" |
14 | 17 |
}, |
15 |
"license": "MIT" |
|
18 |
"author": "Argiro Kokogiannaki <argirok@di.uoa.gr>", |
|
19 |
"license": "NKUA" |
|
16 | 20 |
} |
modules/uoa-services-portal/trunk/services/cache/mecache/cache.js | ||
---|---|---|
5 | 5 |
var mcache = require('memory-cache'); |
6 | 6 |
const request = require('superagent'); |
7 | 7 |
|
8 |
var cors = require('cors') |
|
9 |
app.use(cors()) |
|
10 |
|
|
8 | 11 |
var cache = (duration) => { |
9 | 12 |
return (req, res, next) => { |
13 |
|
|
10 | 14 |
let key = '__express__' + req.originalUrl || req.url |
11 | 15 |
let cachedBody = mcache.get(key) |
12 | 16 |
if (cachedBody) { |
13 |
res.send(cachedBody) |
|
17 |
|
|
18 |
res.send(JSON.parse(cachedBody)) |
|
14 | 19 |
return |
15 | 20 |
} else { |
16 | 21 |
res.sendResponse = res.send |
17 | 22 |
res.send = (body) => { |
18 | 23 |
mcache.put(key, body, duration * 1000); |
19 | 24 |
res.sendResponse(body) |
25 |
|
|
20 | 26 |
} |
21 | 27 |
next() |
22 | 28 |
} |
... | ... | |
24 | 30 |
} |
25 | 31 |
|
26 | 32 |
|
27 |
app.get('/get',cache(10), (req, res) => { |
|
33 |
app.get('/get',cache(10), cors(), (req, res) => {
|
|
28 | 34 |
setTimeout(() => { |
29 | 35 |
const url = (req.query)?req.query.url:null; |
30 | 36 |
if (!url){ |
... | ... | |
32 | 38 |
|
33 | 39 |
}else{ |
34 | 40 |
request.get(url, function (err, response) { |
41 |
// res.header("Access-Control-Allow-Origin", "http://localhost:3000"); |
|
42 |
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length"); |
|
43 |
res.header("Access-Control-Allow-Methods", "GET, OPTIONS"); |
|
44 |
res.header("Access-Control-Allow-Methods", "GET, OPTIONS"); |
|
45 |
res.header("Content-Type","application/json"); |
|
46 |
|
|
35 | 47 |
if (err) throw err; |
36 |
|
|
37 |
// response.body contains an array of public repositories |
|
38 |
// var repoNumber = response.body.length; |
|
39 |
res.send(response); |
|
48 |
res.send(response.body); |
|
40 | 49 |
}) |
41 | 50 |
} |
42 | 51 |
}) |
modules/uoa-services-portal/trunk/services/cache/mecache/npm-debug.log | ||
---|---|---|
3 | 3 |
2 info using npm@4.1.2 |
4 | 4 |
3 info using node@v7.6.0 |
5 | 5 |
4 verbose run-script [ 'prestart', 'start', 'poststart' ] |
6 |
5 info lifecycle server-side-cache-with-express@~prestart: server-side-cache-with-express@
|
|
7 |
6 silly lifecycle server-side-cache-with-express@~prestart: no script for prestart, continuing
|
|
8 |
7 info lifecycle server-side-cache-with-express@~start: server-side-cache-with-express@
|
|
9 |
8 verbose lifecycle server-side-cache-with-express@~start: unsafe-perm in lifecycle true
|
|
10 |
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
|
|
11 |
10 verbose lifecycle server-side-cache-with-express@~start: CWD: /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache
|
|
12 |
11 silly lifecycle server-side-cache-with-express@~start: Args: [ '-c', 'node cache.js' ]
|
|
13 |
12 silly lifecycle server-side-cache-with-express@~start: Returned: code: 1 signal: null
|
|
14 |
13 info lifecycle server-side-cache-with-express@~start: Failed to exec start script
|
|
15 |
14 verbose stack Error: server-side-cache-with-express@ start: `node cache.js`
|
|
6 |
5 info lifecycle memcache-cache@1.0.0~prestart: memcache-cache@1.0.0
|
|
7 |
6 silly lifecycle memcache-cache@1.0.0~prestart: no script for prestart, continuing
|
|
8 |
7 info lifecycle memcache-cache@1.0.0~start: memcache-cache@1.0.0
|
|
9 |
8 verbose lifecycle memcache-cache@1.0.0~start: unsafe-perm in lifecycle true
|
|
10 |
9 verbose lifecycle memcache-cache@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/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
|
|
11 |
10 verbose lifecycle memcache-cache@1.0.0~start: CWD: /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache
|
|
12 |
11 silly lifecycle memcache-cache@1.0.0~start: Args: [ '-c', 'PORT=3000 node cache.js' ]
|
|
13 |
12 silly lifecycle memcache-cache@1.0.0~start: Returned: code: 1 signal: null
|
|
14 |
13 info lifecycle memcache-cache@1.0.0~start: Failed to exec start script
|
|
15 |
14 verbose stack Error: memcache-cache@1.0.0 start: `PORT=3000 node cache.js`
|
|
16 | 16 |
14 verbose stack Exit status 1 |
17 | 17 |
14 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16) |
18 | 18 |
14 verbose stack at emitTwo (events.js:106:13) |
... | ... | |
22 | 22 |
14 verbose stack at ChildProcess.emit (events.js:192:7) |
23 | 23 |
14 verbose stack at maybeClose (internal/child_process.js:890:16) |
24 | 24 |
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) |
25 |
15 verbose pkgid server-side-cache-with-express@
|
|
25 |
15 verbose pkgid memcache-cache@1.0.0
|
|
26 | 26 |
16 verbose cwd /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache |
27 | 27 |
17 error Linux 4.4.0-97-generic |
28 | 28 |
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start" |
29 | 29 |
19 error node v7.6.0 |
30 | 30 |
20 error npm v4.1.2 |
31 | 31 |
21 error code ELIFECYCLE |
32 |
22 error server-side-cache-with-express@ start: `node cache.js`
|
|
32 |
22 error memcache-cache@1.0.0 start: `PORT=3000 node cache.js`
|
|
33 | 33 |
22 error Exit status 1 |
34 |
23 error Failed at the server-side-cache-with-express@ start script 'node cache.js'.
|
|
34 |
23 error Failed at the memcache-cache@1.0.0 start script 'PORT=3000 node cache.js'.
|
|
35 | 35 |
23 error Make sure you have the latest version of node.js and npm installed. |
36 |
23 error If you do, this is most likely a problem with the server-side-cache-with-express package,
|
|
36 |
23 error If you do, this is most likely a problem with the memcache-cache package,
|
|
37 | 37 |
23 error not with npm itself. |
38 | 38 |
23 error Tell the author that this fails on your system: |
39 |
23 error node cache.js |
|
39 |
23 error PORT=3000 node cache.js
|
|
40 | 40 |
23 error You can get information on how to open an issue for this project with: |
41 |
23 error npm bugs server-side-cache-with-express
|
|
41 |
23 error npm bugs memcache-cache
|
|
42 | 42 |
23 error Or if that isn't available, you can get their info via: |
43 |
23 error npm owner ls server-side-cache-with-express
|
|
43 |
23 error npm owner ls memcache-cache
|
|
44 | 44 |
23 error There is likely additional logging output above. |
45 | 45 |
24 verbose exit [ 1, true ] |
modules/uoa-services-portal/trunk/services/cache/redis/package.json | ||
---|---|---|
1 | 1 |
{ |
2 |
"name": "redis-caching-tutorial",
|
|
2 |
"name": "redis-cache",
|
|
3 | 3 |
"version": "1.0.0", |
4 |
"description": "Introduction to caching with Redis",
|
|
5 |
"main": "index.js",
|
|
4 |
"description": "Caching with Redis",
|
|
5 |
"main": "cache.js",
|
|
6 | 6 |
"scripts": { |
7 | 7 |
"test": "echo \"Error: no test specified\" && exit 1", |
8 |
"start": "PORT=3000 REDIS_PORT=7777 node cache.js",
|
|
9 |
"dev": "PORT=3000 REDIS_PORT=7777 node --debug=5858 index.js"
|
|
8 |
"start": "PORT=3000 REDIS_PORT=6379 REDIS_HOST=vatopedi.di.uoa.gr node cache.js",
|
|
9 |
"dev": "PORT=3000 REDIS_PORT=6379 REDIS_HOST=vatopedi.di.uoa.gr node --debug=5858 index.js"
|
|
10 | 10 |
}, |
11 | 11 |
"author": "Argiro Kokogiannaki <argirok@di.uoa.gr>", |
12 | 12 |
"license": "NKUA", |
modules/uoa-services-portal/trunk/services/cache/redis/cache.js | ||
---|---|---|
3 | 3 |
const request = require('superagent'); |
4 | 4 |
const PORT = process.env.PORT; |
5 | 5 |
const REDIS_PORT = process.env.REDIS_PORT; |
6 |
const REDIS_HOST = process.env.REDIS_HOST; |
|
6 | 7 |
|
7 | 8 |
const app = express(); |
8 |
const client = redis.createClient(REDIS_PORT); |
|
9 |
const client = redis.createClient(REDIS_PORT, REDIS_HOST);
|
|
9 | 10 |
|
10 |
function respond(org, numberOfRepos) { |
|
11 |
return `Organization "${org}" has ${numberOfRepos} public repositories.`; |
|
12 |
} |
|
11 |
// function respond(org, numberOfRepos) {
|
|
12 |
// return `Organization "${org}" has ${numberOfRepos} public repositories.`;
|
|
13 |
// }
|
|
13 | 14 |
|
14 | 15 |
function cache(req, res, next) { |
15 | 16 |
const url = req.query.url; |
16 | 17 |
client.get(url, function (err, data) { |
17 | 18 |
if (data != null) { |
18 |
res.send(respond(url, data));
|
|
19 |
res.send(data);
|
|
19 | 20 |
} else { |
20 | 21 |
next(); |
21 | 22 |
} |
... | ... | |
51 | 52 |
|
52 | 53 |
// response.body contains an array of public repositories |
53 | 54 |
// var repoNumber = response.body.length; |
54 |
res.send(response); |
|
55 |
res.send(response.body);
|
|
55 | 56 |
}) |
56 | 57 |
} |
57 | 58 |
}) |
Also available in: Unified diff
Add host for redis, get proper answer, add cors