Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yar + catbox (redis) #24

Closed
ndrut opened this issue Jan 9, 2014 · 18 comments
Closed

yar + catbox (redis) #24

ndrut opened this issue Jan 9, 2014 · 18 comments
Labels
support Questions, discussions, and general support

Comments

@ndrut
Copy link

ndrut commented Jan 9, 2014

Re: https://twitter.com/hapijs/status/421216281866407937

I'm not encountering any errors, it's just not loading into redis.

This is being sent over:

var Catbox = require('catbox');

var client = new Catbox.Client({

  engine: 'redis',
  partition: 'dERP-session',
  host: '127.0.0.1',
  port: 6379,
  password: ''

}); 

config = {
  session: {

    store: client.start(function () {
      return new Catbox.Policy({ expiresIn: 14400 }, client, 'dERP');
    }),
    cookieOptions: {

      password: 'FR9EtT]gA6&B~q4',
      isSecure: false

    }

  }
var server = new Hapi.Server(config.server.port, config.server.options);

server.pack.allow({ ext: true }).require('yar', config.session, function (err) {

    if (err) {
        console.log(err)
        throw err;
    }
});
@hueniverse
Copy link
Contributor

Try:

var Catbox = require('catbox');

var client = new Catbox.Client({
  engine: 'redis',
  partition: 'dERP-session',
  host: '127.0.0.1',
  port: 6379,
  password: ''
}); 

config = {
  session: {
    store: new Catbox.Policy({ expiresIn: 14400 }, client, 'dERP')
  }),
  cookieOptions: {
    password: 'FR9EtT]gA6&B~q4',
    isSecure: false
  }
}

var server = new Hapi.Server(config.server.port, config.server.options);

client.start(function () {
  server.pack.allow({ ext: true }).require('yar', config.session, function (err) {

      if (err) {
          console.log(err)
          throw err;
      }
  });
});

@ndrut
Copy link
Author

ndrut commented Jan 9, 2014

Sorry for the nastiness of that, I was getting desperately creative. I'm still not receiving any errors upon loading it up. But, the session is not being stored in redis.

Using the code you provided.

var Hapi = require('hapi');
var Catbox = require('catbox');

var client = new Catbox.Client({
  engine: 'redis',
  partition: 'dERP-session',
  host: '127.0.0.1',
  port: 6379,
  password: ''
});

var config = {
  server: {
    host: '127.0.0.1',
    port: 9000,
    options: {
    }
  },
  session: {
    store: new Catbox.Policy({ expiresIn: 14400 }, client, 'dERP'),
    cookieOptions: {
      password: 'FR9EtT]gA6&B~q4',
      isSecure: false
    }
  }
};

var server = new Hapi.Server(config.server.port, config.server.options);
client.start(function () {
  console.log('Client started');
  server.pack.allow({ ext: true }).require('yar', config.session, function (err) {
      console.log('Pack allow');
      if (err) {
          console.log(err)
          throw err;
      }
  });
});
server started on port:  9000
Client started
Pack allow
$ redis-cli 
redis 127.0.0.1:6379> keys *
1) "hubot:storage"
redis 127.0.0.1:6379> 

@hueniverse
Copy link
Contributor

Change client.start(function() {}) to client.start(function (err) {}} and check for errors there.

@ndrut
Copy link
Author

ndrut commented Jan 9, 2014

Added the recommended err. Returns undefined, which I assume is due to no errors being encountered.

client.start(function (err) {
  console.log(err);
  console.log('Client started');
  server.pack.allow({ ext: true }).require('yar', config.session, function (err) {
      console.log('Pack allow!');
      if (err) {
          console.log(err)
          throw err;
      }
  });
});

@hueniverse
Copy link
Contributor

Can you try just creating a catbox client and policy, then setting some keys directly and checking? Also, try creating a memory catbox and see if that works.

@ndrut
Copy link
Author

ndrut commented Jan 12, 2014

Thanks so much for your help this far.

I've attempted to create just a standard catbox client:

var Catbox = require('catbox');

var options = {
  engine: 'redis',
  partition: 'testingcatbox',
}

var catbox = new Catbox.Client(options);

catbox.start(function (err) {
  if (err) throw err;
  catbox.set({ segment: 'idkwtfthisis', id: 'test1' }, 'value1', 14400, function (err) {
    if (err) throw err;
  });
  catbox.get({ segment: 'idkwtfthisis', id: 'test1' }, function (err, cached) {
    if (err) throw err;
    console.log(cached);
    catbox.stop();
  });
});

When this is run, I receive the expected value. However, when I check redis to see what is currently present, I find there's no data. I was also able to replicate this by commenting the .set lines and re-executing it returns null by the .get.

Setting a policy up works for me with the following:

var Catbox = require('catbox');

var options = {
  engine: 'redis',
  partition: 'testingcatbox',
}

var catbox = new Catbox.Client(options);

catbox.start(function (err) {
  if (err) throw err;
  var policy = new Catbox.Policy({ expiresIn: 600 }, catbox, 'segment');
  policy.set('test3', 'value3', 14400, function (err) {
    if (err) throw err;
    catbox.stop();
  });
});

Proof of operation:

[derp@jedi project]$ node catbox-policy.js 
[derp@jedi project]$ redis-cli 
redis 127.0.0.1:6379> keys *
1) "testingcatbox:segment:test3"
redis 127.0.0.1:6379> get testingcatbox:segment:test3
"{\"item\":\"value3\",\"stored\":1389503258716,\"ttl\":14400}"

I'm also positive the memory storage is working due to sessions persisting for the life of the hapi.js processes.

Please advise on what you recommend to try/look into next.

@hueniverse
Copy link
Contributor

So both redis tests work but the first one doesn't show up in redis? I'm confused. Also, try setting much longer expiration. Last - note that the proof is from the first test with the 14400 ttl, not the second which is far too fast for you to be able to see at 600ms.

@ndrut
Copy link
Author

ndrut commented Jan 12, 2014

Oh wow, yeah. I guess I overlooked that those were ms and not seconds. Trying that now. Also, I was basing the proof on the key and the presence of "value3" rather than "value1" as the value - sorry for any confusion.

Raised to 100000

[derp@jedi project]$ node cattest.js 
{ item: 'value1', stored: 1389510807301, ttl: 99987 }
[derp@jedi project]$ redis-cli 
redis 127.0.0.1:6379> keys *
1) "testingcatbox:idkwtfthisis:test1"

Is there a way I can configure it to not have a ttl at all in conjunction with redis? I tried 0, -1, null all to no avail.

@hueniverse
Copy link
Contributor

No... no TTL was never a use case. It will require some adjustments. If you need it, open a new issue requesting it.

@findesk
Copy link

findesk commented Jan 18, 2014

@jawsome ...

There's an open issue:

#18

which indicates Yar store is not working with Catbox. Yar is using session.id as the Catbox key, yet Catbox requires getters/setters cache key to be an object, i.e. { segment: 'somesegment', id: 'somecacheid' }.

Were you able to get Yar with Catbox working in the end? (not just standalone Catbox for general caching)

(I stumbled on this when I set Yar maxCookieSize: 0, to force Yar to go to the store.)

@ndrut
Copy link
Author

ndrut commented Jan 19, 2014

I had actually been on a little bit of a break - Resuming picking this up. Upgraded to latest on all the libs and still having issues. Catbox works fine, even with the options I'm providing. The issue only seems to be with yar/hapi.

@hueniverse You able to take another look at this?

@findesk
Copy link

findesk commented Jan 20, 2014

Fyi...I looked into this more...my comment above is incorrect.

I don't think this is an issue now: #18

I wasn't seeing that Catbox Policy is creating the cache key.

I tested YAR with Catbox Memory store by setting YAR maxCookieSize: 1

YAR does indeed go to Catbox Memory store for the session cache. So YAR works with Catbox Memory store.

YAR passes request.session.id to Catbox: https://github.com/spumko/yar/blob/v0.4.0/lib/index.js#L62

With Catbox Policy actually creating the cache key:

Set: https://github.com/spumko/catbox/blob/v1.1.2/lib/policy.js#L39
Get: https://github.com/spumko/catbox/blob/v1.1.2/lib/policy.js#L63

So YAR + Catbox Memory store works. I just couldn't get YAR + Catbox Mongo store to work.

@chrisspiegl
Copy link

+1

@hueniverse
Copy link
Contributor

Simply add a mongo cache to hapi (either as the default cache or as a named cache) and tell yar to use that one via the new cache option.

@jholland918
Copy link
Contributor

I have been spinning my wheels trying to get a hapi + yar + catbox (redis or mongodb) to work storing session data too. I created a small catbox project that could write to redis and mongodb successfully as a sanity check but when I try adding all these plugins together yar is just not saving the session data to redis or mongodb. The yar plugin does save session data somewhere because my get() calls work.

Looking at yar's code I see where it does grab a Catbox.Policy on https://github.com/spumko/yar/blob/master/lib/index.js#L43 but it never gets used in the request.session.set() function here https://github.com/spumko/yar/blob/master/lib/index.js#L115, which gets called when my code executes this test handler:

server.route([
    {
        method: 'GET',
        path: '/',
        handler: function (request, reply) {
            request.session.set("example", {"me":"tester"});
            return reply('finished');
        }
    }
]);

If anyone can post a complete working code example here that shows how all these pieces should fit together that would be great.

@hueniverse
Copy link
Contributor

First, you really should use the hapi server config to setup your mongo or redis connection, not mess around with catbox directly. Second, the cache will be used only when the jar size goes above the default which is 1K. Nothing will go into the cache until you hit that 1K limit. To set a lower limit change maxCookieSize.

@jholland918
Copy link
Contributor

Great! I totally missed the maxCookieSize setting. Here's my code for other hopeless noobs.

var Hapi = require('hapi');

var serverOptions = {
    cache: {
        engine: 'catbox-mongodb'
    }
};

var server = new Hapi.Server('localhost', 8000, serverOptions);

var yarOptions = {
    // Use "0" maxCookieSize to force all session data to
    // be written to the database so you can verify
    // your database connection is working.
    maxCookieSize: 0,
    cache: {
        expiresIn: 24 * 60 * 60 * 1000
    },
    cookieOptions: {
        password: 'password',
        isSecure: false
    }
};

server.pack.require('yar', yarOptions, function (err) {

    if (err) {
        console.log(err);
        throw err;
    }
});

server.route([
    {
        method: 'GET',
        path: '/set/{key}/{value}',
        handler: function (request, reply) {

            // In MongoDB, this data will be stored in the
            // "!yar" collection within the "hapi-cache" database.
            var data = {};
            data[request.params.key] = request.params.value;
            request.session.set('example', data);
            return reply();
        }
    },
    {
        method: 'GET',
        path: '/get/{key}',
        handler: function (request, reply) {

            var example = request.session.get('example');
            reply(example[request.params.key]);
        }
    }
]);

server.start(function () {
    console.log('Server started at: ' + server.info.uri);
});

@Marsup Marsup added support Questions, discussions, and general support and removed question labels Sep 21, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

5 participants