-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Cache results from Imagick::queryFormats #6936
base: trunk
Are you sure you want to change the base?
Changes from all commits
d454fa8
e017d8a
d0a1b15
d1fc4ca
5d499ed
a86a618
c9297b7
33bc1c0
6056c51
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 |
---|---|---|
|
@@ -4123,7 +4123,22 @@ function _wp_image_editor_choose( $args = array() ) { | |
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. | ||
*/ | ||
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); | ||
$supports_input = false; | ||
|
||
$editors = wp_cache_get( 'wp_image_editor_choose', 'image_editor' ); | ||
|
||
if ( ! is_array( $editors ) ) { | ||
$editors = array(); | ||
} | ||
|
||
// Cache the chosen editor implementation based on specific args and available implementations. | ||
$cache_key = md5( serialize( array( $args, $implementations ) ) ); | ||
|
||
if ( isset( $editors[ $cache_key ] ) ) { | ||
Comment on lines
+4127
to
+4136
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. I'm curious what's the rationale for nesting all the cache values in a single cache key "bucket". In most places in Core we store individual values per cache key, and the dynamic portion becomes part of the actual cache key. For example the actual cache key would be: $cache_key = 'wp_image_editor_choose_' . md5( serialize( array( $args, $implementations ) ) ); Since these caches would expire after 1 day anyway, I don't think there's any harm in storing in individual cache keys. This approach here might work too, but there's potential concerns about scalability (if too many different combinations of arguments are all stored under a single cache key) and diverging from how I believe we typically handle such caches in Core. 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. I didn't have any real intention when I chose to implement it this way, but I can see a few benefits. Generally, including the hash as part of the name is done for the purpose of avoiding stale caches (i.e., if the values that are hashed change, the This specific function can get called multiple times during the same run with different args. Having the results stored in multiple distinct caches would mean that we need to load separate cache values for each specific set of args. By saving all of the values to one cache, all of the cached values are loaded to memory the first time Another minor benefit is that it makes it a bit easier to work with a cache that has a static name in the DB instead of a dynamic one. As a theoretical example you could do something like 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. That sounds good to me, makes sense. Let's go with this approach then. |
||
return $editors[ $cache_key ]; | ||
} | ||
|
||
// Assume no support until a capable implementation is identified. | ||
$editor = false; | ||
|
||
foreach ( $implementations as $implementation ) { | ||
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) { | ||
|
@@ -4157,15 +4172,20 @@ function _wp_image_editor_choose( $args = array() ) { | |
* This implementation supports the input type but not the output type. | ||
* Keep looking to see if we can find an implementation that supports both. | ||
*/ | ||
$supports_input = $implementation; | ||
$editor = $implementation; | ||
continue; | ||
} | ||
|
||
// Favor the implementation that supports both input and output mime types. | ||
return $implementation; | ||
$editor = $implementation; | ||
break; | ||
} | ||
|
||
return $supports_input; | ||
$editors[ $cache_key ] = $editor; | ||
|
||
wp_cache_set( 'wp_image_editor_choose', $editors, 'image_editor', DAY_IN_SECONDS ); | ||
|
||
return $editor; | ||
} | ||
|
||
/** | ||
|
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.
+1 to using a global cache group.