简体   繁体   中英

how to use Node.js connect-memcached middleware

There is a simple example about 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.');
});

By this way, we can use Memcached to store our session. But in this sample, i doubt that how Memcached store session in it (No code operating Memcached). when we call req.session.views = 1; is the value views stored into Memcached right now? or it is not stored at all? can somebody explain this for me? or just teach me how to set and get session using Memcached here.

No, this is saved at session for every call you do to server. But to check if session is the same and show the right value, the server need to check the session, when memcached works. The get code check if have views on your session and increase 1 every time you call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM