From 7b1b56c8995d752bbb3bb87216bb16bd8bcd3a78 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 29 Jun 2022 08:31:56 +0000 Subject: [PATCH] Add event listener to job order to clear the jo json cache when updating or deleting a JO. #689 --- config/services.yaml | 11 ++++++ .../JobOrderJsonCacheListener.php | 37 +++++++++++++++++++ src/Service/JobOrderJsonCache.php | 12 ++++++ 3 files changed, 60 insertions(+) create mode 100644 src/EventListener/JobOrderJsonCacheListener.php diff --git a/config/services.yaml b/config/services.yaml index 50791ba1..180c5602 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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" diff --git a/src/EventListener/JobOrderJsonCacheListener.php b/src/EventListener/JobOrderJsonCacheListener.php new file mode 100644 index 00000000..e2410468 --- /dev/null +++ b/src/EventListener/JobOrderJsonCacheListener.php @@ -0,0 +1,37 @@ +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); + } +} diff --git a/src/Service/JobOrderJsonCache.php b/src/Service/JobOrderJsonCache.php index 414bdb52..31e155d5 100644 --- a/src/Service/JobOrderJsonCache.php +++ b/src/Service/JobOrderJsonCache.php @@ -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); + } + } }