mirror of
https://github.com/bckelley/avogadrio.git
synced 2026-08-24 02:04:11 -05:00
Make cache optional on SMILES converter
This commit is contained in:
+18
-5
@@ -26,10 +26,19 @@ class SmilesConverter
|
||||
*
|
||||
* @param Database $db the database in which to cache lookup results
|
||||
*/
|
||||
public function __construct($db) {
|
||||
public function __construct($db = null) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether or not the API result cache is enabled.
|
||||
*
|
||||
* @return bool true if the cache is enabled, otherwise false
|
||||
*/
|
||||
public function isCacheEnabled() {
|
||||
return $this->db !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a compound name to SMILES notation.
|
||||
*
|
||||
@@ -42,9 +51,11 @@ class SmilesConverter
|
||||
$encoded = urlencode($name);
|
||||
|
||||
// Check if we've got the name cached already.
|
||||
$cachedSmiles = $this->db->get('smiles', 'name', $encoded);
|
||||
if ($cachedSmiles !== null) {
|
||||
return $cachedSmiles;
|
||||
if ($this->isCacheEnabled()) {
|
||||
$cachedSmiles = $this->db->get('smiles', 'name', $encoded);
|
||||
if ($cachedSmiles !== null) {
|
||||
return $cachedSmiles;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert chemical name to SMILES if we can using API.
|
||||
@@ -54,7 +65,9 @@ class SmilesConverter
|
||||
// If request was successful.
|
||||
if ($response->getStatusCode() === 200) {
|
||||
$smiles = $response->getBody()->getContents();
|
||||
$this->db->insert(['name' => $encoded, 'smiles' => $smiles]); // Cache name for future.
|
||||
if ($this->isCacheEnabled()) {
|
||||
$this->db->insert(['name' => $encoded, 'smiles' => $smiles]); // Cache name for future.
|
||||
}
|
||||
return $smiles;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user