-
Notifications
You must be signed in to change notification settings - Fork 60
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
Comments
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;
}
});
}); |
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;
}
});
});
|
Change |
Added the recommended 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;
}
});
}); |
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. |
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:
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. |
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. |
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
Is there a way I can configure it to not have a ttl at all in conjunction with redis? I tried |
No... no TTL was never a use case. It will require some adjustments. If you need it, open a new issue requesting it. |
@jawsome ... There's an open issue: 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.) |
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? |
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 So YAR + Catbox Memory store works. I just couldn't get YAR + Catbox Mongo store to work. |
+1 |
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. |
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:
If anyone can post a complete working code example here that shows how all these pieces should fit together that would be great. |
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 |
Great! I totally missed the 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);
}); |
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. |
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:
The text was updated successfully, but these errors were encountered: