Home > Article > Backend Development > Sample code for using apc cache in php
The role of APC
1. Cache period: APC's cache is divided into two parts: system cache and user data cache.
The system cache is automatically used, which refers to the compilation of the PHP file source code by APC The results are cached and then compared to the timestamp when called again.
User data cache is used by users to read and write using apc_store and apc_fetch functions when writing php code. If the amount is not large, I suggest you use it. If The amount is large, I suggest using memcache will be better.
2. Status control and analysis: The source code package of PHP APC comes with an apc.php;
You can upload this file to the web Access a certain directory on the server with a browser, which will display the current status. We can analyze the current cache status from the table here and make further optimizations. apc-info-clublocalhost2.png This is the status of a test site. You can analyze it slowly, this tool will provide many useful tools.
Add a cache, the effective time is 3600 seconds
The code is as follows:
apc_add('name', 'tom', 3600);
Execute the code, and then check the User Cache Entries. You can see that there is an additional cache data with the key value name:
in There are number of hits, size, expiration time, etc.
Query cache
The code is as follows:
apc_add('name', 'tom', 3600); print apc_fetch('name'); //输出tom
Modify cache
The code is as follows:
apc_store('name', 'anny', 3600); print apc_fetch('name'); //输出anny
Delete cache
The code is as follows:
apc_delete('name'); var_dump(apc_fetch('name')); //输出bool(false)
Increase and decrease the number
If the cached content It is a number. You can use apc_ inc to increase by 1 and apc_dec to decrease by 1.
The code is as follows:
apc_add('num', 10); apc_inc('num'); print apc_fetch('num');//输出11 apc_dec('num'); print apc_fetch('num');//输出10
Determine whether the cache exists
The code is as follows:
apc_add('name', 'tom', 3600); var_dump(apc_exists('name')); //输出bool(true) var_dump(apc_exists('age')); //bool(false)
The above is the detailed content of Sample code for using apc cache in php. For more information, please follow other related articles on the PHP Chinese website!