PHP distributed memory sharing (Memcache) distributed memory sharing. Currently, only Memcache can be used for distributed memory sharing in the PHP field. 1. memcached details. Common Web page cache methods include dynamic cache and static cache. PHP distributed memory sharing (Memcache) can be implemented in ASP. NET)
Distributed memory sharing. Currently, only Memcache can be used for distributed memory sharing in the PHP field.
1. memcached details.
Common Web page cache methods include dynamic cache and static cache. NET can be used to cache partial pages, while memcached cache is better than ASP. the local cache of NET is more flexible and can cache any object, regardless of whether it is output on the page or not. The biggest advantage of memcached is that it can be deployed in a distributed manner, which is also essential for large-scale applications.
LiveJournal.com uses memcached for caching on the front end and achieves good results. However, wikipedia and sourceforge also adopt or will soon use memcached as the cache tool. Memcached can play a huge role in large-scale website applications.
What is Memcached?
Memcached is a high-performance, distributed memory object cache system that reduces database loads in Dynamic Applications and improves access speeds.
Developed by Danga Interactive, Memcached improves the access speed of LiveJournal.com. The number of dynamic page views per second on the LJ is several thousand, and the number of users is 7 million. Memcached greatly reduces the database load and makes it easier to allocate resources for faster access.
How to use memcached-Server?
Run on the server:
#./Memcached-d-m 2048-l 10.0.0.40-p 11211
This starts a process that occupies 2 GB of memory and opens port 11211 to receive requests. Because the 32-bit system can only handle 4G memory addressing, 2-3 processes can be run on 32-bit servers with 4G memory and monitored on different ports.
How to use memcached-Client?
After the application end contains a Class used to describe the Client, it can be used directly, which is very simple.
PHP Example:
$ Options ["servers"] = array ("192.168.1.41: 11211", "192.168.1.42: 11212 ");
$ Options ["debug"] = false;
$ Memc = new MemCachedClient ($ options );
$ Myarr = array ("one", "two", 3 );
$ Memc-> set ("key_one", $ myarr );
$ Val = $ memc-> get ("key_one ");
Print $ val [0]. "\ n"; // prints 'one'
Print $ val [1]. "\ n"; // prints 'two'
Print $ val [2]. "\ n"; // prints 3
Why not use a database to do this?
What kind of database (MS-SQL, Oracle, S, MysQL-InnoDB, etc ..), implementing ACID, Atomicity, Consistency, Isolation, and Durability requires a lot of overhead. especially when using hard disks, this means that the query may be blocked. When a database that does not contain transactions (such as Mysql-MyISAM) is used, the preceding overhead does not exist, but the read thread may be blocked by the write thread.
Memcached never blocks and is very fast.
Why not use shared memory?
The original caching method was to cache objects in the thread, but the cache could not be shared between processes, and the hit rate was very low, resulting in extremely low cache efficiency. Later, there was a cache for shared memory. multiple processes or threads shared the same cache, but after all, it was limited to only one machine, the same cache on multiple machines is also a waste of resources, and the hit rate is relatively low.
Memcached Server and Clients work together to implement cross-Server distributed global cache. It can work with the Web Server. the Web Server has high CPU requirements and low memory requirements. the Memcached Server has low CPU requirements and high memory requirements, so it can be used together.
What about Mysql 4.x cache?
Mysql Query cache is not ideal because of the following:
When the specified table is updated, the query cache is cleared. In a system with a large load, such events occur frequently, resulting in low query cache efficiency. in some cases, the query cache may not be available, because it still has overhead for cache management.
On 32-bit machines, Mysql still limits memory operations to 4 GB, but memcached can be distributed out, so the memory size is theoretically unlimited.
Mysql queries the cache instead of the object cache. if a large number of other operations are required after the query, the query cache will not help.
If the data to be cached is small and not frequently queried, you can use Mysql to query the cache. otherwise, memcached is better.
How about database synchronization?
Here, database synchronization refers to the database synchronization mechanism similar to the Mysql Master-Slave mode through log synchronization.
You can distribute read operations but cannot distribute write operations. However, synchronization of write operations consumes a large amount of resources, and the overhead increases with the growth of the slave server.
The next step is to split the database horizontally so that different data is distributed to different database server groups to achieve distributed read/write, this requires connecting different databases based on different data in the application.
When this mode works (we also recommend this), more databases lead to more headaches and hardware errors.
Memcached can effectively reduce access to the database, so that the database uses the primary energy to do less frequent write operations, which is controlled by the database itself and rarely blocks itself.
Is Memcached fast?
It uses libevent to handle any number of open connections (using epoll instead of poll), and uses non-blocking network I/O, Distributed Hash objects to different servers, the query complexity is O (1 ). (Yudund)
References:
Distributed Caching with Memcached | Linux Journal
Http://www.danga.com/
Http://www.linuxjournal.com/article/7451
2. install memcached in freebsd:
Reference Step 1: install memcached
Code:
# Cd/usr/ports/databases/memcached/
# Make install
Step 2: Start memcached
Code:
#/Usr/bin/memcached-d-m 128-l 192.168.1.1-p 11211-u httpd
Parameter description:
-D run memcached in daemon mode;
-M: Set the memory size available for memcached, in MB;
-L set the IP address of the listener. if it is a local machine, this parameter is usually not set;
-P: Set the listening port. the default value is 11211. Therefore, you can leave this parameter unspecified;
-U specifies the user. if the current user is root, you must use this parameter to specify the user.
The above steps have completed the installation and startup of memcached!
Enter the IP address and port as required.
3. how memcached works
First, memcached runs on one or more servers as a daemon and accepts client connection operations at any time. the client can be written in various languages, currently, known client APIs include Perl, PHP, Python, Ruby, Java, C #, and C. After the PHP client establishes a connection with the memcached service, the next thing is to access the object. each accessed object has a unique identifier key, and the access operation is performed through this key, objects saved to memcached are actually stored in the memory, not in the cache file. This is why memcached is so efficient and fast. Note that these objects are not persistent. after the service is stopped, the data in the objects will be lost.
?
?
?
?
?
?
?
4. install the php support module for memcache
You can use PHP as the memcached client to call the memcached service for object access.
First, PHP has an extension called memcache. during compilation in Linux, The-enable-memcache [= DIR] option must be included, and the Window is in php. remove the annotator in front of php_memcache.dll in ini to make it available.
Http://pecl.php.net/package/memcache
In addition, there is also a way to avoid the trouble of extension and re-compilation, that is, directly using php-memcached-client.
Http://nio.infor96.com/wp-content/uploads/2006/04/memcached-client.zip
The second method is used in this article. although the efficiency is slightly lower than that of the extended library, it is not a problem.