1

I'm using Sails to add some data to Redis... It is working OK but I'm not sure how to set the EXPIRE for a key...

Im using the sails-redis adapter/connection for the model... My model looks like this

module.exports = {
    connection: 'cache',
    attributes: {
        id: {type: 'string', primaryKey: true},
        data: {type: 'string'}

    }
};

To save the model I use

Cache.create({id: "somekey", data: data}, function(err, data){})
mickey
  • 101
  • 9

1 Answers1

0

Waterline doesn't provide this functionality with the native adapter currently near as I can tell. However, it's a wee bit hacky, but you can access the redis client itself through Waterline and do it that way with the created model ID.

// Create the base model
Model.create({ field: value }).exec( function ( err, created ) {
  if ( created ) {
    // Then manually set expiration via native Redis adapter
    var native = Model.adapter.connections.connectionName._adapter.native;

    native( 'connectionName', null, function ( err, connection ) {
      if ( connection ) {
        connection.expire('waterline:<model name, lower case>:id:' + created.id, <timeout, in seconds>, function ( err, reply ) {
          console.log( err, reply );
        });
      }
    });
  }
});

Not quite as clean as I'd prefer, but I also don't like writing that functionality manually when it's built in to Redis itself.