Skip to main content
Cache - Dynamic Shortcodes

Utilize the Cache Dynamic Shortcode for optimal data caching in WordPress to improve site performance and user experience.

Updated over 7 months ago

The Cache Dynamic Shortcode offers a method for temporary data storage, enabling the caching of content within WordPress. Its main benefit is to boost performance by reducing database load and cutting down on page loading times.

This shortcode allows for the caching of other Dynamic Shortcodes, and it simplifies the process of storing, fetching, and erasing cached entries with customizable expiration periods.

Syntax

The syntax for implementing the Cache Dynamic Shortcode is as follows:

  • For creating/updating cached data

    {cache:key value@keyargs}

  • For retrieving cached data

    {get-cache:key}

  • For deleting cached data

    {delete-cache:key}

Key Arguments

  • expiration (alias exp) Determines how long the data should be kept cached, specified in natural language like "30 minutes", "1 day" or "2 weeks".

Caching a Complex Query

Without Caching

Typically, retrieving the latest posts from a specific category without caching would involve running a database query every time the page loads. This can be resource-intensive, especially on a high-traffic site.

Example without Cache

{loop:{query:posts@posts_per_page=50}
{post:title}
@separator=', '
}

In this example, the {query:posts} shortcode directly queries the database each time the page is viewed, which could lead to slower page loading times as each query consumes resources.
​

With Caching

Using the Cache Dynamic Shortcode, we can store the results of this query, reducing the number of times the database needs to be accessed.
​

Example with Cache

{cache:latest_articles
{query:posts@posts_per_page=50}
@expiration="1 hour"
}

After caching, retrieving and displaying the cached data would use the following shortcode:

{loop:{get-cache:latest_articles}
{post:title}
@separator=', '
}

Why Caching is Preferable

  • Performance Improvement: By caching the query results, we avoid repeatedly querying the database. This reduces the server's workload and significantly speeds up page loading times for users.

  • Consistency: Cached data ensures that all users see the same content without slight variations that might occur if the database updates between individual user queries.

  • Resource Efficiency: Reducing the frequency of queries helps conserve server resources, which is particularly beneficial during peak traffic periods.

Managing Cache Deletion

Deleting the cache is a crucial operation to ensure that data does not become stale, especially in dynamic contexts where content frequently updates.

Example of Cache Deletion

Suppose you want to reset the stored data for latest_articles to reflect significant content updates or after a specific event, such as the publication of new articles. You can use the following shortcode:

{delete-cache:latest_articles}

This command clears the data stored under the key latest_articles, ensuring that the next page view will generate fresh data and store it again in the cache.

Important Considerations

  • Single Execution: It is vital to ensure that the delete-cache shortcode is only executed when necessary. Leaving the shortcode active on the page could lead to continuous cache clearing every time the page is loaded, negating the benefits of having a cache.

  • Removing the Shortcode: After the delete-cache shortcode has been executed and the cache has been cleared, it is advisable to remove or deactivate the shortcode from the page to prevent unintended executions. This can be managed manually.


​

Did this answer your question?