Skip to content

Reduced excessive database queries#539

Open
girishpanchal30 wants to merge 7 commits intodevelopmentfrom
bugfix/pro/552
Open

Reduced excessive database queries#539
girishpanchal30 wants to merge 7 commits intodevelopmentfrom
bugfix/pro/552

Conversation

@girishpanchal30
Copy link
Copy Markdown
Contributor

Summary

Implemented a cache mechanism to reduce database queries and improve performance.

Check before Pull Request is ready:

Closes https://github.com/Codeinwp/ppom-pro/issues/552

@girishpanchal30 girishpanchal30 requested a review from Copilot March 26, 2026 13:55
@girishpanchal30 girishpanchal30 added the pr-checklist-skip Allow this Pull Request to skip checklist. label Mar 26, 2026
@pirate-bot pirate-bot added the pr-checklist-complete The Pull Request checklist is complete. (automatic label) label Mar 26, 2026
@pirate-bot
Copy link
Copy Markdown
Contributor

pirate-bot commented Mar 26, 2026

Plugin build for 1b86067 is ready 🛎️!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a WordPress object-cache layer around PPOM meta reads to reduce repeated database queries, with cache invalidation triggered after meta mutations in admin flows.

Changes:

  • Add a cache “version” mechanism (get_cache_version() / flush_cache()) to invalidate PPOM meta caches.
  • Cache PPOM settings, fields, category/tag mappings, and settings rows using wp_cache_get/wp_cache_set.
  • Call PPOM_Meta::flush_cache() after create/update/delete actions and category/tag updates.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.

File Description
inc/admin.php Flushes PPOM meta cache after admin CRUD operations on meta rows.
classes/ppom.class.php Implements cache versioning + wraps several DB reads with object caching.
classes/admin.class.php Flushes PPOM meta cache after saving categories/tags.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread classes/ppom.class.php Outdated
Comment thread classes/ppom.class.php Outdated
Comment thread classes/ppom.class.php
Comment thread classes/ppom.class.php Outdated
Comment thread classes/ppom.class.php Outdated
* @return void
*/
public static function flush_cache() {
wp_cache_set( 'ppom_meta_cache_version', time(), 'ppom_meta' );
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush_cache() sets the cache version using time() (seconds). If the cache is flushed multiple times within the same second, the version may not change and previously cached values will continue to be served, leading to stale meta/fields/settings after updates. Use a monotonic version bump (e.g., read current version and increment it) or a higher-resolution value to guarantee the version changes on every flush.

Suggested change
wp_cache_set( 'ppom_meta_cache_version', time(), 'ppom_meta' );
$version = self::get_cache_version();
$version++;
wp_cache_set( 'ppom_meta_cache_version', $version, 'ppom_meta' );

Copilot uses AI. Check for mistakes.
Comment thread classes/ppom.class.php Outdated
Comment thread classes/ppom.class.php Outdated
Comment thread inc/admin.php Outdated
Comment thread classes/admin.class.php Outdated
Comment thread classes/ppom.class.php Outdated
Comment thread classes/ppom.class.php Outdated
* Flush meta cache by updating the cache version.
* @return void
*/
public static function flush_cache() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick question here:

@selul
Copy link
Copy Markdown
Contributor

selul commented Mar 27, 2026

@girishpanchal30 the current solution is not very scalable and is just patching things here and there. Please research a better caching strategy for the table and it's iteraction and propose an unified api for access the table data that includes caching aswell.

@girishpanchal30
Copy link
Copy Markdown
Contributor Author

@selul, I have updated the database query caching mechanism with the latest commit. Please check it and let me know if any changes are needed. Thanks!

Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php Outdated
Comment thread classes/db-meta.class.php
Comment thread classes/admin.class.php Outdated
Comment thread classes/ppom.class.php Outdated
Comment thread inc/admin.php
Comment thread classes/db-meta.class.php
Comment on lines +48 to +67
public static function get( $id ) {
$id = absint( trim( $id ) );
if ( ! $id ) {
return null;
}

$cache_key = "ppom_meta_{$id}";
$meta = wp_cache_get( $cache_key, self::CACHE_GROUP );

if ( false === $meta ) {
global $wpdb;
$table = self::get_table_name();
$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE productmeta_id = %d", $id ) );

// Cache even if null to avoid repetitive queries for non-existent IDs
wp_cache_set( $cache_key, $meta, self::CACHE_GROUP, DAY_IN_SECONDS );
}

return $meta;
}
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New caching behavior is introduced via PPOM_Meta_DB (row caching + lookup caching), but there are no tests covering cache hit/miss behavior or invalidation on insert/update/delete. Given the existing PHP unit test suite under tests/, it would be valuable to add tests asserting that get()/get_multiple() do not re-query when cached, and that caches are invalidated after mutations.

Copilot uses AI. Check for mistakes.
Comment thread classes/db-meta.class.php
@Soare-Robert-Daniel Soare-Robert-Daniel self-requested a review April 1, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-checklist-complete The Pull Request checklist is complete. (automatic label) pr-checklist-skip Allow this Pull Request to skip checklist.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants