无法在 Redis 键上设置过期时间

问题描述 投票:0回答:1

我正在尝试为 Redis 键设置过期时间,并尝试了多种方法但没有成功。我用

"redis": "^4.6.15"
。例如this 回答他们说要这样做:

await redisClient.set(key , value, {EX: 60*60*4})

我尝试过这个,但无论采用哪种方法,我都会收到错误“

[ErrorReply: ERR value is not an integer or out of range]
”。我像这样启动 Redis 客户端:

const redisClient = createClient({
  password: process.env.RedisCachePass,
  socket: {
      host: process.env.RedisCacheUrl,
      port: process.env.RedisCachePort,
      tls: true
  }
})
redisClient.on('error', (err) => {
  console.error('Could not connect to Redis', err);
});

redisClient.on('connect', () => {
  console.log('Connected to Redis ');
  console.log(redisClient);
});

这给了我这个对象:

Commander {
  _events: [Object: null prototype] {
    error: [Function (anonymous)],
    connect: [Function (anonymous)]
  },
  _eventsCount: 2,
  _maxListeners: undefined,
  commandOptions: [Function: commandOptions],
  select: [AsyncFunction: SELECT],
  subscribe: [Function: SUBSCRIBE],
  unsubscribe: [Function: UNSUBSCRIBE],
  pSubscribe: [Function: PSUBSCRIBE],
  pUnsubscribe: [Function: PUNSUBSCRIBE],
  sSubscribe: [Function: SSUBSCRIBE],
  sUnsubscribe: [Function: SUNSUBSCRIBE],
  quit: [Function: QUIT],
  multi: [Function: MULTI],
  bf: {},
  cms: {},
  cf: {},
  tDigest: {},
  topK: {},
  graph: {},
  json: {},
  ft: {},
  ts: {},
  [Symbol(shapeMode)]: false,
  [Symbol(kCapture)]: false
}

然后我用 redisClient 创建一个 Express 服务器,如下所示:

 server.use(session({
    resave: false,
    saveUninitialized: false,
    secret: 'secret-key',
    cookie: {
      secure: 'auto',
      httpOnly: true,
      ephemeral: true
    },
    store: new RedisStore({
      client: redisClient
    })
  }))

这是我在代码中的另一个地方使用 redisClient 的方法,这就是我收到错误的地方:

post: async(req, res) => {
    console.log('#### REDIS POST (SET) ####')
    try {
      req.body = camelcaseKeys(req.body, { deep: true })
      const redisClient = req.sessionStore.client
      if (redisClient) {
        const { key, ...data } = req.body
        if (typeof data.jwtRequired === 'boolean' && data.jwtRequired) {
            data.jwt = req.session.jwt
        }
        const value = JSON.stringify(data)

        console.log(redisClient)
        await redisClient.set(key , value, {EX: 60*60*4})

        console.log('#### key: ', key)
        console.log('#### REDIS POST (SET) END ####')
        res.status(200).json()
      } else {
        throw new Error(REDIS_NOT_CONNECTED_ERROR)
      }
    } catch (error) {
      console.error(error)
      res.status(500).json()
    }
  },

这给了我输出:

#### REDIS POST (SET) ####
{
  get: [Function: get],
  set: [Function: set],
  del: [Function: del],
  expire: [Function: expire],
  mget: [Function: mget],
  scanIterator: [Function: scanIterator]
}
[ErrorReply: ERR value is not an integer or out of range]

为什么我的 Redis 密钥无法设置过期时间?为什么我的 Redis 客户端似乎没有像其他人那样具有 ttl 之类的功能,当我 Google 时?

redis vuejs3
1个回答
0
投票

我怀疑这里的问题是你需要先将客户端连接到Redis,然后再将其交给connect-redis。或者至少这是您最终会遇到的a问题。

客户端连接后要连接Redis,只需执行以下操作:

await redisClient.connect()

让我知道这是否有帮助,如果没有,我可以修改这个答案。

© www.soinside.com 2019 - 2024. All rights reserved.