Support

Account

Home Forums Front-end Issues ACF backend cache

Solving

ACF backend cache

  • Are there any ACF-specific keys/group to be cleared with regard to caching?

    We have Redis setup as the backend cache for WordPress. In most all cases, when an ACF field is edited on a post, the changes do not show up until the cache is flushed. I think it’s acceptable to flush specific groups or target a specific key, but it doesn’t seem logical to have to flush the entire cache for WordPress.

    Current logic is something like this.

    
    	add_filter( 'acf/save_post', 'acf_clear_object_cache' );
    
    	/**
    	 * Intended to clear a post's cache
    	 */
    	public function acf_clear_object_cache( $post_id ) {
    		if ( empty( $_POST['acf'] ) ) {
    			return;
    		}
    		
    		// clear post related cache
    		clean_post_cache( $post_id );
    		
    		// clear ACF cache
    		$acf_cache_cleared = wp_cache_delete( 'acf-post', 'acf' );
    		
    		// clear all cache if no specific key/group is found
    		if ( !$acf_cache_cleared ) {
    			wp_cache_flush();
    		}
    	}
    
  • After doing some digging in your ACF code, I may have a more targeted solution from the acf_get_value() function. This is untested, but assuming this is where the value is cached, it should easily be cleared on post update hook.

    	public function acf_clear_object_cache( $post_id ) {
    		if ( empty( $_POST['acf'] ) ) {
    			return;
    		}
    		
    		// clear post related cache
    		clean_post_cache( $post_id );
    		
    		// clear ACF cache
    		if ( is_array( $_POST['acf'] ) ) {
    			foreach ( $_POST['acf'] as $field_name => $value ) {
    				$cache_slug = "load_value/post_id={$post_id}/name={$field_name}";
    				wp_cache_delete( $cache_slug, 'acf' );
    			}
    		}
    	}
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF backend cache’ is closed to new replies.