Project

General

Profile

« Previous | Next » 

Revision 49707

Commit a first draft of caching service for redis and memcache

View differences:

modules/uoa-services-portal/trunk/services/cache/mecache/package.json
1
{
2
  "name": "server-side-cache-with-express",
3
  "main": "server.js",
4
  "scripts": {
5
    "start": "node cache.js"
6
  },
7
  "dependencies": {
8
    "express": "^4.15.3",
9
    "memory-cache": "^0.2.0",
10
    "superagent": "^3.8.0"
11
  },
12
  "engines": {
13
    "node": "8.1.x"
14
  },
15
  "license": "MIT"
16
}
modules/uoa-services-portal/trunk/services/cache/mecache/cache.js
1
'use strict'
2

  
3
var express = require('express');
4
var app = express();
5
var mcache = require('memory-cache');
6
const request = require('superagent');
7

  
8
var cache = (duration) => {
9
  return (req, res, next) => {
10
    let key = '__express__' + req.originalUrl || req.url
11
    let cachedBody = mcache.get(key)
12
    if (cachedBody) {
13
      res.send(cachedBody)
14
      return
15
    } else {
16
      res.sendResponse = res.send
17
      res.send = (body) => {
18
        mcache.put(key, body, duration * 1000);
19
        res.sendResponse(body)
20
      }
21
      next()
22
    }
23
  }
24
}
25

  
26

  
27
app.get('/get',cache(10), (req, res) => {
28
  setTimeout(() => {
29
    const url = (req.query)?req.query.url:null;
30
    if (!url){
31
        res.status(404).send('Not Found'); //not found
32

  
33
    }else{
34
     request.get(url, function (err, response) {
35
          if (err) throw err;
36

  
37
          // response.body contains an array of public repositories
38
          // var repoNumber = response.body.length;
39
          res.send(response);
40
    })
41
  }
42
})
43
});
44

  
45
app.use((req, res) => {
46
  res.status(404).send('Not Found') //not found
47
})
48

  
49
app.listen(process.env.PORT, function () {
50
  console.log(`Example app listening on port ${process.env.PORT}!`)
51
})
modules/uoa-services-portal/trunk/services/cache/mecache/npm-debug.log
1
0 info it worked if it ends with ok
2
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
3
2 info using npm@4.1.2
4
3 info using node@v7.6.0
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`
16
14 verbose stack Exit status 1
17
14 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16)
18
14 verbose stack     at emitTwo (events.js:106:13)
19
14 verbose stack     at EventEmitter.emit (events.js:192:7)
20
14 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
21
14 verbose stack     at emitTwo (events.js:106:13)
22
14 verbose stack     at ChildProcess.emit (events.js:192:7)
23
14 verbose stack     at maybeClose (internal/child_process.js:890:16)
24
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
25
15 verbose pkgid server-side-cache-with-express@
26
16 verbose cwd /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/mecache
27
17 error Linux 4.4.0-97-generic
28
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
29
19 error node v7.6.0
30
20 error npm  v4.1.2
31
21 error code ELIFECYCLE
32
22 error server-side-cache-with-express@ start: `node cache.js`
33
22 error Exit status 1
34
23 error Failed at the server-side-cache-with-express@ start script 'node cache.js'.
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,
37
23 error not with npm itself.
38
23 error Tell the author that this fails on your system:
39
23 error     node cache.js
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
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
44
23 error There is likely additional logging output above.
45
24 verbose exit [ 1, true ]
modules/uoa-services-portal/trunk/services/cache/redis/package.json
1
{
2
  "name": "redis-caching-tutorial",
3
  "version": "1.0.0",
4
  "description": "Introduction to caching with Redis",
5
  "main": "index.js",
6
  "scripts": {
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"
10
  },
11
  "author": "Argiro Kokogiannaki <argirok@di.uoa.gr>",
12
  "license": "NKUA",
13
  "dependencies": {
14
    "express": "^4.14.0",
15
    "redis": "^2.6.3",
16
    "superagent": "^2.3.0"
17
  }
18
}
modules/uoa-services-portal/trunk/services/cache/redis/cache.js
1
const redis = require('redis');
2
const express = require('express');
3
const request = require('superagent');
4
const PORT = process.env.PORT;
5
const REDIS_PORT = process.env.REDIS_PORT;
6

  
7
const app = express();
8
const client = redis.createClient(REDIS_PORT);
9

  
10
function respond(org, numberOfRepos) {
11
    return `Organization "${org}" has ${numberOfRepos} public repositories.`;
12
}
13

  
14
function cache(req, res, next) {
15
    const url = req.query.url;
16
    client.get(url, function (err, data) {
17
        if (data != null) {
18
            res.send(respond(url, data));
19
        } else {
20
            next();
21
        }
22
    });
23
}
24

  
25
// function getNumberOfRepos(req, res, next) {
26
//     const org = req.query.org;
27
//     request.get(`https://api.github.com/orgs/${org}/repos`, function (err, response) {
28
//         if (err) {
29
//             throw err;
30
//         }
31
//
32
//         var repoNumber = 0;
33
//         if (response && response.body) {
34
//             repoNumber = response.body.length;
35
//         }
36
//         client.setex(org, 5, repoNumber);
37
//         res.send(respond(org, repoNumber));
38
//     });
39
// }
40
//
41
// app.get('/repos', cache, getNumberOfRepos);
42
app.get('/get',cache, (req, res) => {
43
  setTimeout(() => {
44
    const url = (req.query)?req.query.url:null;
45
    if (!url){
46
        res.status(404).send('Not Found'); //not found
47

  
48
    }else{
49
     request.get(url, function (err, response) {
50
          if (err) throw err;
51

  
52
          // response.body contains an array of public repositories
53
          // var repoNumber = response.body.length;
54
          res.send(response);
55
    })
56
  }
57
})
58
});
59
app.listen(PORT, function () {
60
    console.log('app listening on port', PORT);
61
});
modules/uoa-services-portal/trunk/services/cache/redis/npm-debug.log
1
0 info it worked if it ends with ok
2
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
3
2 info using npm@4.1.2
4
3 info using node@v7.6.0
5
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
6
5 info lifecycle redis-caching-tutorial@1.0.0~prestart: redis-caching-tutorial@1.0.0
7
6 silly lifecycle redis-caching-tutorial@1.0.0~prestart: no script for prestart, continuing
8
7 info lifecycle redis-caching-tutorial@1.0.0~start: redis-caching-tutorial@1.0.0
9
8 verbose lifecycle redis-caching-tutorial@1.0.0~start: unsafe-perm in lifecycle true
10
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
11
10 verbose lifecycle redis-caching-tutorial@1.0.0~start: CWD: /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/redis
12
11 silly lifecycle redis-caching-tutorial@1.0.0~start: Args: [ '-c', 'PORT=3000 REDIS_PORT=7777 node cache.js' ]
13
12 silly lifecycle redis-caching-tutorial@1.0.0~start: Returned: code: 1  signal: null
14
13 info lifecycle redis-caching-tutorial@1.0.0~start: Failed to exec start script
15
14 verbose stack Error: redis-caching-tutorial@1.0.0 start: `PORT=3000 REDIS_PORT=7777 node cache.js`
16
14 verbose stack Exit status 1
17
14 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16)
18
14 verbose stack     at emitTwo (events.js:106:13)
19
14 verbose stack     at EventEmitter.emit (events.js:192:7)
20
14 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
21
14 verbose stack     at emitTwo (events.js:106:13)
22
14 verbose stack     at ChildProcess.emit (events.js:192:7)
23
14 verbose stack     at maybeClose (internal/child_process.js:890:16)
24
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
25
15 verbose pkgid redis-caching-tutorial@1.0.0
26
16 verbose cwd /home/argirok/projects/openaire/uoa-services-portal/trunk/services/cache/redis
27
17 error Linux 4.4.0-97-generic
28
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
29
19 error node v7.6.0
30
20 error npm  v4.1.2
31
21 error code ELIFECYCLE
32
22 error redis-caching-tutorial@1.0.0 start: `PORT=3000 REDIS_PORT=7777 node cache.js`
33
22 error Exit status 1
34
23 error Failed at the redis-caching-tutorial@1.0.0 start script 'PORT=3000 REDIS_PORT=7777 node cache.js'.
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 redis-caching-tutorial package,
37
23 error not with npm itself.
38
23 error Tell the author that this fails on your system:
39
23 error     PORT=3000 REDIS_PORT=7777 node cache.js
40
23 error You can get information on how to open an issue for this project with:
41
23 error     npm bugs redis-caching-tutorial
42
23 error Or if that isn't available, you can get their info via:
43
23 error     npm owner ls redis-caching-tutorial
44
23 error There is likely additional logging output above.
45
24 verbose exit [ 1, true ]

Also available in: Unified diff