Skip to content
Snippets Groups Projects
Commit a67cd11d authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Issue #3226070 by AaronBauman: Expose cache lifetimes to config; make default lifetimes longer

parent 4505018d
No related branches found
No related tags found
2 merge requests!40Added check for null value.,!26Resolve #3226070 "Expose cache lifetimes"
...@@ -9,3 +9,5 @@ show_all_objects: false ...@@ -9,3 +9,5 @@ show_all_objects: false
standalone: false standalone: false
limit_mapped_object_revisions: 10 limit_mapped_object_revisions: 10
salesforce_auth_provider: '' salesforce_auth_provider: ''
short_term_cache_lifetime: 3600
long_term_cache_lifetime: 604800
...@@ -2,18 +2,6 @@ salesforce.settings: ...@@ -2,18 +2,6 @@ salesforce.settings:
type: config_object type: config_object
label: 'Salesforce Settings' label: 'Salesforce Settings'
mapping: 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: global_push_limit:
type: integer type: integer
label: 'Global push queue limit' label: 'Global push queue limit'
...@@ -42,6 +30,14 @@ salesforce.settings: ...@@ -42,6 +30,14 @@ salesforce.settings:
type: string type: string
label: 'Default authorization provider id' label: 'Default authorization provider id'
description: 'A salesforce_auth config entity id which provides API authorization.' 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: rest_api_version:
type: mapping type: mapping
label: 'REST API Version' label: 'REST API Version'
......
...@@ -197,7 +197,7 @@ function salesforce_get_usage_requirements() { ...@@ -197,7 +197,7 @@ function salesforce_get_usage_requirements() {
else { else {
$usage = str_replace('api-usage=', '', $usage); $usage = str_replace('api-usage=', '', $usage);
list($usage, $limit) = explode('/', $usage, 2); [$usage, $limit] = explode('/', $usage, 2);
$pct = 'N/A'; $pct = 'N/A';
if ($limit > 0) { if ($limit > 0) {
$pct = ($usage / $limit) * 100.0; $pct = ($usage / $limit) * 100.0;
...@@ -456,3 +456,20 @@ function salesforce_update_8402() { ...@@ -456,3 +456,20 @@ function salesforce_update_8402() {
return; 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
...@@ -97,6 +97,20 @@ class SettingsForm extends ConfigFormBase { ...@@ -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()])); $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'] = [ $form['rest_api_version'] = [
'#title' => $this->t($definition['rest_api_version']['label']), '#title' => $this->t($definition['rest_api_version']['label']),
'#description' => $this->t($definition['rest_api_version']['description']), '#description' => $this->t($definition['rest_api_version']['description']),
......
...@@ -154,6 +154,14 @@ class RestClient implements RestClientInterface { ...@@ -154,6 +154,14 @@ class RestClient implements RestClientInterface {
return $this; 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} * {@inheritdoc}
*/ */
...@@ -377,7 +385,7 @@ class RestClient implements RestClientInterface { ...@@ -377,7 +385,7 @@ class RestClient implements RestClientInterface {
foreach ($response->data as $version) { foreach ($response->data as $version) {
$versions[$version['version']] = $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; return $versions;
} }
...@@ -417,7 +425,7 @@ class RestClient implements RestClientInterface { ...@@ -417,7 +425,7 @@ class RestClient implements RestClientInterface {
} }
else { else {
$result = $this->apiCall('sobjects'); $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); // print_r($result);
$sobjects = []; $sobjects = [];
...@@ -485,7 +493,7 @@ class RestClient implements RestClientInterface { ...@@ -485,7 +493,7 @@ class RestClient implements RestClientInterface {
} }
else { else {
$response = new RestResponseDescribe($this->apiCall("sobjects/{$name}/describe", [], 'GET', TRUE)); $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; return $response;
} }
} }
...@@ -605,7 +613,7 @@ class RestClient implements RestClientInterface { ...@@ -605,7 +613,7 @@ class RestClient implements RestClientInterface {
foreach ($result->records() as $rt) { foreach ($result->records() as $rt) {
$record_types[$rt->field('SobjectType')][$rt->field('DeveloperName')] = $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) { if ($name != NULL) {
......
...@@ -407,4 +407,20 @@ interface RestClientInterface { ...@@ -407,4 +407,20 @@ interface RestClientInterface {
*/ */
public function getObjectTypeName(SFID $id); 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();
} }
...@@ -97,6 +97,8 @@ class RestClientTest extends UnitTestCase { ...@@ -97,6 +97,8 @@ class RestClientTest extends UnitTestCase {
if (empty($methods)) { if (empty($methods)) {
$methods = $this->methods; $methods = $this->methods;
} }
$methods[] = 'getShortTermCacheLifetime';
$methods[] = 'getLongTermCacheLifetime';
$args = [ $args = [
$this->httpClient, $this->httpClient,
...@@ -113,6 +115,14 @@ class RestClientTest extends UnitTestCase { ...@@ -113,6 +115,14 @@ class RestClientTest extends UnitTestCase {
->setMethods($methods) ->setMethods($methods)
->setConstructorArgs($args) ->setConstructorArgs($args)
->getMock(); ->getMock();
$this->client->expects($this->any())
->method('getShortTermCacheLifetime')
->willReturn(0);
$this->client->expects($this->any())
->method('getLongTermCacheLifetime')
->willReturn(0);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment