How to use Node.js connect-memcached middleware

There is a simple example on how to use connect-memcached .

var express      = require('express')
, session        = require('express-session')
, cookieParser   = require('cookie-parser')
, http           = require('http')
, app            = express()
, MemcachedStore = require('connect-memcached')(session);

app.use(cookieParser());
app.use(session({
      secret  : 'CatOnKeyboard'
    , key     : 'test'
    , proxy   : 'true'
    , store   : new MemcachedStore({
        hosts: ['127.0.0.1:11211']
    })
}));

app.get('/', function(req, res){
    if(req.session.views) {
        ++req.session.views;
    } else {
        req.session.views = 1;
    }
    res.send('Viewed <strong>' + req.session.views + '</strong> times.');
});

      

This way we can use Memcached to store our session. But in this example, I doubt it is storing a Memcached session (without using Memcached code). when do we call the req.session.views = 1;

value views

stored in Memcached right now? or is it not stored at all? can anyone explain this to me? or just teach me how to set up and get a session using Memcached here.

+3


source to share


1 answer


No, this is saved in the session for every call made by the server. But to check if the session is the same and show the correct value, the server needs to check the session while memcached is running. Check the code if you have views in your session and increment 1 every time you call it.



0


source







All Articles