Add event listener to job order to clear the jo json cache when updating or deleting a JO. #689
This commit is contained in:
parent
bc0ed9ea30
commit
7b1b56c899
3 changed files with 60 additions and 0 deletions
|
|
@ -221,6 +221,17 @@ services:
|
||||||
event: 'postPersist'
|
event: 'postPersist'
|
||||||
entity: 'App\Entity\JobOrder'
|
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:
|
App\Service\JobOrderCache:
|
||||||
arguments:
|
arguments:
|
||||||
$redis_prov: "@App\\Service\\RedisClientProvider"
|
$redis_prov: "@App\\Service\\RedisClientProvider"
|
||||||
|
|
|
||||||
37
src/EventListener/JobOrderJsonCacheListener.php
Normal file
37
src/EventListener/JobOrderJsonCacheListener.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -38,4 +38,16 @@ class JobOrderJsonCache
|
||||||
|
|
||||||
return $jo_data;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue