From 17cdaa93e6723a0c82ee5c50226f0a2f39a19598 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathias=20Maa=C3=9F?= <mathias.maass@uni-leipzig.de>
Date: Wed, 30 Nov 2022 13:58:24 +0100
Subject: [PATCH] refs #22797 [finc] empty paia patron cache after updating
 patron data

* add getter methods for each PAIA cache key
** items
** fees
** notifications
* remove items cache after placeTitleHold
* remove items cache after
---
 module/finc/src/finc/ILS/Driver/FincILS.php   |  4 +-
 .../finc/src/finc/ILS/Driver/FincLibero.php   |  6 +-
 module/finc/src/finc/ILS/Driver/PAIA.php      | 82 ++++++++++++-------
 3 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/module/finc/src/finc/ILS/Driver/FincILS.php b/module/finc/src/finc/ILS/Driver/FincILS.php
index 6eab90c6eea..036bb03fe6a 100644
--- a/module/finc/src/finc/ILS/Driver/FincILS.php
+++ b/module/finc/src/finc/ILS/Driver/FincILS.php
@@ -1145,7 +1145,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
 
         // check for existing data in cache
         if ($this->paiaCacheEnabled) {
-            $itemsResponse = $this->getCachedData($patron['cat_username']);
+            $itemsResponse = $this->getCachedData($this->getCacheKeyItems($patron));
         }
 
         if (!isset($itemsResponse) || $itemsResponse == null) {
@@ -1159,7 +1159,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
             }
 
             if ($this->paiaCacheEnabled) {
-                $this->putCachedData($patron['cat_username'], $itemsResponse);
+                $this->putCachedData($this->getCacheKeyItems($patron), $itemsResponse);
             }
         }
 
diff --git a/module/finc/src/finc/ILS/Driver/FincLibero.php b/module/finc/src/finc/ILS/Driver/FincLibero.php
index 7bbc7143e0a..1fc98649fc3 100644
--- a/module/finc/src/finc/ILS/Driver/FincLibero.php
+++ b/module/finc/src/finc/ILS/Driver/FincLibero.php
@@ -465,7 +465,7 @@ class FincLibero extends FincILS implements TranslatorAwareInterface
                         $this->removeCachedData($holdDetails['doc_id']);
                     }
                     if ($this->paiaCacheEnabled) {
-                        $this->removeCachedData($patron['cat_username']);
+                        $this->removeCachedData($this->getCacheKeyItems($patron));
                     }
                 }
             }
@@ -1252,6 +1252,10 @@ class FincLibero extends FincILS implements TranslatorAwareInterface
                     if ($this->daiaCacheEnabled) {
                         $this->removeCachedData($holdDetails['doc_id']);
                     }
+
+                    if ($this->paiaCacheEnabled) {
+                        $this->removeCachedData($this->getCacheKeyItems($patron));
+                    }
                 }
             }
         }
diff --git a/module/finc/src/finc/ILS/Driver/PAIA.php b/module/finc/src/finc/ILS/Driver/PAIA.php
index 15c6aecc6ff..ccdf2d1f776 100644
--- a/module/finc/src/finc/ILS/Driver/PAIA.php
+++ b/module/finc/src/finc/ILS/Driver/PAIA.php
@@ -205,7 +205,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
             // Otherwise the changed status will not be shown before the cache
             // expires.
             if ($this->paiaCacheEnabled) {
-                $this->removeCachedData($patron['cat_username']);
+                $this->removeCachedData($this->getCacheKeyItems($patron));
             }
         }
         $returnArray = ['count' => $count, 'items' => $details];
@@ -305,7 +305,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
 
         if ($this->paiaCacheEnabled) {
-            $fees = $this->getCachedData($this->_feesCacheKey($patron));
+            $fees = $this->getCachedData($this->getCacheKeyFees($patron));
         }
 
         if (!isset($fees) || $fees == null) {
@@ -319,7 +319,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
             }
 
             if ($this->paiaCacheEnabled) {
-                $this->putCachedData($this->_feesCacheKey($patron), $fees);
+                $this->putCachedData($this->getCacheKeyFees($patron), $fees);
             }
         }
 
@@ -588,7 +588,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
                     }
 
                     if ($this->paiaCacheEnabled) {
-                        $this->removeCachedData($patron['cat_username']);
+                        $this->removeCachedData($this->getCacheKeyItems($patron));
                     }
                 }
             }
@@ -692,7 +692,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
             // item renew was successfull and therefore the status changed. Otherwise
             // the changed status will not be shown before the cache expires.
             if ($this->paiaCacheEnabled) {
-                $this->removeCachedData($patron['cat_username']);
+                $this->removeCachedData($this->getCacheKeyItems($patron));
             }
         }
         $returnArray = ['blocks' => false, 'details' => $details];
@@ -723,7 +723,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
 
         // check for existing data in cache
         if ($this->paiaCacheEnabled) {
-            $itemsResponse = $this->getCachedData($patron['cat_username']);
+            $itemsResponse = $this->getCachedData($this->getCacheKeyItems($patron));
         }
 
         if (!isset($itemsResponse) || $itemsResponse == null) {
@@ -736,7 +736,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
                 throw $e;
             }
             if ($this->paiaCacheEnabled) {
-                $this->putCachedData($patron['cat_username'], $itemsResponse);
+                $this->putCachedData($this->getCacheKeyItems($patron), $itemsResponse);
             }
         }
 
@@ -768,6 +768,18 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         return [];
     }
 
+    /**
+     * Helper function to generate cache key for items.
+     *
+     * @param array $patron Array with patron information
+     *
+     * @return string
+     */
+    protected function getCacheKeyItems($patron)
+    {
+        return $this->getCacheKey($patron['cat_username'] . '_items');
+    }
+
     /**
      * Helper function to generate cache key for fees.
      *
@@ -775,21 +787,33 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
      *
      * @return string
      */
-    private function _feesCacheKey($patron)
+    protected function getCacheKeyFees($patron)
     {
-        return $patron['cat_username'] . '_fees';
+        return $this->getCacheKey($patron['cat_username'] . '_fees');
     }
 
     /**
      * Helper function to generate cache key for notifications.
      *
      * @param array $patron Array with patron information
-     * 
+     *
+     * @return string
+     */
+    protected function getCacheKeyNotifications($patron)
+    {
+        return $this->getCacheKey($patron['cat_username'] . '_notifications');
+    }
+
+    /**
+     * Helper function to generate cache key for patron data.
+     *
+     * @param array $patron Array with patron information
+     *
      * @return string
      */
-    private function _notificationsCacheKey($patron)
+    protected function getCacheKeyPatron($patron)
     {
-        return $patron['cat_username'] . '_notifications';
+        return $this->getCacheKey($patron['cat_username'] . '_patron');
     }
 
     /**
@@ -827,7 +851,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
 
         if ($this->paiaCacheEnabled) {
-            $response = $this->getCachedData($this->_notificationsCacheKey($patron));
+            $response = $this->getCachedData($this->getCacheKeyNotifications($patron));
             if ($response !== null) {
                 return $response;
             }
@@ -863,7 +887,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
 
         if ($this->paiaCacheEnabled) {
-            $this->putCachedData($this->_notificationsCacheKey($patron), $response);
+            $this->putCachedData($this->getCacheKeyNotifications($patron), $response);
         }
 
         return $response;
@@ -896,7 +920,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
 
         if (!$keepCache && $this->paiaCacheEnabled) {
-            $this->removeCachedData($this->_notificationsCacheKey($patron));
+            $this->removeCachedData($this->getCacheKeyNotifications($patron));
         }
 
         return $response;
@@ -917,7 +941,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
 
         if ($this->paiaCacheEnabled) {
-            $this->removeCachedData($this->_notificationsCacheKey($patron));
+            $this->removeCachedData($this->getCacheKeyNotifications($patron));
         }
 
         return true;
@@ -1166,7 +1190,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
 
             // From Vufind documentation:
             // "dueStatus - A special status – may be 'due' (for items due very
-            // soon) or 'overdue' (for overdue items). If this is false, empty 
+            // soon) or 'overdue' (for overdue items). If this is false, empty
             // string, or unset, VuFind will assume that items have no special status.
             // (optional)."
             $result['dueStatus'] = isset($doc['endtime']) && (new \DateTime() > new \DateTime($doc['endtime']))
@@ -1313,7 +1337,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
 
         // prepare post data depending on configured grant type
         switch ($this->grantType) {
-        case 'password' :
+        case 'password':
             $post_data = [
                 "username"   => $username,
                 "password"   => $password
@@ -1419,33 +1443,29 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
      * @return array
      * @throws ILSException
      */
-    protected function paiaGetUserDetails($patron)
+    protected function paiaGetUserDetails($patronId)
     {
         // check if user has appropriate scope
         if (!$this->paiaCheckScope(self::SCOPE_READ_PATRON)) {
             throw new ILSException('You are not entitled to read patron.');
         }
 
-        $cacheKey = function ($patron) {
-            return $patron . '_user_details';
-        };
-
         $response = null;
 
         if ($this->paiaCacheEnabled) {
-            $response = $this->getCachedData($cacheKey($patron));
+            $response = $this->getCachedData($this->getCacheKeyPatron(['cat_username' => $patronId]));
         }
 
         if ($response === null) {
             // all error handling is done in paiaHandleErrors so pass on the excpetion
-            $response = $this->paiaGetAsArray('core/' . $patron);
-            
+            $response = $this->paiaGetAsArray('core/' . $patronId);
+
             if ($this->paiaCacheEnabled) {
-                $this->putCachedData($cacheKey($patron), $response);
+                $this->putCachedData($this->getCacheKeyPatron(['cat_username' => $patronId]), $response);
             }
         }
 
-        return $this->paiaParseUserDetails($patron, $response);
+        return $this->paiaParseUserDetails($patronId, $response);
     }
 
     /**
@@ -1522,6 +1542,10 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
                 'success' => true,
                 'sysMessage' => 'Successfully requested'
             ];
+
+            if ($this->paiaCacheEnabled) {
+                $this->removeCachedData($this->getCacheKeyPatron($patron));
+            }
         }
         return $details;
     }
@@ -1697,7 +1721,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         }
         return $retval;
     }
-    
+
     /**
      * PAIA support function to implement ILS specific parsing of user_details
      * Calling parent method and just trimming firstname
-- 
GitLab