The installation and basic use of Memcached and php5-memcache under Ubuntu11.04 at present, many large load sites use Memcache as the cache service to share the pressure on the database server. Here we will only introduce how to configure Memcache in Ubuntu and how to use Memecache in PHP, and install and use Memcached and php5-memcache in Ubuntu11.04.
At present, many large load sites use Memcache as the cache service to share the pressure on database servers.
Here we will only introduce how to configure Memcache in Ubuntu and how to use Memecache in PHP. For more information, such as the operating mechanism of Memcache or more advanced applications, Google... Pai_^
About Memcache and memcached
Memcache is the project name and memcached is the service name. It makes a lot of new contacts feel inexplicable. I personally think it is more correct to use the former.
Install Memcache
Memcache is divided into two parts: Memcache server and client. The Memcache server runs as a service. The creation, storage, and deletion of all data caches are actually completed here.
The client here refers to the extensions that PHP can call.
1) install the Memcache server
?
sudo apt-get install memcached
?
After installing the Memcache server, we need to start the service:
?
memcached -d -m 128 -p 11211 -u root
?
Here we need to describe the memcached service startup parameters:
-P listening port
-L connected IP address. the default value is local
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached service
-D install the memcached service
-D uninstall memcached service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C: maximum number of simultaneous connections. the default value is 1024.
-F block size growth factor. the default value is 1.25-n. the minimum allocation space is 48 for key + value + flags.
-H Show Help
?
2) install the Memcache Client
sudo apt-get install php5-memcache
?
After installation, we need to perform simple configuration in php. ini.,
Open the/etc/php5/apache2/php. ini file and add the following content at the end:
?
[Memcache]; a high-performance distributed memory object cache system maintains a unified and huge hash table in the memory. it can be used to store data in various formats, including image, video, file, and database retrieval results .; Whether to transparently transfer data to other servers when an error occurs. Memcache. allow_failover = On; maximum number of servers allowed to receive and send data, valid only when memcache. allow_failover is enabled. Memcache. max_failover_attempts = 20; data will be transferred according to the block size set by this value. The smaller the value, the more extra network transmission is required .; If you find that the speed cannot be explained is reduced, you can try to increase the value to 32768. Memcache. chunk_size = 8192; default TCP port used to connect to the memcached server. Memcache. default_port = 11211; controls the policy for ing keys to the server. The default value "standard" indicates that the old hash policy of the previous version is used .; Set as "consistent" to allow you to add or delete servers in the connection pool without re-computing the key ing between Keys and servers .; Memcache. hash_strategy = "standard"; controls the hash function that maps keys to the server. The default value "crc32" uses the CRC32 algorithm, while "fnv" indicates that the FNV-1a algorithm is used .; FNV-1a than CRC32 speed is slightly lower, but the hash effect is better .; Memcache. hash_function = "crc32"
?
Save php. ini and run sudo/etc/init. d/apache2 restart to restart Apache.
?
Use Memcache in PHP
Connect ('2017. 0.0.1 ', 11211); // connect to the Memcache server $ val = "this is a Memcache test. "; $ key = md5 ($ val); $ mem-> set ($ key, $ val, 0,120); // Insert a cache, the cache time is 120sif ($ k = $ mem-> get ('key') {// determines whether the specified keyecho 'from cache: 'is obtained :'. $ k;} else {echo 'normal'; // Here we need to replace it with query database and create cache in actual use.}?>
?
For Keys, the md5 query statement is usually used to obtain them. it is determined according to the specific needs in actual use...
Through the above steps, we have completed the configuration and basic usage of Memcache...
Method provided by php5-memcache extension
Memcache: add-add a value. if it already exists, false is returned.
Memcache: addServer-add an available server address
Memcache: close-close a Memcache object
Memcache: connect-create a Memcache object
Memcache_debug-control debugging function
Memcache: decrement-deletes the value of a saved key.
Memcache: delete-delete a key value
Memcache: flush-clear all cached data
Memcache: get-get a key value
Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.
Memcache: getServerStatus-get the parameters of the running server
Memcache: getStats-return some running statistics of the server
Memcache: getVersion-returns the version information of the running Memcache.
Memcache: increment-adds the value of a saved key.
Memcache: pconnect-creates a persistent connection object for Memcache.
Memcache: replace-R: overwrites an existing key.
Memcache: set-add a value. if it already exists, overwrite it.
Memcache: setCompressThreshold-compresses data larger than a certain size
Memcache: setServerParams-modify server parameters at runtime