From 484dc1e876e4caab7ca22dc60cf7fba59ce33c3c Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Tue, 9 Jun 2020 15:18:44 -0400
Subject: [PATCH] Add support for item-specific cache lifetimes.

---
 module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php
index 65f83c6ee34..4dfa0e0ca8b 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php
@@ -91,7 +91,8 @@ trait CacheTrait
         $item = $this->cache->getItem($fullKey);
         if (null !== $item) {
             // Return value if still valid:
-            if (time() - $item['time'] < $this->cacheLifetime) {
+            $lifetime = $item['lifetime'] ?? $this->cacheLifetime;
+            if (time() - $item['time'] <= $lifetime) {
                 return $item['entry'];
             }
             // Clear expired item from cache:
@@ -105,12 +106,13 @@ trait CacheTrait
      * Data is cached for up to $this->cacheLifetime seconds so that it would be
      * faster to process e.g. requests where multiple calls to the backend are made.
      *
-     * @param string $key   Cache entry key
-     * @param mixed  $entry Entry to be cached
+     * @param string $key      Cache entry key
+     * @param mixed  $entry    Entry to be cached
+     * @param int    $lifetime Optional lifetime for the entry in seconds
      *
      * @return void
      */
-    protected function putCachedData($key, $entry)
+    protected function putCachedData($key, $entry, $lifetime = null)
     {
         // Don't write to cache if we don't have a cache!
         if (null === $this->cache) {
@@ -118,7 +120,8 @@ trait CacheTrait
         }
         $item = [
             'time' => time(),
-            'entry' => $entry
+            'lifetime' => $lifetime,
+            'entry' => $entry,
         ];
         $this->cache->setItem($this->getCacheKey($key), $item);
     }
-- 
GitLab