Home > Article > Backend Development > Steps to use memcache caching in discuz
discuzA variety of caches can be used, 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 it can be used if 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($cachename); //读缓存,将缓存写到$_G变量中,通过读取$_G['cache']来获取缓存数据。
where savecache() writes the data to the set In the cache (such as memcache\redis\xpc, etc.), the cache will be recorded in 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 For memory cached data, if the data exists, it will be returned directly. If it does not exist and the memory cache is not set, step 2 will be entered.
2. Then determine whether the file cache is set. If so, Read the file cache data. If the data exists, it will be returned directly. 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 must be taken into consideration when clearing the cache)
The key is to clear the cache, which can be done in the global->Performance Optimization->Memory optimization Some settings are optimized, and the specified cache can also be cleared directly in the memory cache management. 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 is the detailed content of Steps to use memcache caching in discuz. For more information, please follow other related articles on the PHP Chinese website!