• Resolved Mínimo

    (@minimoio)


    It would be great to have a method to get cache_key from object id, so we can avoid custom functions like the one below. What do you think? Does it already exists? Thanks

    function hm_getPostCacheKeys($postID){
      global $wpdb;
      $sql = "SELECT  <code>cache_id</code>
                  FROM    .<code>{$wpdb->prefix}wrc_relations</code>
                  WHERE   <code>object_id</code> = %s
                  LIMIT   %d";
      // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
      $caches = $wpdb->get_results( $wpdb->prepare( $sql, $postID, 1 ) );
    
      if (!isset($caches) || empty($caches)) return false;
    
      $keys = Array();
      foreach ( $caches as $cache ) {
    
        $sql = "SELECT  <code>cache_key</code>
                    FROM    <code>{$wpdb->prefix}wrc_caches</code>
                    WHERE   <code>cache_id</code> = %s
                    ";
        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
        $cachesKeys = $wpdb->get_results( $wpdb->prepare( $sql, $cache->cache_id) );
        if ($cachesKeys){
          foreach ( $cachesKeys as $key ) {
            $keys[] = $key->cache_key;
          }
        }
    
      }
    
      return $keys;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @minimoio

    Thank you for using our plugin!

    Currently we do not have such a function, may I ask why you would need the cache keys?

    Thread Starter Mínimo

    (@minimoio)

    Hi Richard, in our case we needed to clear the cache **only** for the specific posts being updated, for our custom endpoints that was not being cleared by the current version of the plugin.

    So we can do this:

      $caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
    
      $a_cacheKeys = hm_getPostCacheKeys($postID, $postCacheType);
      foreach ( $a_cacheKeys as $key ) {
        $ret = $caching->delete_cache($key);
      }

    Cheers!

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @minimoio

    Sorry for the very late reply.

    I believe what you want could be achieved with the following code:

    $caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
    
    $caching->delete_related_caches( $postID, $postCacheType );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Have a method to get cache_key from object id?’ is closed to new replies.