Want to use the APC cache in the end how the effect, helpless in Windows can not find the appropriate version of the Php-apc.dll extension file, had to be in Linux
installed, without the source mode installation, direct Yum on the line:
To install the APC's dependent package first:
The code is as follows |
Copy Code |
Yum install php-pear php-devel httpd-devel pcre-devel gcc make
|
Then use PECL to install the APC:
The code is as follows |
Copy Code |
PECL Install APC
|
To add an APC extension to a configuration file:
The code is as follows |
Copy Code |
echo "extension=apc.so" >/etc/php.d/apc.ini
|
Finally remember to reboot the server:
The code is as follows |
Copy Code |
Service httpd Restart |
, and then use the PHP phpinfo () function to detect:
APC Cache function Instance
In APC we can also enjoy the features of the cache large file upload progress brought by APC, and we need to set the apc.rfc1867 to 1 in php.ini.
and add a hidden field apc_upload_progress to the form, the value of this field can randomly generate a hash to ensure that it is unique.
APC has many settings, and you can set it in php.ini, such as:
The code is as follows |
Copy Code |
[APC] apc.enabled = 1 Apc.shm_segments = 1 Apc.shm_size = 64 Apc.max_file_size = 10M Apc.stat=1
|
I tried the common APC function in PHP:
The code is as follows |
Copy Code |
<?php /* Add variable to data store BOOL Apc_add (String $key, mixed $var [, int $ttl = 0]) If key exists, it will not overwrite, but return false */ Apc_add (' url ', ' http://www.111cn.Net ');
/* Remove stored variables from cache Mixed Apc_fetch (mixed $key [, BOOL & $success]) */ Var_dump (apc_fetch (' url '));
/* Use Apc_store () stored variables,. Key is unique, so two values using the same name, the original will be overwritten by the new value BOOL Apc_store (String $key, mixed $var [, int $ttl = 0]) */ Apc_store (' var ', ' new variable ');
/* Remove a variable from the user cache Mixed Apc_delete (String $key) */ Apc_delete (' url ');
/* Clear APC Cache bool Apc_clear_cache ([string $cache _type]) */ Apc_clear_cache (' user ');
/* Check for the presence of a key in the APC and return TRUE if key exists or FALSE Mixed apc_exists (mixed $keys) */ if (apc_exsists (' url ')) { echo "This key really exists"; }else{ echo "seems to be the key does not exist"; }
/* Increments a stored number, returns the current value of the key when it succeeds, or returns FALSE on failure int Apc_inc (string $key [, int $step = 1 [, BOOL & $success]]) */ Apc_store (' Anumber ', 42); $ret = Apc_inc (' Anumber ', 1, $fail); Var_dump ($ret); Var_dump ($fail);
/* decrements the number of a stored variable, returns the current value of the key when it succeeds, or returns FALSE on failure int Apc_dec (string $key [, int $step = 1 [, BOOL & $success]]) */ $ret = Apc_dec (' astring ', 1, $fail); Var_dump ($ret); Var_dump ($fail); |
Also provides a useful APC cache class:
The code is as follows |
Copy Code |
<?php
/********************************************************************************* * APC Cache class, Copyright (excerpt from initphp framework) ***********************************************************************************/ Class MYAPC {
/** * APC Cache-Set cache * Set cache Key,value and cache time * @param string $key key value * @param string $value value * @param string $time cache time */ Public Function Set_cache ($key, $value, $time = 0) { if ($time = = 0) $time = null; Permanent cache in case of NULL Return Apc_store ($key, $value, $time) }
/** * APC Cache-Get Cache * Get cached data through key * @param string $key key value */ Public Function Get_cache ($key) { Return Apc_fetch ($key); }
/** * APC Cache-Clears a cache * Remove a cache from the Memcache * @param string $key key value */ Public function Clear ($key) { Return Apc_delete ($key); }
/** * APC Cache-Clears all caches * It is not recommended to use this feature * @return */ Public Function Clear_all () { Apc_clear_cache (' user '); Clear User Cache return Apc_clear_cache (); Clear Cache }
/** * Check for APC cache presence * @param string $key key value */ Public function exists ($key) { Return apc_exists ($key); }
/** * Field self-used for counting * @param string $key key value * @param int $step The new step value */ Public Function Inc ($key, $step) { Return Apc_inc ($key, (int) $step); }
/** * Field self-minus-for counting * @param string $key key value * @param int $step The new step value */ Public function Dec ($key, $step) { Return Apc_dec ($key, (int) $step); }
/** * Return APC Cache information */ Public Function info () { return Apc_cache_info (); } }
Use the following methods: $APC = new MYAPC (); $APC->set_cache (' key ', ' Test by www.phpddt.com '); Print_r ($APC->get_cache (' key ')); |
Summary
1, the use of spinlocks lock mechanism, to achieve the best performance.
The 2,APC provides apc.php for monitoring and managing the APC cache. Don't forget to change the administrator name and password
3,APC By default creates shared memory by Mmap Anonymous mappings, which are stored in this "large" memory space. Self-control by APC
Deserve shared memory
4, we need to adjust the values of apc.shm_size, Apc.num_files_hints, apc.user_entries_hint by statistics. Until the most
Good
5, OK, I admit Apc.stat = 0 can achieve better performance. Anything I can do is acceptable.
6,php predefined constants, you can use the apc_define_constants () function. However, according to APC developers said PECL HiDef performance is more
Good, throw the define, it is inefficient.
7, Function Apc_store (), for system settings, such as PHP variables, lifecycle is the entire application (from httpd daemon to httpd daemon)
Process, using APC is better than memcached. Must not go through the Network Transport Protocol TCP.
8,APC is not suitable for user data that is frequently changed through function Apc_store () caching, there are some strange phenomena.