Reduced excessive database queries#539
Conversation
There was a problem hiding this comment.
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.
| * @return void | ||
| */ | ||
| public static function flush_cache() { | ||
| wp_cache_set( 'ppom_meta_cache_version', time(), 'ppom_meta' ); |
There was a problem hiding this comment.
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.
| 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' ); |
| * Flush meta cache by updating the cache version. | ||
| * @return void | ||
| */ | ||
| public static function flush_cache() { |
There was a problem hiding this comment.
A quick question here:
- What is the advantage of this version-based cache compared to just https://developer.wordpress.org/reference/functions/wp_cache_flush_group/?
|
@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. |
|
@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! |
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.
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