【问题标题】:Laravel Redis Predis and method GETLaravel Redis Predis 和方法 GET
【发布时间】:2020-04-07 08:34:41
【问题描述】:

我有这个功能:

   public function showProfile($id)
    {

        // Check if user already in redis with cache key user.1 for example
        if (!Redis::exists('user.' . $id)) {
            // If the user is not in redis, fetch it from DB and add it in redis for 60 seconds before returning it
            $user = User::findOrFail($id);
            Redis::set('user.' . $user->id, $user);

            return $user;
        } else {
            // If user is in redis, just return it without querying the database
            return Redis::get('user'.$id);// How to set this?

        }

enter image description here

但是函数返回为null,请问为什么...

【问题讨论】:

  • 你有什么理由想直接使用redis而不是cache和redis驱动?
  • 就像 Remul 说的,直接使用 Redis 没有意义。将缓存与 redis 驱动程序一起使用。如果您已经在不同的驱动程序上拥有缓存并且只想为此目的使用 Redis,那么只需将缓存与多个驱动程序一起使用。

标签: laravel redis predis


【解决方案1】:

您的密钥似乎不同。你正在设置user.id:

Redis::set('user.' . $user->id, $user);

而你正在尝试获取userid

return Redis::get('user'.$id);

【讨论】:

  • 别着急,大家都会的哈哈。我花了几个小时调试一些东西,以发现我拼写错误或遗漏了;。祝你好运!
猜你喜欢
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 2018-01-21
相关资源
最近更新 更多