• Resolved nicoleadoff

    (@nicoleadoff)


    Hello, I dont understand how the cron works.
    In the cleanup_deleted_caches() function the DB query is selecting cache records where “expiration = date_i18n( ‘Y-m-d H:i:s’, 1 )”.

    /**
    	 * Function called by a cron job to delete flushed or deleted caches from the transients API.
    	 *
    	 * @return void
    	 */
    	public function cleanup_deleted_caches() {
    		global $wpdb;
    
    		/**
    		 * How many caches should be cleanup in each run?
    		 *
    		 * Allows to change the number of cleaned up caches per cron run.
    		 *
    		 * @since 2020.2.0
    		 *
    		 * @param int $limit The maximum number of cleaned up caches per cron run.
    		 */
    		$limit = (int) apply_filters( 'wp_rest_cache/max_cleanup_caches', 1000 );
    
    		$sql = "SELECT  cache_key, deleted
                    FROM    {$this->db_table_caches}
                    WHERE   expiration = %s
                    AND     cleaned = %d
                    LIMIT   %d";
    		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    		$caches = $wpdb->get_results( $wpdb->prepare( $sql, date_i18n( 'Y-m-d H:i:s', 1 ), 0, $limit ) );
    		if ( $caches ) {
    			foreach ( $caches as $cache ) {
    				$this->delete_cache( $cache->cache_key, $cache->deleted );
    			}
    		}

    I dont understand how it can work ?
    What is the probability that the expiration date is equal to the current date to the nearest second?

    Thanks for your help.

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

    (@rockfire)

    Hi @nicoleadoff

    Thank you for using our plugin!

    “expiration = date_i18n( ‘Y-m-d H:i:s’, 1 )” doesn’t mean current date to the nearest second. It means Unix timestamp of 1 second, or to translate it to a date: 1970-01-01 00:00:01

    Let me explain this timestamp: When the expiration is set to a timestamp in the future, our plugin considers the cache record as valid. When expiration is set to a timestamp in the past, our plugin treats is as expired and will not serve the cache record. So far nothing special.
    When expiration is set to 0, it is considered to be unlimited, so no expiration.

    Now when a user deletes or flushes cache records manually, for performance reasons we do not immediately delete the transient cache record, we do this via a cron job. So when it is deleted or flushed manually we set the expiration to 1 second (there is no chance this can happen in any other situation), so we know it was done manually, but we also know it will not return a cache record since expiration is in the past.

    Thread Starter nicoleadoff

    (@nicoleadoff)

    Thanks for your reply! I understand now how the cron job works.

    In our REST API we have a list of resources that are heavy to compute, so we want to prefill the cache in background. During this process I want to serve the current cache value stored in the cache.

    Is it possible to prefill the cache value with a cron job before the expiration date so that there no latency for users ?

    Can I implement it with some magic hook ?

    Thanks a lot for your help

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Question about cleanup_deleted_caches()’ is closed to new replies.