Memcached is a high-performance distributed memory object Cache System for dynamic web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.
For memcached Server Client Communication, simple text line-based protocols are used instead of complex XML and other formats.
Therefore, you can use Telnet to save and retrieve data on memcached. The following is an example.
$ Telnet local host 11211
Trying 127.0.0.1...
Connected to localhost. localdomain (127.0.0.1 ).
Escape Character is '^]'.
Set Foo 0 0 3 (save command)
Bar (data)
Stored (result)
Get Foo (GET command)
Value Foo 0 3 (data)
Bar (data)
Event Processing
Libevent is a library that encapsulates the epoll, kqueue, and other event processing functions of Linux operating systems into a unified interface. O (1) performance can be used even if the number of connections to the server increases. Memcached uses this libevent library to achieve high performance in Linux, BSD, Solaris, and other operating systems. For more information about event processing, see the c10k problem of Dan keel.
Storage Method
To improve performance, data stored in memcached is stored in the memory storage space built in memcached. Because the data only exists in the memory, restarting memcached and the operating system will cause all data to disappear. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (least recently used) algorithm. Memcached itself is a server designed for caching, so it does not take the permanent data into consideration.
Distributed Communication
Although memcached is a "distributed" cache server, the server does not have distributed functions. Memcached does not communicate with each other to share information. So, how to implement distributed? This depends entirely on the implementation of the client. This article also introduces the distributed memcached.
3
Editing
A visual memcached management system written in PHP
Memadmin is a visualized memcached management and monitoring tool. It is developed using PHP and is small in size and easy to operate.
Main functions:
Server parameter monitoring: Real-Time refresh of stats, settings, items, slabs, and sizes
Server performance monitoring: real-time monitoring of hit rates of common operations such as get, delete, incr, decr, and CAS
Supports data traversal to facilitate monitoring of stored content
Supports conditional queries to filter out keys or values that meet the conditions.
Array, JSON, and other Serialized character deserialization display
Other services compatible with the memcache protocol, such as Tokyo tyrant (excluding traversal)
Supports server connection pools, facilitating and simplifying multi-server management Switching
4. Edit tips
In many languages, clients connected to memcached are implemented, including Perl and PHP. List only on memcached website
The language
· Perl
· PHP
· Python
· Ruby
· C #
· C/C ++
· Lua
And so on.
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
Centos Installation
Install
Yum-y install memcached
Set to boot
Chkconfig -- level 2345 memcached on
Start and Stop
/Etc/init. d/memcached START | stop
Supplement: If the installation lacks other support, you can:
Yum groupinstall "development tools"
1. To install memcached, install libevent first.
Install libevent using Yum-y install.
Then install memcached in Yum-y install
View installation status
[[Email protected] ~] # Telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape Character is]
Stats
Stat PID 1422
Statuptime 635
Stat time 1406012189
Stat version 1.4.4
Stat pointer_size 64
Stat rusage_user 0.000000
Stat rusage_system 0.032994
Stat curr_connections 10
Stat total_connections 13
Stat connection_structures 11
Stat performance_get 0
Stat performance_set 0
Stat 1__flush 0
Stat get_hits 0
Stat get_misses 0
Stat delete_misses 0
Stat delete_hits 0
Stat incr_misses 0
Stat incr_hits 0
Stat decr_misses 0
Stat decr_hits 0
Stat cas_misses 0
Stat cas_hits 0
Stat cas_badval 0
Stat auth_cmds 0
Stat auth_errors 0
Stat bytes_read 21
Stat bytes_written 1537
Stat limit_maxbytes 67108864
Stat accepting_conns 1
Stat listen_disabled_num 0
Stat threads 4
Stat conn_yields 0
Stat bytes 0
Stat curr_items 0
Stat total_items 0
Stat evictions 0
End
This article is from the "Anna -- Linux house" blog and is not reposted!
Memcached High-Performance Distributed Object Cache System