Home > Backend Development > PHP Tutorial > Example of using apc cache in php_PHP tutorial

Example of using apc cache in php_PHP tutorial

WBOY
Release: 2016-07-13 17:18:35
Original
806 people have browsed it

I remember that in the past, when uploading php files to the server, you needed to use zend guard to encrypt them. With apc, you don’t need to. From the news seen on Wikipedia, apc will be built into php6, so apc is still worth learning.

1. Install extensions

In ubuntu 12.04, you can install the apc extension directly through apt-get install php-apc.

Regarding the windows system, the author has installed apc under windows before, but it ran unstable. I don’t know if it has been solved now. In Windows, you can use wincache instead of apc. It is developed by Microsoft itself and is very stable.

Tips: After the installation is complete, remember to restart the web server.

2. Download apc.php

Download address: apc_php.zip

apc.php is a script that can check the usage of apc. The interface is as follows:

Example of using apc cache in php_PHP tutorial

There are two tabs that you can pay a little attention to:

1 System Cache Entries: This represents the system cache options, which are intermediate codes for caching some php files.

2 User Cache Entries: Represents the cache of user data. User data can be cached to apc during encoding. If you want to view the user data cache, you need to modify the access account and password first. Open the apc.php file, find the following two lines of code and modify them:

Copy code The code is as follows:

defaults('ADMIN_USERNAME','apc'); // Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

3. apc usage example

apc is very simple to use. See the following examples of adding, querying, modifying, and deleting.

Add a cache with a valid time of 3600 seconds

Copy code 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:

Example of using apc cache in php_PHP tutorial
 

It includes the number of hits, size, expiration time, etc.

Query Cache

Copy code The code is as follows:

apc_add('name', 'tom', 3600);
print apc_fetch ('name'); //Output tom

Modify cache

Copy code The code is as follows:

apc_store('name', 'anny', 3600);
print apc_fetch ('name'); //Output anny

Delete cache

Copy code The code is as follows:

apc_delete('name');
var_dump(apc_fetch('name') ); //Output bool(false)

Increasing and decreasing numbers

If the cached content is a number, you can use apc_ inc to increase by 1 and apc_dec to decrease by 1.

Copy code The code is as follows:

apc_add('num', 10);
apc_inc('num') ;
print apc_fetch('num');//Output 11
apc_dec('num');
print apc_fetch('num');//Output 10

Check whether the cache exists

Copy code The code is as follows:

apc_add('name', 'tom', 3600);
var_dump( apc_exists('name')); //Output bool(true)
var_dump(apc_exists('age')); //bool(false)


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621657.htmlTechArticleI remember that in the past, when php files were uploaded to the server, they needed to be encrypted with zend guard. With apc, this is no longer necessary. From the news seen on Wikipedia, apc will be built into php6, so apc is still valuable...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template