【问题标题】:How do to call EXPIRE command for a Redis record using Sails如何使用 Sails 为 Redis 记录调用 EXPIRE 命令
【发布时间】:2015-02-26 09:41:14
【问题描述】:

我正在使用 Sails 向 Redis 添加一些数据...它工作正常,但我不确定如何设置键的 EXPIRE...

我正在为模型使用sails-redis 适配器/连接... 我的模型是这样的

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

    }
};

保存我使用的模型

Cache.create({id: "somekey", data: data}, function(err, data){})

【问题讨论】:

    标签: node.js redis sails.js waterline sails-redis


    【解决方案1】:

    据我所知,Waterline 目前附近的本机适配器不提供此功能。但是,这有点 hacky,但是您可以通过 Waterline 访问 redis 客户端本身,并使用创建的模型 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 );
            });
          }
        });
      }
    });
    

    不像我希望的那样干净,但我也不喜欢手动编写该功能,因为它内置于 Redis 本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-10
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多