redis缓存键选择

nhhxz33t  于 2021-06-08  发布在  Redis
关注(0)|答案(0)|浏览(204)

我对为用户的配置文件路由选择唯一的缓存键感到困惑
例如

router.get('/profile/me', auth, async (req, res) => {

// first check that is it available on Redis

// if not execute mongoose query and store this on Redis

  const key = JSON.stringify({
    ...this.getQuery(),
    collection: this.mongooseCollection.name
  });

//assume hashKey: req.user._id

const result = await exec.apply(this, args);
client.hset(this.hashKey, key, JSON.stringify(result));

// if yes then immediately return from Redis
})

我要缓存此路由,并在转到这些路由时清除该缓存

router.patch('/profile/me', auth, async (req, res) => {
// clear cache with specific hashKey if response is 200
client.del(hashKey)
})

router.delete('/profile/me', auth, async (req, res) => {
// clear cache with specific hashKey if response is 200
client.del(hashKey)
})

但问题是,如果我们想缓存另一个用户的配置文件,我们如何知道该用户已更新或删除,因为我们将清除缓存时,他删除或更新他的配置文件
例如:

router.get('/profile/user/:userId', auth, async (req, res) => {

// first check that is it available on Redis

// if not execute mongoose query and store this on Redis

//WHAT WILL BE hashKey ? req.params.userId OR req.user._id

// if yes then immediately return from Redis
})

我们怎么清除缓存因为我们没有这些路径

router.patch('/profile/user/:userId', auth, async (req, res) => {
// clear cache with specific hashkey if response is 200
})

router.delete('/profile/user/:userId', auth, async (req, res) => {
// clear cache with specific hashkey if response is 200
})

所以问题是hashkey应该是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题