Activity::prefetch()

Description #

Prefetch question, answer, comment and user data.

Firstly it group ids and then fetch data by ids in single query and then cache each of them. Using this method reduces mysql queries and also speed up site.

Changelog #

VersionDescription
4.1.2Introduced.

Source #

File: includes/class/class-activity.php

	private function prefetch() {
		foreach ( (array) $this->objects as $key => $activity ) {
			// Add question and answer id.
			if ( ! empty( $activity->q_id ) ) {
				$this->add_prefetch_id( 'post', $activity->q_id );
				$this->add_prefetch_id( 'post', $activity->a_id );
			}

			// Add comment ID.
			if ( ! empty( $activity->c_id ) ) {
				$this->add_prefetch_id( 'comment', $activity->c_id );
			}

			// Add user ID.
			if ( ! empty( $activity->user_id ) ) {
				$this->add_prefetch_id( 'user', $activity->user_id );
			}
		}

		$this->prefetch_posts();
		$this->prefetch_actors();
		$this->prefetch_comments();
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment