From 542910cb6d7ec9fc328221107c48261b5ee6c638 Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 20 Aug 2024 15:47:32 +0200 Subject: [PATCH] bug fixed in get_available_tags: do not always use persistent_cache ... also optimize tag name fetching --- include/functions_tag.inc.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/include/functions_tag.inc.php b/include/functions_tag.inc.php index 052b1a150..fb54ec2f6 100644 --- a/include/functions_tag.inc.php +++ b/include/functions_tag.inc.php @@ -41,6 +41,8 @@ function get_available_tags($tag_ids=array()) { global $persistent_cache, $user; + $use_persistent_cache = true; + // we can find top fatter tags among reachable images $query = ' SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter @@ -59,6 +61,8 @@ SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter if (is_array($tag_ids) and count($tag_ids) > 0) { + $use_persistent_cache = false; + $query .= ' AND tag_id IN ('.implode(',', $tag_ids).') '; @@ -68,11 +72,18 @@ SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter GROUP BY tag_id ;'; - $cache_key = $persistent_cache->make_key(__FUNCTION__.$user['id'].$user['cache_update_time']); - if (!$persistent_cache->get($cache_key, $tag_counters)) + if ($use_persistent_cache) + { + $cache_key = $persistent_cache->make_key(__FUNCTION__.$user['id'].$user['cache_update_time']); + if (!$persistent_cache->get($cache_key, $tag_counters)) + { + $tag_counters = query2array($query, 'tag_id', 'counter'); + $persistent_cache->set($cache_key, $tag_counters); + } + } + else { $tag_counters = query2array($query, 'tag_id', 'counter'); - $persistent_cache->set($cache_key, $tag_counters); } if ( empty($tag_counters) ) @@ -83,15 +94,21 @@ SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter $query = ' SELECT * FROM '.TAGS_TABLE; + + if (count($tag_counters) < 1000) + { + $query .= ' + WHERE id IN ('.implode(',', array_keys($tag_counters)).') +'; + } $result = pwg_query($query); $tags = array(); while ($row = pwg_db_fetch_assoc($result)) { - $counter = intval(@$tag_counters[ $row['id'] ]); - if ( $counter ) + if (isset($tag_counters[ $row['id'] ])) { - $row['counter'] = $counter; + $row['counter'] = intval($tag_counters[ $row['id'] ]); $row['name'] = trigger_change('render_tag_name', $row['name'], $row); $tags[] = $row; }