Function: CACHING

NOTE

This behavior will significantly change in version 0.3.

Wonderful Relations applies query caching in two distinct ways:

  • Caching for Query Generation
  • Caching for Query Execution

Caching for Query Generation

When generating a query that relies on other queries or joins, it is inefficient to regenerate it every time. Instead, the query only needs to be generated once and updated if changes occur. Wonderful Relations functions as a query builder, managing updates dynamically when modifications happen.

Parameter:

  • Cache Query

Caching a query involves more than just storing the main query structure. If a query includes temporary tables, these must also be cached because they need to be created before executing the query. Additionally, since there are normal queries and count queries, both versions must be stored.

If the cache_query parameter is enabled, the resulting SQL queries are stored in the following columns of the wp_wr_query table:

  • cached_query – Stores the complete SQL query.
  • cached_count_query – Stores the query specifically for counting results.
  • temporary_tables – Stores any required temporary tables.

Caching for Query Execution

The second major caching mechanism is used when queries are repeatedly executed.

Example:

When generating a query, multiple configuration tables may be accessed multiple times. One of the most frequently used queries is get_query_by_identifier, which is executed before every query when no ID is provided.

To optimize this process, Wonderful Relations utilizes a singleton pattern. This ensures that:

  • Queries are only loaded once per instance, reducing redundant database access.
  • Cached versions of count queries and temporary tables are reused efficiently.

This approach significantly improves performance by avoiding unnecessary query execution while ensuring consistency across multiple instances of the same query.