Home >Backend Development >PHP Tutorial >Using data caching in discuz
I have been tinkering with discuz’s cache for a few days and have come up with some clues. The efficiency is really low, 2333333~~~~
discuz can use a variety of caches, here we only talk about data cache. discuz supports caching methods such as redis and memcache. You only need to set it in /config/config_global.php and the environment supports it.
In fact, reading and writing cache is relatively simple. The code is as follows:
require_once libfile('function/cache'); //加载缓存类 savecache($cachename, $data); //写缓存 loadcache('plugin_fahao_kflist'); //读缓存,将缓存写到$_G变量中,通过读取$_G['cache']来获取缓存数据。
where savecache() writes the data to the set cache (such as memcacheredisxpc, etc.), and records the cache into the common_syscache table.
When using loadcache(), the fetch_all method in the table_common_syscache class will be called to obtain the cache. 1. First, determine whether the memory cache is set. If so, read the data in the memory cache. If the data exists, it will be returned directly. If it does not exist, it will be returned directly. To set up the memory cache, go to step 2. 2. Then determine whether the file cache is set. If so, read the file cache data and return it directly if the data exists. If it does not exist or the file cache is not set, go to step 3. 3. Read the data from the common_syscache table and return it. (So these aspects should be taken into consideration when clearing the cache)
The key is to clear the cache. In Global->Performance Optimization->Memory Optimization, you can do some setting optimization. In Memory Cache Management, you can also directly clear the specified cache. . But for customized cache, you need to develop your own function to clear it. In my environment, the memcache cache is turned on, and then the system's built-in memory() function is used to clear the cache. However, when I use loadcache() to read data, I find that the cache still exists. The reason is that memory() can clear the memcache data, but it does not clear the commom_syscache table, so it still exists when loadcache() is used. Later, I checked and found the deletegroupcache() function. In order to adapt to the overall situation, I simply wrote the deletecache() function in /source/function/function_core.php. The code is as follows:
function deletecache($cachenames) { if(!empty($cachenames)) { C::t('common_syscache')->delete($cachenames); } }
The above has introduced the use of data caching in discuz, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.