从缓存laravel中的数组获取对象值

whhtz7ly  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(481)

我在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);
            }
        }
    }
apeeds0o

apeeds0o1#

将值存储为数组,使用 $arrayCacheKey 但是在前面的代码中,您尝试使用 $cacheKey 有不同的价值。
如果你想得到 unsubscriber1107 您需要同时使用两个键:

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

以上假设va

相关问题