From a67cd11dd09ef8d7ef43844a8ce176c68d3c3ac1 Mon Sep 17 00:00:00 2001
From: AaronBauman <aaronbauman@384578.no-reply.drupal.org>
Date: Thu, 12 Aug 2021 13:34:52 +0000
Subject: [PATCH] Issue #3226070 by AaronBauman: Expose cache lifetimes to
 config; make default lifetimes longer

---
 config/install/salesforce.settings.yml |  2 ++
 config/schema/salesforce.schema.yml    | 20 ++++++++------------
 salesforce.install                     | 19 ++++++++++++++++++-
 src/Form/SettingsForm.php              | 14 ++++++++++++++
 src/Rest/RestClient.php                | 16 ++++++++++++----
 src/Rest/RestClientInterface.php       | 16 ++++++++++++++++
 tests/src/Unit/RestClientTest.php      | 10 ++++++++++
 7 files changed, 80 insertions(+), 17 deletions(-)

diff --git a/config/install/salesforce.settings.yml b/config/install/salesforce.settings.yml
index 77ae9eb2..6414ec24 100644
--- a/config/install/salesforce.settings.yml
+++ b/config/install/salesforce.settings.yml
@@ -9,3 +9,5 @@ show_all_objects: false
 standalone: false
 limit_mapped_object_revisions: 10
 salesforce_auth_provider: ''
+short_term_cache_lifetime: 3600
+long_term_cache_lifetime: 604800
diff --git a/config/schema/salesforce.schema.yml b/config/schema/salesforce.schema.yml
index 621c6ae8..dcf22482 100644
--- a/config/schema/salesforce.schema.yml
+++ b/config/schema/salesforce.schema.yml
@@ -2,18 +2,6 @@ salesforce.settings:
   type: config_object
   label: 'Salesforce Settings'
   mapping:
-    consumer_key:
-      type: string
-      label: 'Salesforce consumer key'
-      description: 'Consumer key of the Salesforce remote application you want to grant access to.'
-    consumer_secret:
-      type: string
-      label: 'Salesforce consumer secret'
-      description: 'Consumer secret of the Salesforce remote application you want to grant access to.'
-    login_url:
-      type: string
-      label: 'Login URL'
-      description: 'API login URL, either https://login.salesforce.com or https://test.salesforce.com.'
     global_push_limit:
       type: integer
       label: 'Global push queue limit'
@@ -42,6 +30,14 @@ salesforce.settings:
       type: string
       label: 'Default authorization provider id'
       description: 'A salesforce_auth config entity id which provides API authorization.'
+    short_term_cache_lifetime:
+      type: integer
+      label: "Short term cache lifetime"
+      description: "Value, in seconds, to store short term meta data. This is used for, e.g., the list of Object Types, Object Descriptions, and Record Types."
+    long_term_cache_lifetime:
+      type: integer
+      label: "Long term cache lifetime"
+      description: "Value, in seconds, to store long term meta data. This is used for, e.g., the list of API versions."
     rest_api_version:
       type: mapping
       label: 'REST API Version'
diff --git a/salesforce.install b/salesforce.install
index 738bb199..2d75bbb4 100644
--- a/salesforce.install
+++ b/salesforce.install
@@ -197,7 +197,7 @@ function salesforce_get_usage_requirements() {
   else {
     $usage = str_replace('api-usage=', '', $usage);
 
-    list($usage, $limit) = explode('/', $usage, 2);
+    [$usage, $limit] = explode('/', $usage, 2);
     $pct = 'N/A';
     if ($limit > 0) {
       $pct = ($usage / $limit) * 100.0;
@@ -456,3 +456,20 @@ function salesforce_update_8402() {
     return;
   }
 }
+
+/**
+ * New cache lifetime config options.
+ */
+function salesforce_update_8800() {
+  $settings = \Drupal::configFactory()->getEditable('salesforce.settings');
+  $save = FALSE;
+  if (!$settings->get('short_term_cache_lifetime')) {
+    $settings->set('short_term_cache_lifetime', \Drupal\salesforce\Rest\RestClient::CACHE_LIFETIME);
+    $save = TRUE;
+  }
+  if (!$settings->get('long_term_cache_lifetime')) {
+    $settings->set('long_term_cache_lifetime', \Drupal\salesforce\Rest\RestClient::LONGTERM_CACHE_LIFETIME);
+    $save = TRUE;
+  }
+  $settings->save();
+}
\ No newline at end of file
diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php
index f8715bcf..9891dd5b 100644
--- a/src/Form/SettingsForm.php
+++ b/src/Form/SettingsForm.php
@@ -97,6 +97,20 @@ class SettingsForm extends ConfigFormBase {
       $this->messenger()->addError($this->t('Error when connecting to Salesforce. Please <a href="@href">check your credentials</a> and try again: %message', ['@href' => $href->toString(), '%message' => $e->getMessage()]));
     }
 
+    $form['short_term_cache_lifetime'] = [
+      '#title' => $this->t($definition['short_term_cache_lifetime']['label']),
+      '#description' => $this->t($definition['short_term_cache_lifetime']['description']),
+      '#type' => 'number',
+      '#default_value' => $config->get('short_term_cache_lifetime'),
+    ];
+
+    $form['long_term_cache_lifetime'] = [
+      '#title' => $this->t($definition['long_term_cache_lifetime']['label']),
+      '#description' => $this->t($definition['long_term_cache_lifetime']['description']),
+      '#type' => 'number',
+      '#default_value' => $config->get('short_term_cache_lifetime'),
+    ];
+
     $form['rest_api_version'] = [
       '#title' => $this->t($definition['rest_api_version']['label']),
       '#description' => $this->t($definition['rest_api_version']['description']),
diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php
index 781a884e..4dfc69fa 100644
--- a/src/Rest/RestClient.php
+++ b/src/Rest/RestClient.php
@@ -154,6 +154,14 @@ class RestClient implements RestClientInterface {
     return $this;
   }
 
+  public function getShortTermCacheLifetime() {
+    return $this->immutableConfig->get('short_term_cache_lifetime') ?? static::CACHE_LIFETIME;
+  }
+
+  public function getLongTermCacheLifetime() {
+    return $this->immutableConfig->get('long_term_cache_lifetime') ?? static::LONGTERM_CACHE_LIFETIME;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -377,7 +385,7 @@ class RestClient implements RestClientInterface {
     foreach ($response->data as $version) {
       $versions[$version['version']] = $version;
     }
-    $this->cache->set('salesforce:versions', $versions, $this->getRequestTime() + self::LONGTERM_CACHE_LIFETIME, ['salesforce']);
+    $this->cache->set('salesforce:versions', $versions, $this->getRequestTime() + $this->getLongTermCacheLifetime(), ['salesforce']);
     return $versions;
   }
 
@@ -417,7 +425,7 @@ class RestClient implements RestClientInterface {
     }
     else {
       $result = $this->apiCall('sobjects');
-      $this->cache->set('salesforce:objects', $result, $this->getRequestTime() + self::CACHE_LIFETIME, ['salesforce']);
+      $this->cache->set('salesforce:objects', $result, $this->getRequestTime() + $this->getShortTermCacheLifetime(), ['salesforce']);
     }
     // print_r($result);
     $sobjects = [];
@@ -485,7 +493,7 @@ class RestClient implements RestClientInterface {
     }
     else {
       $response = new RestResponseDescribe($this->apiCall("sobjects/{$name}/describe", [], 'GET', TRUE));
-      $this->cache->set('salesforce:object:' . $name, $response, $this->getRequestTime() + self::CACHE_LIFETIME, ['salesforce']);
+      $this->cache->set('salesforce:object:' . $name, $response, $this->getRequestTime() + $this->getShortTermCacheLifetime(), ['salesforce']);
       return $response;
     }
   }
@@ -605,7 +613,7 @@ class RestClient implements RestClientInterface {
       foreach ($result->records() as $rt) {
         $record_types[$rt->field('SobjectType')][$rt->field('DeveloperName')] = $rt;
       }
-      $this->cache->set('salesforce:record_types', $record_types, $this->getRequestTime() + self::CACHE_LIFETIME, ['salesforce']);
+      $this->cache->set('salesforce:record_types', $record_types, $this->getRequestTime() + $this->getShortTermCacheLifetime(), ['salesforce']);
     }
 
     if ($name != NULL) {
diff --git a/src/Rest/RestClientInterface.php b/src/Rest/RestClientInterface.php
index b7c18398..e5746221 100644
--- a/src/Rest/RestClientInterface.php
+++ b/src/Rest/RestClientInterface.php
@@ -407,4 +407,20 @@ interface RestClientInterface {
    */
   public function getObjectTypeName(SFID $id);
 
+  /**
+   * Getter for short term cache lifetime.
+   *
+   * @return int
+   *   Short term cache lifetime, in seconds.
+   */
+  public function getShortTermCacheLifetime();
+
+  /**
+   * Getter for long term cache lifetime.
+   *
+   * @return int
+   *   Long term cache lifetime, in seconds.
+   */
+  public function getLongTermCacheLifetime();
+
 }
diff --git a/tests/src/Unit/RestClientTest.php b/tests/src/Unit/RestClientTest.php
index 9f6cfb85..f843a7a0 100644
--- a/tests/src/Unit/RestClientTest.php
+++ b/tests/src/Unit/RestClientTest.php
@@ -97,6 +97,8 @@ class RestClientTest extends UnitTestCase {
     if (empty($methods)) {
       $methods = $this->methods;
     }
+    $methods[] = 'getShortTermCacheLifetime';
+    $methods[] = 'getLongTermCacheLifetime';
 
     $args = [
       $this->httpClient,
@@ -113,6 +115,14 @@ class RestClientTest extends UnitTestCase {
       ->setMethods($methods)
       ->setConstructorArgs($args)
       ->getMock();
+
+    $this->client->expects($this->any())
+      ->method('getShortTermCacheLifetime')
+      ->willReturn(0);
+
+    $this->client->expects($this->any())
+      ->method('getLongTermCacheLifetime')
+      ->willReturn(0);
   }
 
   /**
-- 
GitLab