Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A method of optimizing the Statistical efficiency of browsing WordPress articles by using memory Cache

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How to optimize the statistical efficiency of WordPress article browsing using in-memory caching? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

Use memory caching to optimize the statistical efficiency of WordPress article browsing.

WordPress default custom field caching method

General article browsing statistics plug-ins use custom fields to store data. If the server turns on Memcached memory cache, how can the data of custom fields be cached? WordPress caches $post_id as the cache_key,'post_meta' as the cache_group as a whole.

So updating a custom field is equivalent to updating the cache of all custom fields under the $post_id, and each time the article is viewed, the custom field data will be added 1, so that the cache of the custom field will be updated each time, and then there will be three more SQL queries on each page.

Using memory cache to optimize the statistical efficiency of article browsing

Is there any way to solve this problem? We can cache the browsing statistics of the article in memory and add 10 browses each time before writing to the database, which can greatly reduce the number of SQL queries caused by custom field changes in WordPress operations.

Copy the above code into the funtions.php file of the current theme:

/ / when updating the number of views of the article, update it to memory first, and then write to the database add_filter every 10 times ('update_post_metadata', function ($check, $post_id, $meta_key, $meta_value) {if ($meta_key = =' views') {if ($meta_value% 10! = 0) {$check= true;wp_cache_set ($post_id, $meta_value, 'views');} else {wp_cache_delete ($post_id,' views') }} return $check;}, 1,4); / / get the number of views of the article from memory first, and then get the add_filter from the database without ('get_post_metadata', function ($pre, $post_id, $meta_key) {if ($meta_key = =' views') {$views= wp_cache_get ($post_id, 'views'); if ($views! = = false) {return [$views];} return $pre;}, 1,3)

There may also be a small problem: because Memcached's cache is not persistent, if you accidentally empty the memory cache, the number of views of the article will be lost, but it will definitely be less than 10.

The WPJAM Basic plug-in has integrated Memcached. After downloading WPJAM Basic, copy the object-cache.php file in the wpjam-basic/template/ directory to the wp-content directory.

Thank you for reading! After reading the above, do you have a general idea of how to optimize the statistical efficiency of WordPress article browsing using in-memory caching? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report