【问题标题】:Laravel 5.5 - Update Redis Cache without expiration timeLaravel 5.5 - 更新 Redis 缓存没有过期时间
【发布时间】:2019-01-16 06:45:20
【问题描述】:

我想在不指定过期时间或从缓存中删除项目并再次存储的情况下更新值。我是这样尝试的:

创建后:

Cache::put('post' . $id, $post, 10);

更新后:

Cache::put('post' . $id, $post);

但是没有过期时间是不会更新的。

这不可能吗?

【问题讨论】:

    标签: laravel caching redis laravel-5.5


    【解决方案1】:

    我不确定是否可以在不包括分钟数的情况下覆盖值。但是,一种可能的解决方法是将日期和时间与您的帖子一起存储,并添加逻辑以根据需要进行调整。

    创建示例:

    Cache::put('post' . $id, $post, 10);
    Cache::put('time:post' . $id, Carbon::now(), 10);
    

    更新示例:

    $time = Cache::get('time:post' . $id);
    Cache::put('post' . $id, $post, $time->diffInMinutes(Carbon::now()));
    

    【讨论】:

      【解决方案2】:

      最近的 Laravel 5.7 示例:

      function CountWeek()
      {
          if (!Cache::get('count_for_week:init')) {
              Cache::put('count_for_week:init', now()->addDays(7));
              Cache::put('count_for_week:value', 1, now()->addMonth(1));
              return true;
          }
      
          $time = Cache::get('count_for_week:init');
          Cache::put('count_for_week:value', (Cache::get('count_for_week:value') ?? 0) + 1, $time->diffInMinutes(now()));
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-10
        • 2020-08-20
        • 1970-01-01
        • 2021-09-11
        • 2015-10-02
        • 2018-01-30
        • 2017-06-05
        • 2012-07-16
        相关资源
        最近更新 更多