-
Notifications
You must be signed in to change notification settings - Fork 799
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
Add caching for VideoPress functions with meta queries #14803
Changes from 1 commit
9608b4b
f91ef7c
96a565a
67df908
85b14a0
21dc2cd
073e096
69447b8
331f265
5c05e33
64918b1
e630175
1e163aa
c13c629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,8 +73,6 @@ function videopress_get_video_details( $guid ) { | |
* | ||
* Modified from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ | ||
* | ||
* @todo: Add some caching in here. | ||
* | ||
* @param string $url | ||
* | ||
* @return int|bool Attachment ID on success, false on failure | ||
|
@@ -87,13 +85,21 @@ function videopress_get_attachment_id_by_url( $url ) { | |
// Is URL in uploads directory? | ||
if ( false !== strpos( $url, $dir ) ) { | ||
|
||
$file = basename( $url ); | ||
$file = basename( $url ); | ||
$cache_key = 'videopress_get_attachment_id_by_url_' . md5( $url ); | ||
$cache_group = 'videopress'; | ||
$cached_id = wp_cache_get( $cache_key, $cache_group ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick question; did you consider using transients instead of object caching? Would it be better for performance on sites that do not use caching plugins? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, I'll look at that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I could add a transient here if you'd like. But since the attachment ID is from a database query (WP_Query), I'm not sure that using a transient to store it in the database (wp_options) would be the best approach. It would probably improve performance for sites without persistent object caching. But it could fill the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kienstra Expiring transients aren't autoloaded and are deleted once they expire. Do you think we need to worry about polluting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kienstra Also, is I think the original usage was in I ran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thanks, what do you think about the latest commits, which apply those 2 points? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm, did you have in mind something like: /**
* Using a GUID, find a post ID.
*
* @param string $guid The guid to use to find the post ID.
* @return int|false The post ID.
*/
function video_get_post_id_by_guid( $guid ) {
$post = video_get_post_by_guid( $guid );
if ( is_object( $post ) && 'WP_Post' === get_class( $post ) ) {
return $post->ID;
}
return false;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What I thought is that we can use both transients and wp_cache at the same time, each one for a purpose. Transients to store the post ID, so we avoid making the costly SQL SELECT many times though several requests. Object Caching to avoid calling
Not exactly. I would actually leave the Looks a bit cleaner to me. Each method doing just one thing, and avoiding things like Also it's important to add an expiration time for the transient. This is how I see it. (Please consider this just as an opinion!) function video_get_post_by_guid( $guid ) {
// ...
$cached_post = wp_cache_get( $cache_key, $cache_group );
if ( $cached_post ) {
return $cached_post;
}
$post_id = $this->video_get_post_id_by_guid( $guid );
if ( is_int( $post_id ) ) {
$post = get_post( $cached_post );
wp_cache_set( $cache_key, $post, $cache_group, HOUR_IN_SECONDS );
if ( $post ) {
return $post;
}
}
}
function video_get_post_id_by_guid( $guid ) {
// ...
$cached_id = get_transient( $cache_key );
if ( $cached_id ) {
return $cached_id;
}
// do the query
set_transient( $cache_key, $id, HOUR_IN_SECONDS );
return $id;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, and sorry for the delay. I'm working on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this look? It mainly uses your snippet above. |
||
if ( false !== $cached_id ) { | ||
return $cached_id; | ||
} | ||
|
||
$query_args = array( | ||
'post_type' => 'attachment', | ||
'post_status' => 'inherit', | ||
'fields' => 'ids', | ||
'meta_query' => array( | ||
'post_type' => 'attachment', | ||
'post_status' => 'inherit', | ||
'fields' => 'ids', | ||
'no_found_rows' => true, | ||
'update_post_term_cache' => false, | ||
'meta_query' => array( | ||
array( | ||
'key' => '_wp_attachment_metadata', | ||
'compare' => 'LIKE', | ||
|
@@ -111,7 +117,9 @@ function videopress_get_attachment_id_by_url( $url ) { | |
$cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); | ||
|
||
if ( $original_file === $file || in_array( $file, $cropped_files ) ) { | ||
return (int) $attachment_id; | ||
$attachment_id = (int) $attachment_id; | ||
wp_cache_set( $cache_key, $attachment_id, $cache_group, HOUR_IN_SECONDS ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the cache expiration time should be different |
||
return $attachment_id; | ||
} | ||
} | ||
} | ||
|
@@ -637,13 +645,28 @@ function video_image_url_by_guid( $guid, $format ) { | |
* Using a GUID, find a post. | ||
* | ||
* @param string $guid | ||
* @return WP_Post | ||
* @return WP_Post|false The post for that guid, or false if none is found. | ||
*/ | ||
function video_get_post_by_guid( $guid ) { | ||
$cache_key = 'video_get_post_by_guid_' . $guid; | ||
$cache_group = 'videopress'; | ||
$cached_post_id = wp_cache_get( $cache_key, $cache_group ); | ||
|
||
if ( false !== $cached_post_id ) { | ||
$cached_post = get_post( $cached_post_id ); | ||
if ( $cached_post ) { | ||
return $cached_post; | ||
} else { | ||
// The cached post ID doesn't belong to a post, so delete it. | ||
wp_cache_delete( $cache_key, $cache_group ); | ||
} | ||
} | ||
|
||
$args = array( | ||
'post_type' => 'attachment', | ||
'post_mime_type' => 'video/videopress', | ||
'post_status' => 'inherit', | ||
'no_found_rows' => true, | ||
'meta_query' => array( | ||
leogermani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array( | ||
'key' => 'videopress_guid', | ||
|
@@ -655,9 +678,13 @@ function video_get_post_by_guid( $guid ) { | |
|
||
$query = new WP_Query( $args ); | ||
|
||
$post = $query->next_post(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There can be an error here if the query didn't find any posts. |
||
if ( $query->have_posts() ) { | ||
$post = $query->next_post(); | ||
wp_cache_set( $cache_key, $post->ID, $cache_group, HOUR_IN_SECONDS ); | ||
return $post; | ||
} | ||
|
||
return $post; | ||
return false; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
unset( $video );
above, it looks likeisset( $video->expires )
will never be trueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch.