Add event listener to job order to clear the jo json cache when updating or deleting a JO. #689

This commit is contained in:
Korina Cordero 2022-06-29 08:31:56 +00:00
parent bc0ed9ea30
commit 7b1b56c899
3 changed files with 60 additions and 0 deletions

View file

@ -221,6 +221,17 @@ services:
event: 'postPersist'
entity: 'App\Entity\JobOrder'
App\EventListener\JobOrderJsonCacheListener:
arguments:
$json_cache: "@App\\Service\\JobOrderJsonCache"
tags:
- name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'App\Entity\JobOrder'
- name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'App\Entity\JobOrder'
App\Service\JobOrderCache:
arguments:
$redis_prov: "@App\\Service\\RedisClientProvider"

View file

@ -0,0 +1,37 @@
<?php
namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Service\JobOrderJsonCache;
use App\Entity\JobOrder;
class JobOrderJsonCacheListener
{
protected $json_cache;
public function __construct(JobOrderJsonCache $json_cache)
{
$this->json_cache = $json_cache;
}
// when a job order is updated
public function postUpdate(JobOrder $jo, LifecycleEventArgs $args)
{
// get JO id
$id = $jo->getID();
$this->json_cache->removeJOJsonInfo($id);
}
// when a job order is deleted
public function postRemove(JobOrder $jo, LifecycleEventArgs $args)
{
// get JO id
$id = $jo->getID();
$this->json_cache->removeJOJsonInfo($id);
}
}

View file

@ -38,4 +38,16 @@ class JobOrderJsonCache
return $jo_data;
}
public function removeJOJsonInfo($jo_id)
{
$key = $jo_id;
// check if JO id is in redis hash
$is_exist = $this->redis->hexists($this->jo_json_info_key, $key);
if ($is_exist)
{
$this->redis->hdel($this->jo_json_info_key, $key);
}
}
}