From 44ef29f225f736431d4ecd2e76c327951c8b52c8 Mon Sep 17 00:00:00 2001 From: Artom Lifshitz Date: Tue, 13 May 2025 12:39:52 -0400 Subject: [PATCH] FUP to PR#231 - keywords search dropdown for advanced search * Remove migration and rely on cron * Remove APCU as it's a key-value cache, and doesn't make much sense to use for a single JSON blob --- application/config/constants.php | 1 - application/config/migration.php | 2 +- .../cron/Keywords_cache_update.php | 22 ------ .../005_write_keywords_initial_cache.php | 34 --------- application/models/Keyword_model.php | 70 +------------------ 5 files changed, 2 insertions(+), 127 deletions(-) delete mode 100644 application/migrations/005_write_keywords_initial_cache.php diff --git a/application/config/constants.php b/application/config/constants.php index c12a4049..07d7e74f 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -97,7 +97,6 @@ //Cache for autocompleting Keywords field in Catalog Advanced Search define('CACHE_DIR', '../application/cache/'); define('KEYWORDS_CACHE_FILENAME', 'keywords_cache.txt'); -define('KEYWORDS_CACHE_KEY_APCU', 'keywords_cache'); /* End of file constants.php */ /* Location: ./application/config/constants.php */ diff --git a/application/config/migration.php b/application/config/migration.php index d9cadccf..185232aa 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -2,5 +2,5 @@ $config['migration_enabled'] = TRUE; $config['migration_type'] = 'sequential'; -$config['migration_version'] = 5; +$config['migration_version'] = 4; $config['migration_path'] = APPPATH . 'migrations/'; diff --git a/application/controllers/cron/Keywords_cache_update.php b/application/controllers/cron/Keywords_cache_update.php index 723dbb39..1479143e 100644 --- a/application/controllers/cron/Keywords_cache_update.php +++ b/application/controllers/cron/Keywords_cache_update.php @@ -21,29 +21,7 @@ public function update_cache() $query = $this->db->query($sql); $json_encoded_keywords_cache = json_encode($query->result_array()); - // Use following two lines for file-based cache. - // If using file-based cache, make sure that autocomplete() function in - // application/models/Keyword_model.php is also using file-based caching - $this->load->helper('file'); write_file(CACHE_DIR . KEYWORDS_CACHE_FILENAME, $json_encoded_keywords_cache); - -/** - // Use following for apcu cache. - // If using apcu cache, make sure that autocomplete() function in - // application/models/Keyword_model.php is also using apcu caching - - if (apcu_exists(KEYWORDS_CACHE_KEY_APCU)) - { - // overwrite existing data for this key - apcu_store(KEYWORDS_CACHE_KEY_APCU, $json_encoded_keywords_cache); - } - else - { - // must use apcu_add if key not already present in cache - apcu_add(KEYWORDS_CACHE_KEY_APCU, $json_encoded_keywords_cache); - } -**/ - } } diff --git a/application/migrations/005_write_keywords_initial_cache.php b/application/migrations/005_write_keywords_initial_cache.php deleted file mode 100644 index b3d12390..00000000 --- a/application/migrations/005_write_keywords_initial_cache.php +++ /dev/null @@ -1,34 +0,0 @@ -db->query($sql); - $json_encoded_keywords_cache = json_encode($query->result_array()); - - // Use following two lines to create a file-based cache. - $this->load->helper('file'); - write_file(CACHE_DIR . KEYWORDS_CACHE_FILENAME, $json_encoded_keywords_cache); - - // Use following line to create an apcu cache. - - // apcu_add(KEYWORDS_CACHE_KEY_APCU, $json_encoded_keywords_cache); - - } - - public function down() - { - ; - } -} diff --git a/application/models/Keyword_model.php b/application/models/Keyword_model.php index e7b61694..d052d560 100644 --- a/application/models/Keyword_model.php +++ b/application/models/Keyword_model.php @@ -10,22 +10,7 @@ public function get_applied($list_keywords) } - /** - public function get_all_keywords_used_in_projects() - { - error_log("In get_all_ keywords_used_in_project"); - $sql = 'SELECT DISTINCT k.value - FROM keywords k - JOIN project_keywords pk - ON k.id = pk.keyword_id'; - - $query = $this->db->query($sql); - return $query->result_array(); - } - - **/ - - public function autocomplete($term) + public function autocomplete($term) { // To minimise the number of database calls in our system if multiple // users are entering values in the Advanced Search Keywords field at the @@ -36,18 +21,9 @@ public function autocomplete($term) $json_encoded_keywords_cache_as_read_from_file = ''; $cached_result_array = array(); - // Use following two lines for file-based cache. - // If using file-based cache, make sure that /application/controllers/cron/Keywords_cache_update - // is writing a file-based cache $this->load->helper('file'); $json_encoded_keywords_cache = read_file(CACHE_DIR . KEYWORDS_CACHE_FILENAME); - // Use following line for apcu cache. - // If using apcu cache, make sure that /application/controllers/cron/Keywords_cache_update - // is writing to apcu cache - - // $json_encoded_keywords_cache = apcu_fetch(KEYWORDS_CACHE_KEY_APCU); - $associative = true; $keywords_cache_array = json_decode($json_encoded_keywords_cache, $associative); @@ -102,50 +78,6 @@ public function autocomplete($term) return $cached_result_array; } // end of function - -/** - // The following code achieves a result similar to the cache-based system - // above, but without case-insensitive matching. The argument for favouring - // a cache-based system over the one below is that the one below makes heavier - // demands on our database and shows poorer performance under load testing. - - // Leaving the code below here for the present to facilitate easy comparison - // of caching versus non-caching approaches - - // Escaping -- https://www.codeigniter.com/userguide3/database/queries.html#escaping-queries - $escaped_term = $this->db->escape_like_str($term); - - // For extra safety, parameterise the query as well - - $params = []; - array_push($params, $escaped_term . "%"); - array_push($params, $escaped_term); - array_push($params, "%" . $escaped_term . "%"); - array_push($params, $escaped_term . "%"); - - $sql = 'SELECT DISTINCT k.value, "A" AS priority - FROM keywords k - JOIN project_keywords pk - ON k.id = pk.keyword_id - WHERE k.value LIKE ? - UNION - SELECT "=== some other keywords containing \"?\" ===" AS value, "B" AS priority - UNION - SELECT DISTINCT k.value, "C" AS priority - FROM keywords k - JOIN project_keywords pk - ON k.id = pk.keyword_id - WHERE k.value LIKE ? - AND k.value NOT LIKE ? - ORDER BY priority ASC, value ASC - LIMIT 200'; - - $query = $this->db->query($sql, $params); - return $query->result_array(); - - - } -**/ //return comma delimited list of keywords from project public function create_keyword_list($project_id)