diff --git a/classes/engine.php b/classes/engine.php index 27dd73b..75c8005 100644 --- a/classes/engine.php +++ b/classes/engine.php @@ -60,6 +60,11 @@ class engine extends \core_search\engine { */ protected $count = 0; + /** + * @var cache_application|cache_session|cache_store The cache storage. + */ + protected $cache = null; + /** * * @var array Configuration defaults. @@ -87,6 +92,8 @@ public function __construct() { set_config($name, $value, 'search_elastic'); } + $this->cache = \cache::make('search_elastic', 'elasticserver'); + } /** @@ -181,12 +188,19 @@ public function validate_index() { * @return object Apache Lucene version details. */ private function get_es_version_details() { - $url = $this->get_url(); - $client = new \search_elastic\esrequest(); - $response = $client->get($url); - $responsebody = json_decode($response->getBody()); + $data = $this->cache->get('es_version'); + + if ($data === false) { + $url = $this->get_url(); + $client = new \search_elastic\esrequest(); + $response = $client->get($url); + $responsebody = json_decode($response->getBody()); + + $data = $responsebody->version; + $this->cache->set('es_version', $responsebody->version); + } - return $responsebody->version; + return $data; } /** diff --git a/db/caches.php b/db/caches.php new file mode 100644 index 0000000..5ecea89 --- /dev/null +++ b/db/caches.php @@ -0,0 +1,32 @@ +. + +/** + * Elasticsearch web service external functions and service definitions. + * + * @package search_elastic + * @copyright 2017 Matt Porritt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$definitions = [ + 'elasticserver' => [ + 'mode' => cache_store::MODE_APPLICATION, + 'ttl' => 5 * MINSECS, + ] +]; \ No newline at end of file diff --git a/lang/en/search_elastic.php b/lang/en/search_elastic.php index f5f0ac3..31a889a 100644 --- a/lang/en/search_elastic.php +++ b/lang/en/search_elastic.php @@ -24,6 +24,7 @@ $string['pluginname'] = 'Elastic'; $string['pluginname_help'] = 'Search backend for the Elasticsearch search engine'; +$string['cachedef_elasticsearch'] = 'Cache server\'s metadata'; $string['addfail'] = 'Failed to add document to index'; $string['advsettings'] = 'Advanced settings';