Home » Php » php – Laravel Event listener and caching not working

php – Laravel Event listener and caching not working

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am facing some difficulties while developing an app on Laravel.
I want to use Event and Listener to delete and rebuild the cache of an object.

Here is the code:

app\Events\CampaignEvent.php

namespace App\Events;

use Illuminate\Queue\SerializesModels;

class CampaignEvent extends Event
{
    use SerializesModels;
    public $user_id;
    public $cache_keys;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user_id, $cache_keys)
    {
        $this->user_id = $user_id;
        $this->cache_keys = $cache_keys;
    }
}

app\Listenters\CampaignListener.php

<?php

namespace App\Listeners;

use App\Events\CampaignEvent;
use Cache;
use Log;
use App\BrandCampaign;

class CampaignListener
{

    /**
     * Handle the event.
     *
     * @param  CampaignEvent  $event
     * @return void
     */
    public function handle(CampaignEvent $event)
    {
        /**
         * Remove cache
         */
        if(is_array($event->cache_keys)){
            foreach($event->cache_keys as $index => $cache_key){
                \Cache::forget($cache_key);
                Log::debug("[CACHE] Deleted cache for: " . $cache_key);
            }
        } else {
            \Cache::forget($event->cache_keys);
            Log::debug("[CACHE] Deleted cache for: " . $event->cache_keys);
        }

        /**
         * Rebuild cache for BrandCampaigns
         */
        $campaigns = BrandCampaign::with(['influencers' => function($query){
            $query->with(['influencer' => function($query){
                $query->select('id','profile_picture');
            }])->latest();
        }])->where('user_id', $event->user_id )->latest()->get();

        $total_influencers = [];
        foreach($campaigns as $campaign){
            foreach ($campaign->influencers as $influencer) {
                if(!in_array($influencer->influencer_id, $total_influencers))
                    $total_influencers[] = $influencer->influencer_id;
            }
        }
        $total_influencers = count($total_influencers);

        $campaigns = collect($campaigns)->toArray();

        \Cache::forever('@suppliers_campaigns('.$event->user_id.')', $campaigns);
        \Cache::put('@suppliers_total_campaigns('.$event->user_id.')', $total_influencers, 10);

        Log::debug("[CACHE] Cache rebuilt successfully!");
        return $event;
    }
}

I want to cache an array "forever", but in my campaign controller, after the event is fired, when I pull the array from cache it is returning null

Thanks!

How to&Answers:

Works in Laravel 5 (based on the question) & Laravel 7 (latest) as well.

use Illuminate\Support\Facades\Cache;

// Remove cache
Cache::forget('brandCampaigns');

// Rebuild cache for BrandCampaigns. Here, when the cache key doesn't exists, the function will be called and the returned value will be stored in the cache
$campaigns = Cache::rememberForever('brandCampaigns', function () {
    return BrandCampaign::with(['influencers' => function ($query) {
        $query->with(['influencer' => function ($query) {
            $query->select('id', 'profile_picture');
        }])->latest();
    }])->where('user_id', $event->user_id)->latest()->get();
});