Support » Plugin: WP REST Cache » Add Option Or Filter for Change schedule cleanup Time

  • Resolved Mehrshad Darzi

    (@mehrshaddarzi)


    I want to clean up cache faster, I see this code in the class-caching.php line 1219:

    
    wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_rest_cache_cleanup_deleted_caches' );
    

    Please new apply_filters for this time in your code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @mehrshaddarzi

    THank you for using our plugin!

    May I ask why you would want to change the timing on this cleanup? The caches that are being cleaned by this function are already no longer being used, so it is just some simple garbage collection and I don’t see why it would make any difference to do this after 5 minutes or to do it after (let’s say) 1 minute?

    Thread Starter Mehrshad Darzi

    (@mehrshaddarzi)

    when I edit the post title in the WordPress admin area.your plugin change post_title after 5 minutes in Endpoint post lists.
    You do not think it is better for the user to be able to change this?

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @mehrshaddarzi

    This cleanup code shouldn’t have anything to do with it. What it does (or at least should do) is the following:
    Once a cache is being invalidated (because for instance you edited the post) it is marked as deleted in the database and that cache is no longer used when requesting the endpoint. After 5 minutes a cron job is run to clean up the actual transients. It is done this way because transients need to be trashed one-by-one and doing it directly after you edit a post could slow things down significantly (a single post could be in a lot of caches, which all need to be deleted).

    Now what you are saying is that after you edit a post, the cache still returns the old value until the garbage collection is done? I would have to investigate this, this shouldn’t happen. If this is indeed happening changing the 5 minutes would only mask a bug, so I want to investigate this and solve the bug rather than have you change the inteval.

    Thread Starter Mehrshad Darzi

    (@mehrshaddarzi)

    Please see the circle of updated cache post in your plugin:

    1) you have an action:

    
    add_action( 'save_post', [ $caching, 'save_post' ], 999, 3 );
    

    2) in this action if post is updated call:

    
    if ( $update ) {
    			$this->delete_related_caches( $post_id, $post->post_type );
    		} else {
    			$this->delete_object_type_caches( $post->post_type );
    		}
    
    

    3) in the delete_related_caches method first checklist of for update and set new cleanup schedule:

    
    
    if ( 0 !== $affected_rows && false !== $affected_rows ) {
    			$this->schedule_cleanup();
    		}
    

    4) your schedule_cleanup method set a new single event for 5 minutes:

    
    wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_rest_cache_cleanup_deleted_caches' );
    

    This means that the delete operation will have to wait until this time.
    I tested several times and there is this problem.
    Please Check.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @mehrshaddarzi

    Thank you for your explanation, I know how these steps work. But if you have a look at the delete_related_caches( ... ) function you can see the expiration of the cache is set to 1:

    $set_clause = 'c.expiration= %s';

    and

    $affected_rows = $wpdb->query( $wpdb->prepare( $sql, date_i18n( 'Y-m-d H:i:s', 1 ), $id, $object_type ) );

    Now in the get_cache( $cache_key ) function there is a check:

    $expiration = $this->get_cache_expiration( $cache_key );
    		if ( 1 === strtotime( $expiration ) ) {
    			return false;
    		}

    So if the expiration is set to 1, no cache should be returned (even if the transient still exists). Now for some reason (as you say) the get_cache( $cache_key ) function is returning a cache after it has been flushed? As you can see, and as I said, the problem is not with the 5 minute interval…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Option Or Filter for Change schedule cleanup Time’ is closed to new replies.