【问题标题】:Get object value from array in cache laravel从缓存laravel中的数组中获取对象值
【发布时间】:2020-06-25 05:06:24
【问题描述】:

我在 Redis 缓存中有一个这样的数组

127.0.0.1:6379> MGET laravel:campaign1107
1) "a:1:{s:21:\"unsubscriberCount1107\";i:2;}"
127.0.0.1:6379> 

现在我需要获取unsubscriber1107 值。我试过这样

 dd(cache()->get($arrayCacheKey[$cacheKey]));

但是,它不起作用。如何访问此对象?

我的设置缓存代码

public function updateUnsubscriberCountCache($campaign_id, $type) 
{  
    $assoc = [];
    $arrayCacheKey = 'campaign'.$campaign_id.'';
    $cacheKey = 'unsubscriberCount'.$campaign_id.'';
    if($type == 'unsubscriberLogCount') {
        $cacheKey = 'unsubscriberCount'.$campaign_id.'';
         if( cache()->get($cacheKey) > 0) {
              cache()->increment($cacheKey);
              //cache()->forget($cacheKey); 
            } else {
                $total = UnsubscribeLog::select('unsubscribe_logs.*')->leftJoin('tracking_logs', 'tracking_logs.message_id', '=', 'unsubscribe_logs.message_id')->where('tracking_logs.campaign_id', '=', $campaign_id)->distinct('subscriber_id')->count('subscriber_id');
                 //cache()->forever($cacheKey, $total);
                $assoc[$cacheKey] = $total;
                cache()->forever($arrayCacheKey, $assoc);
            }
        }
    }

【问题讨论】:

  • 你是如何设置值的?
  • 编辑我的问题。请检查

标签: laravel caching redis


【解决方案1】:

您使用 $arrayCacheKey 将值存储为数组,但在代码的前面部分您尝试使用具有不同值的 $cacheKey 访问它。

如果您想获得unsubscriber1107 的值,您需要使用两个键的组合:

$campaignData = cache()->get($arrayCacheKey); //To get the array value from the cache
$count = $campaignData ? $campaignData[$cacheKey] : null; //get the count 

以上假设 va

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2016-12-24
    • 2017-10-06
    相关资源
    最近更新 更多