Skip to content

For 'wp cache type', use a standard information function, if available, rather than class-sniffing #5735

@OllieJones

Description

@OllieJones

Feature Request

wp cache type retrieves and displays the type of persistent object cache. Make it more easily extensible.

Describe your use case and the problem you are facing

For yet another persistent object cache implementation, this one via SQLite (https://github.com/OllieJones/sqlite-object-cache) it would be nice if thewp cache type command returned something more useful than Unknown.

Describe the solution you'd like

It uses an if / else if cascade in wp_get_cache_type() starting at https://github.com/wp-cli/wp-cli/blob/main/php/utils-wp.php#L255 to look for implementation specifics for each known type of cache and guess the cache type.

Proposal: change near line 256 to add the following bit of code. This will use a method in the cache implementation if it's available to get the cache type.

  // Use the get_cache_type method if the cache implements it.
 if ( method_exists( $wp_object_cache, 'get_cache_type' ) ) {
	  $message = $wp_object_cache->get_cache_type();
  
  // Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend)
  } elseif ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) {
    ...

This allows future object cache implementations (like mine) to have support from the wp cache type command without needing to embed any more implementation specifics in utils-wp.php. And, future releases of existing caches can easily adapt.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions