Install and use Memcache in Ubuntu

Source: Internet
Author: User
Tags crc32
Many large load sites around the world use Memcache as a cache service to share the pressure on database servers. Here we will only describe 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 and hellip; many large load sites around the world use Memcache as a 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 11111-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 the 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 images, videos, files, and database retrieval results.



Whether to transparently transfer data to other servers in case of errors.
Memcache. allow_failover = On

The maximum number of servers allowed to receive and send data, which is 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 this value to 32768.
Memcache. chunk_size = 8192

The default TCP port used to connect to the memcached server.
Memcache. default_port = 11111

Control the policy to map 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/delete servers in the connection pool without re-computing the 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

$ Mem = new Memcache; // creates a Memcache object.
$ Mem-> connect ("127.0.0.1", 11111); // connect to the Memcache server

$ Val = "this is a Memcache test .";
$ Key = md5 ($ val );
$ Mem-> set ($ key, $ val, 0,120); // adds a cache to be inserted. the cache time is 120 s.

If ($ k = $ mem-> get ('key') {// determines whether the specified key is obtained.
Echo 'from cache: '. $ 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

 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.