hsntngr
9 months ago

Event subscriber doesn't work well when event fired

Posted 9 months ago by hsntngr

I'm using the event subscriber to delete rendered view caches of posts..

There several events belongs to post such as ArticleUpdated, NewComment, CommentUpdated etc..

When one of them fired I want to clear rendered view cache.

on this purpose I create an event listener and added inside to EventServiceProvider::$subscribe method.

protected $subscribe = [
        'App\Subscribers\ClearHtmlCacheSubscriber',
    ];

and ClearHtmlCacheSubscriber

public function onArticleEvent($event)
    {
        if ($event->isSpam === false)
            $this->clearHtmlCache($event->article->articleable);
    }

    public function onCommentEvent($event)
    {
        $this->clearHtmlCache($event->comment->commentable);
    }

    public function clearHtmlCache($model)
    {
        $key = "posts.renderedView" . $model->getTable() . $model->id . \LaravelLocalization::setLocale();
        Redis::del($key);
    }

    public function subscribe($events)
    {
        $events->listen(
            'App\Events\ArticleUpdated',
            'App\Subscribers\[email protected]'
        );
        $events->listen(
            'App\Events\NewComment',
            'App\Subscribers\[email protected]'
        );
        $events->listen(
            'App\Events\CommentUpdated',
            'App\Subscribers\[email protected]'
        );

If I understood document correctly, above example should work.. But it seems there is something that I'm doing wrong.. How can I make it work this subscriber ?

Please sign in or create an account to participate in this conversation.