Home  >  Article  >  Backend Development  >  Sample code for using apc cache in php

Sample code for using apc cache in php

怪我咯
怪我咯Original
2017-07-12 15:40:281973browse

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:


Sample code for using apc cache in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn