Get object value from array in cache laravel

Viewed 584

I have an array in Redis cache like that

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

Now I need to get unsubscriber1107 value. I tried to this way

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

but, it's doesn't work. How can I access this object?

My code for set cache

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);
            }
        }
    }
1 Answers

You're storing the value as an array using $arrayCacheKey but earlier in the code you're trying to access it using the $cacheKey which has a different value.

If you want to get the value of unsubscriber1107 you will need to use a combination of both keys:

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

The above assumes that the va

Related