Memcached is a high-performance distributed memory cache server. The purpose of the general use is to reduce the number of database visits by caching database query results to improve the speed and scalability of dynamic Web applications.
The specific installation is as follows:
1. Installation Libevent
Because the memcached event handling mechanism uses the Libevent library, it must be installed.
#tar ZXVF libevent-2.0.18-stable.tar.gz
#cd libevent-2.0.18-stable
#./configure--The default installation path
#make
#make Intsall
Second, install memcached server
#tar ZXVF memcached-1.4.13.tar.gz
#cd memcached-1.4.13
#./configure
#make
#make Intsall
Or: sudo apt-get install memcached
Third, install libmemcached client (c + +)
#tar ZXVF libmemcached-1.0.4.tar.gz
#cd libmemcached-1.0.4
#./configure--The default installation path
#make
#make Intsall
Note: Because the libmemcached default installation path is/usr/local/lib, you need to connect
sudo ln-s/usr/local/lib/libmemcached.so.11/usr/lib/libmemcached.so.11 (refer to your own installation specific version)
Iv. installation of memcache Client (PHP)
sudo apt-get install Php5-memcache
After installation we need to do a simple configuration in the php.ini, open the/etc/php5/apache2/php.ini file at the end to add the following:
[Memcache]
; A high-performance distributed Memory object caching system that maintains a unified, huge hash table in memory
; It can be used to store data in a variety of formats, including images, videos, files, and the results of database retrieval.
; Whether to transparently failover to other servers when an error is encountered.
Memcache.allow_failover = On
; The maximum number of servers to try when accepting and sending data, only when opening memcache.allow_failover. Memcache.max_failover_attempts = 20
; The data is transferred according to the block size set by this value. The smaller the value, the more network traffic is required.
; If you find an unexplained rate decrease, you can try to increase this value to 32768.
Memcache.chunk_size = 8192
; The default TCP port to use when connecting to the memcached server.
Memcache.default_port = 11211
; Controls the policy that maps the key to the server. The default value "Standard" represents the old hash policy using the previous version.
; Set to "consistent" allows you to add/Remove servers in a connection pool without having to recalculate the mapping relationship between key and server.
; memcache.hash_strategy = "standard"; Controls the hash function that maps the key to the server. The default value, Crc32″, uses the CRC32 algorithm, whereas "FNV" means the FNV-1A algorithm is used.
; The fnv-1a is slightly lower than the CRC32 speed, but the hash effect is better.
; memcache.hash_function = "Crc32″
Save PHP.ini, perform sudo/etc/init.d/apache2 restart restart Apache.
Five, start memcached server
Memcached-d-M 10-u root-l 127.0.0.1-p 12000-c 256-p/tmp/memcached.pid
Command Options Description:
The-D option launches a daemon,
The-m option is allocated to the amount of memory used by the memcache, in megabytes, 10MB in the command,
The-u option runs the memcache user, and the command is root,
The-l option listens to the server IP address, and if there are multiple addresses, the server's IP address local host 127.0.0.1 is specified in the command.
-p option to set memcache listening port, I set up here 12000, preferably more than 1024 of the port,
-C option The maximum number of concurrent connections to run, the default is 1024, the command set to 256, generally according to the server's load to set,
-p option to save the memcache pid file, which is saved in/tmp/memcached.pid.
If you want to boot up automatically, just add a line above the/etc/rc.d/rc.local command.
Also someone uses the command: #/usr/local/memcached/bin/memcached-d-m 10-u deamon-p 12000
The above command some options to refer to: That is, the IP is not specified, the default is the local host; the user's best choice is: Deamon or Apache, which is the user's service, which user to start.
Simple test:
1. C + +
#cd/
# mkdir Memcache
# chmod 777 memcache/
# CD memcache/
#echo "Test memcached" > Data
#memcached-D-u root-m 64-l 127.0.0.1-p 11211
#ps-aux | grep memcache
# MEMCP Data--servers=127.0.0.1:11211
# Memcat Data--servers=127.0.0.1:11211
Print out: Test memcached
or:
root@ubuntu:/memcache# telnet localhost 11211
Trying 127.0.0.1 ...
Connected to localhost.
Escape character is ' ^] '.
Get Data
VALUE Data 0 15
Test memcached
End
2, PHP
<?php
$memcache = new Memcache;
$memcache->connect (' localhost ', 11211) or die ("could not Connect");
$version = $memcache->getversion ();
echo "Server" version: ". $version." <br/>\n ";
$tmp _object = new StdClass;
$tmp _object->str_attr = ' Test ';
$tmp _object->int_attr = 123;
$memcache->set (' key ', $tmp _object, False, All) or Die ("Failed to save data at the server");
echo "Store data in the cache" (data would expire in seconds) <br/>\n ";
$get _result = $memcache->get (' key ');
echo "Data from the cache:<br/>\n";
Var_dump ($get _result);
?>
memcached Related commands: 1.3 Common parameter combinations
# memcached-d-m-p 11212-u nobody-l 127.0.0.1-x 127.0.0.1-x 11222-p/tmp/localhost_slave.pid-vv
2. Basic commands and Operations
2.1 Stored Commands
Mainly include: Set,add,replace,append,prepend,cas; format is:
Command key flag expiration_time bytes
value
Key, flag represents the Key/value additional information, expiration_time indicates the expiration time, in seconds, 0 means never expire, bytes the number of bytes that represent the value, must match exactly, value represents the key, and always appears in the second line.
The set command indicates that a key/value pair is stored, and if the key already exists, the corresponding value value is updated; If successful, return stored.
Set File_path 0 0 5
/opt/
<29 rep file_path 0 0 5
rep>29
STORED STORED
The Add command also indicates an increase of key/value, if the key/value already exists, the add operation fails, the save succeeds back to STORED, and the failure returns not_stored.
Add File_path 0 5
/opt/
not_stored
add file_suffix 0 0 2
JS
<29 rep file_suffix 0 0 2 16
rep>29 STORED
STORED
The Replace command indicates the value of the update key, and if Key/value does not exist, the replace operation fails, the STORED returns successfully, and the failure returns to not_stored;
Replace 0 0 7
Tianjin
STORED
Replace second 0 0 8
Shanghai
not_stored
Append indicates that the key must already exist or the operation will fail if the value is appended to the value of the key and the STORED is returned successfully, and the failure returns to not_stored;
Append Second 0 0 8
Shanghai
not_stored
Append-0 0 3
go
STORED
Prepend the value of the key corresponding to append the data, the key must already exist, otherwise the operation failed, successful return STORED, failure to return to not_stored;
Prepend Second 0 0 2
hi
not_stored
prepend a 0 0 2
hi
STORED
Get-i-0 Hitianjin Go Go
End
CAS (check and set): First compare after storage, that is, atomic updates, the principle is similar to optimism. Each time a request stores a data with a CAS value, the memcached ratio is equal to the CAS value of the currently stored data, and if it is equal, the data is updated, otherwise the operation fails; the currently stored CAs value is obtained by the gets command. Successfully returned stored and failed to return exists.
Gets
the 0 7 //12 indicates the CAS ID, which can be understood as version number
Chengdu end
CAs a
0 0 8 //10!= 12,cas failed
Shanghai
EXISTS
CAs 0 0 8 //12 The value of the key is not modified after gets, so you can set
Shanghai
STORED
2.2 commands to read
Get value value based on key, get multiple key values; |
Get Key1 Key2 |
Get-I-value-0 8 Shanghai End get-i-
fine
value-0 8
Shanghai
value fine 0 5< c7/>yes!!
End
Gets is a command that is used with CAs, gets returns an additional CAS value that can be understood as a version; If the CAS value changes after the last gets, the value of the CAS setting is not stored; Gets key |
Gets Key1 Key2 |
Gets
0 8
Shanghai end
set 0 0 7
Chengdu
STORED
CAs a 0 0 7 //Since the gets was set, the CAS ID changed and CAS failed
Chengdu
EXISTS
The delete command deletes a key/value pair, only one key/value can be deleted at a time; The operation fails if the key you want to delete does not exist: Delete key
Delete the second
client_error bad command line format. Usage:delete <key> [noreply] Delete-A-
DELETED
INCR/DECR: If the value of a key represents a 64-bit integer, the value can be added or subtracted by the incr and DECR commands: INCR/DECR key num
Set ID 0 2
STORED
incr ID
DECR ID 5
15
2.3 Order of the statistics
Stats displays information such as processes and current status.
Stats STAT PID 1224//Process ID STAT uptime 30385//System run events, units: seconds STAT time 1392199633//system current events, Unix time stamp represented by: 2/12/2014 6: 25:40 PM STAT version 1.4.13//memcached edition STAT libevent 2.0.21-stable//libevent version STAT pointer_size 64/Os Word size (6 4-bit) STAT Rusage_user 1.892712//Process Cumulative User Time STAT Rusage_system 0.996848//Process cumulative system time STAT curr_connections 8//Currently open connection number STA T total_connections 9//Total number of connections that have been opened STAT connection_structures 9/Server allocated number of connections STAT Reserved_fds STAT 29//
Total number of line get commands STAT cmd_set 29////Total number of Execute SET commands STAT Cmd_flush 2//execute Flush_all command total STAT cmd_touch 0 STAT get_hits 14 STAT get_misses STAT delete_misses 3 STAT delete_hits 3 STAT incr_misses 0 STAT incr_hits 4 STAT decr_misses 0 ST
At Decr_hits 2 STAT cas_misses 1 STAT cas_hits 2 STAT cas_badval 2 STAT touch_hits 0 STAT touch_misses 0 STAT Auth_cmds 0 STAT auth_errors 0 STAT bytes_read 1503 STAT bytes_written 4125 STAT limit_maxbytes 134217728 STAT Accepting_conns 1 STAT Listen_disabled_num 0 STAt threads 4//Threads STAT conn_yields 0 STAT hash_power_level STAT hash_bytes 524288 STAT 0 hash_is_expanding ex pired_unfetched 0 STAT evicted_unfetched 0 STAT replication MASTER STAT repcached_version 2.3.1 STAT repcached_qi_free 819 1 STAT Bytes 217//stored Item bytes STAT curr_items 3//Current Item quantity STAT Total_items//Item Total STAT Evictions 0//
Get space to delete item quantity STAT reclaimed 2 end
Stats items display information about items
Stats Items
STAT items:1:number 3
STAT items:1:age 1552 STAT
items:1:evicted 0
STAT items:1:evicted_ Nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT Items : 1:reclaimed 2
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
End
Stats Slabs Display Slab information
Stats slabs
STAT 1:chunk_size STAT 1:chunks_per_page 10922 STAT 1:total_pages
1
STAT 1:total_ Chunks 10922
STAT 1:used_chunks 3
STAT 1:free_chunks 0
STAT 1:free_chunks_end 10919
STAT 1:mem_ Requested 217
STAT 1:get_hits
STAT 1:cmd_set-
STAT 1:delete_hits 3
STAT 1:incr_hits 4
STAT 1:decr_hits 2
STAT 1:cas_hits 2
STAT 1:cas_badval 2
STAT 1:touch_hits 0
STAT active_slabs 1
stat total_malloced 1048512 End
Stats sizes
Stats sizes
STAT 3 End
Stats Cachedump Displays information about items in slab (the command may no longer be supported in the future): Stats cachedump [slab ID] [number of items, 0 for all items]
Set City 0 0 7
Tianjin
STORED
stats cachedump 1 0
ITEM City [7 B; 1392169248 S]
end Get City V Alue City 0 7
Tianjin
End
Flush_all causes all items in the cache to expire, the server does not stop, and the memory is not refreshed or freed.
Flush_all
OK Get the