


WordPress Web Application Development Guide: Detailed explanation of available features (Part 7): Caching Technology
One of the most important things we must always pay attention to when building web applications is performance.
As they say, performance is a feature.
Whether you're a designer, developer, or user, you know this intuitively: when it comes to apps, we hate waiting. We get frustrated when things don't work fast enough, or we have to wait longer than we think we should.
To this end, most modern web development frameworks can implement some type of caching by using some API, and WordPress (albeit a base) is no exception.
So, as we continue our discussion of why WordPress is a viable choice as a foundation for web application development, we will look at the APIs provided by the core application, how they work and how we can use them to our advantage, and how Improve performance further with additional caching plugins.
Why is caching important?
In short, caching is important because it allows us to store frequently retrieved data somewhere in memory so that it can be retrieved quickly.
When you look at the bigger picture, this becomes increasingly apparent when multiple users view a website. What I mean is, if one person (or very few people) visits a website, and that website stores its data in a database, then every time the page is loaded, that information has to be retrieved from the website. database, inserted into the page, and returned to the user.
If a certain level of cache is established, there is no need to call the database frequently. Instead, information can be pulled from an area in memory, allowing for faster retrieval and therefore faster page load times.
We will introduce the technical details of this in detail later in this article.
What about plugins?
If you’ve been using WordPress for a long time, you’re probably familiar with the many caching plugins available.
These are undoubtedly great solutions for speeding up your website and/or web application, but it does raise the question: if you can use a plugin to do this, then why should we worry about it?
Of course, this is a valid question, but plugins can only do so much on their own.
Developers can build their applications in such a way that they not only perform well without caching mechanisms, but are also greatly enhanced by said caching plugins.
What I mean is that these caching plugins look for data that themes and applications want to store somewhere, and if we can do this programatically in our own working context, then these plugins will yield a much larger Effect performance.
After understanding the available APIs, we will revisit this topic later in this article and see how they can improve the performance of cache plugins.
Transient API
In order to introduce first-level caching into our application, we need to leverage WordPress’s Transients API. First, please note that the official definition of a transient is “something that exists only for a short period of time.”
As defined in the Codex Alimentarius:
[Transient API] provides a simple and standardized way to temporarily store cached data by giving it a custom name and a time range after which the data will expire and be deleted. in the database.
But what does it mean? After all, the data is still stored in the database, so why is this better than storing the data in any other database table (such as the wp_options
table?)?
We'll discuss this in more detail once we revisit the discussion about caching and plugins.
Set transient
Setting transient values is actually as simple as storing the data in an options table.
Specifically, you need a key value that uniquely identifies the data, and then a value associated with that key. You also need an expiration time (in seconds) to keep the data in the table before refreshing the table.
Suppose we want to store the current user's name as the last or most recently active user on the site. We can do this using the wp_get_current_user()
function.
First, we will set the following values:
set_transient( 'most_recent_user', wp_get_current_user()->user_login, 12 * HOUR_IN_SECONDS )
Here, please pay attention to the following points:
- I identify the most recent user in the system as the currently logged in user and we store this for 12 hours.
- Please note that the
HOUR_IN_SECONDS
constant was introduced in WordPress 3.5. A complete list of constants is available here.
Although this is how we set up transients , this still doesn't explain how we manage transients if they don't exist or already exist.
We will cover this in detail later in this article.
Retrieve Transient
When it comes to retrieving transients, it's very similar to retrieving things like metadata or options. I mean, we just need to know the key to retrieve the information.
So, to be consistent with the example above, we can retrieve the application's most recent user with the following call:
get_transient('most_recent_user');
This will obviously return whatever type of information you have stored, or if the transient has expired (i.e. more than 12 hours have passed), the function will return a boolean of FALSE
value.
This is key to remember, especially if you are trying to read cached values and then need to get them from another data source if they are not available in temporary storage.
We'll look at a complete example of doing this before the end of this article.
Delete transient
Finally, if you need to delete a transient to remove it completely, or to remove it before its defined expiration so that it can be replaced with another value, then you can simply use the following function:
delete_transient('most_recent_user');
Additionally, if temporary value deletion is unsuccessful, the function will return FALSE
; otherwise, it will return FALSE
.
Expiring Transient
When setting the expiration time of a cached value, there are several ways to make setting the value easier than musically basic integer operations.
For example, MINUTE_IN_SECONDS
is easier to use than 60, especially when multiplying by minutes, hours, or days.
Starting with WordPress 3.5, several constants have been added to the core application to make these calculations easier to read.
Right now:
MINUTE_IN_SECONDS
HOUR_IN_SECONDS
DAY_IN_SECONDS
WEEK_IN_SECONDS
YEAR_IN_SECONDS
Easier to use, read and write, isn't it?
Full example using transients
At this point, I think it's important to understand how to set up a transient starting from storing a value in an options table.
Here's the order in which we do this:
- We will save an option in the
wp_options
table. - Next, we will check if the value exists in the cache.
- If the value does exist in the cache, we will remove it; otherwise, we will add it.
Then, in the second half of the code, we do the following:
- We will try to retrieve the value function from the cache.
- If the value exists in the cache, we will fall back to the options table; however, if the value does exist, then we will use it.
With that being said, let’s take a look:
$username = wp_get_current_user()->user_name; add_option( 'most_recent_user', $username ); // Check to see if the value exists in the cache if ( FALSE !== get_transient( 'most_recent_user' ) ) { // If it does, we'll delete it... if( delete_transient( 'most_recent_user' ) ) { // ...and store the most recent user for a maximum of one minute set_transient( 'most_recent_user', MINUTE_IN_SECONDS ); } else { // The deletion was unsuccessful, so log the error } } // Now try to read the value from the cache. if ( FALSE !== ( $username = get_transient( 'most_recent_user' ) ) { // Since it doesn't exist, then we'll read it from the option's table $username = get_option( 'most_recent_user' ); // And then we'll update the cache set_transient( 'most_recent_user', $username, MINUTE_IN_SECONDS ); }
Please note that this example is not complete - it could be refactored to be clearer and the code should be abstracted into functions more relevant to the application, but the purpose of this code is to show how to handle conditional logic, options and transients .
How does this work with plugins?
Now, with the above in mind, we can revisit the question of how to use transients to improve plug-in performance.
As we mentioned before:
After understanding the available APIs, we will revisit this topic later in this article to understand how they can enhance the performance of cache plugins.
块引用>Having said that, caching and the WordPress database have to do with the location of the data in the database.
Because transient data is stored in a different location than other data, plugins (such as those based on memcached) will look for the data where the transient data is stored, and then load the data into memory from that location.
So when data is requested, the data will be retrieved from memory. If the data does not exist, it is retrieved from the database.
The most important thing is that if programmed correctly, when reading data from the cache fails and retrieving data from the database, it will be inserted back into the cache so that it will be inserted into the cache the next time it is retrieved. available in memory.
Finally, the key thing to note about transient information is that it has an expiration date. This means that the data will only be stored in that area of the database for a specific period of time.
For this, we need to take this into account. This means that whenever we want to retrieve transients, we need to make sure they exist. If not, we pull them out of their location and store them in the correct location.
Custom query
So far, we have introduced many of the features provided by WordPress related to the basics of web application development.
But we have one last major component to cover, and that is how to handle custom queries.
Of course, there are some great APIs as it relates to running queries designed for the specific purpose of WordPress, such as
WP_Query
andWP_User_Query
, but we also You'll learn about some of the native tools we can write that allow us to make custom queries to the database using defined WordPress objects as well as methods that allow for proper data cleansing.But we’ll cover all of this and more in the next article.
The above is the detailed content of WordPress Web Application Development Guide: Detailed explanation of available features (Part 7): Caching Technology. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.
