Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastore for Theme-Options and Meta producing massive duplicate queries because lack of caching #1271

Open
jhalitschke opened this issue Jan 17, 2025 · 1 comment

Comments

@jhalitschke
Copy link

The SELECTs in

core/Datastore/Theme_Options_Datastore.php
core/Datastore/Meta_Datastore.php

should use the Cache API to avoid hundreds of duplicate queries (in our case 350 for a mid-complex category page, using 30 theme options and 4 meta keys)

I've tried this for theme options and got no more duplicates:

		// build a safe cache key and group from class name, field name and storage key comparisons
		$cache_key = 'crb_' . md5( get_class( $this ) . $field->get_name() . $storage_key_comparisons );
		$cache_group = 'crb_datastore';

		// Try to get the cached result
		$storage_array = wp_cache_get( $cache_key, $cache_group );

		if ( $storage_array === false ) {
			// If cache is empty, perform the query
			$storage_array = $wpdb->get_results( '
				SELECT `option_name` AS `key`, `option_value` AS `value`
				FROM ' . $wpdb->options . '
				WHERE ' . $storage_key_comparisons . '
				ORDER BY `option_name` ASC
			' );

			// Store the result in cache
			wp_cache_set( $cache_key, $storage_array, $cache_group, 3600 ); // Cache TTL should be configurable
		}
@HeikoMamerow
Copy link

+1

This is an real performance booster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants