to facilitate testing, a C client is given libmemcached Links : https://launchpad.net/libmemcached/
and memcacheclient-2.0 : http://code.jellycan.com/files/memcacheclient-2.0.zip( SLN generated, opened under Windows directly with VS, Compiled successfully )
In memcached startup, there are many configuration parameters can be selected, the following parameters corresponding to memcached1.4.15, now give the specific meaning of these parameters:
"A:" The//unix socket's permission bit information, the UNIX socket's permission bit information and the normal file's permission bit information "P:"//memcached listening TCP port value, the default is 11211 "s:"//unix Socket listening Socket file path "U:"//memcached listening UDP port value, default is 11211 "M:"//memcached use the maximum memory value, default is 64M "M"//When the memcached memory is used, Do not carry out LRU data, directly return error, this option is to close the LRU "C:"//memcached maximum number of connections, if not specified, according to the maximum value of the system "K"//whether to lock the memory held by memcached, if the memory is locked, Other business-held memory will reduce the size of "HI"//help Information "R"//core file, if not specified, "V"//debug Information "D"//set to the maximum value of the system (daemon) Run "L:"//Bind IP information If the server has multiple IPs , you can start multiple memcached instances on multiple IPs, note: This is not the acceptable IP address "u:"//memcached running user, if you start with root, you need to specify the user, otherwise the program error, exit. "P:" When running in daemon mode, the//memcached file path information "F:"//Memory expansion factor, which relates to a change in memcached internal initialization space, is explained in detail "N:" The minimum size of the//chunk (byte) , subsequent growth is the value of the *factor to increase the number of "T:"//internal worker threads, the default is 4, the maximum recommended not more than 64 "D:"//Internal data storage when the separator "L"//Specify the size of the memory page, the default memory page size is 4K, the page maximum not more than 2M , the size of large pages, can effectively reduce the size of the page table, improve the efficiency of memory Access "R:"//The maximum number of requests for a single worker "C"//Disable the business of CAs, that is compare and set "B:"//listen operation cache Connection number "B:"//memc ached internal use of protocols, support for binary protocols and text protocols, early only text protocol, binary protocol is followed by the "I:"///single item maximum value, default is 1M, can be modified, the minimum value modified is 1k, the maximum value can not exceed 128M"S"//Open SASL security Protocol "O:"//There are four parameter entries that can be set: Maxconns_fast (if the number of connections exceeds the maximum number of connections, immediately closes the new connection) Hashpower (the exponential value of the size of the hash table, is pressed 1<
(Note: The following sections of this article are mostly from http://blog.csdn.net/caihaijiang/article/details/7017329)
memcached protocol
Old version: Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
NEW: Https://github.com/memcached/memcached/blob/master/doc/protocol.txt
First, the agreement
The memcached client interacts with the memcached using a TCP connection, and the memcached server listens on the specified port (the default port is 11211). The client connects to the memcached server, sends instructions, gets the data, and then closes the connection.
It is usually not necessary to send any commands to close a session. Clients can turn off unwanted connections at any time. However, clients are often encouraged to cache these connections because the memcached server itself is designed to be a server that can support hundreds or thousands of connections, and after the client caches the connection, it avoids the overhead of duplicate connections.
The memcached protocol contains two parts of data, text lines and unstructured data. The former is a command from the client or a response from the server side, which represents the data accessed by the client. The command ends with \ r \ n, the data can be \r,\n or \ r \ n to identify the end of the respective section.
Second, the key
Memcached is usually stored by key, and the length of a key cannot exceed 250 characters. Key cannot contain control characters or white space characters
Third, the order
There are three types of commands:
1. Storage command: Set, add, replace, append, prepend, CAs
2, read the command: GET, gets
3, the Third kind of command, does not involve unstructured data. The client sends such a command, and the server returns the result of the response
1. Expiry time
There are two types of expiration time: One is Unix time (the number of seconds from 1970.1.1 to now) and the other is the number of seconds relative to the current time. If the number of seconds for the expiration time is greater than 60*60*24*30 (that is, 30 days), the server is considered to be Unix time.
2. Error settings
The server may return an error prompt character for each command issued by the client. There are three types of error prompt characters:
1) error\r\n: Indicates that the command sent by the client does not exist
2) Client_error <error>\r\n: Indicates an error in the client's input
3) Server_error <error>\r\n: server-side error
3. Store command:
Command format: <command name> <key><flags> <exptime> <bytes> [noreply]\r\n
Command name: such as set, add, replace, append, prepend
Command name |
Role |
Set |
Store this data |
Add |
Store this data when and only if the key does not exist |
Replace |
Store this data, when and only if the key exists |
Append |
Store the data behind the contents of the existing key, ignoring <flags> and <exptime> |
Prepend |
Store the data in front of the content that already exists for the key, ignoring <flags> and <exptime> |
Cas |
This data is stored when and only if the data has not been updated since it was last acquired |
Flags: An arbitrary 32-bit (old version is a 16-bit) unsigned integer
Exptime: Expiration time, if 0, indicates never expires. Units per second
Bytes: Represents the number of bytes of data that will be stored, which can be 0. The number of bytes of content to be stored must be equal to the value and cannot be greater than or less than.
Example: An integer 12 is stored in the memcache, the key is Var,flags 1, the expiration time is 1000, and the number of bytes is 2.
To connect the memcached server side with Telnet:
Enter the command as follows: Set VAR 1 1000 2, then press ENTER, then enter to store is data: 12, press ENTER, the server returns stored, as follows:
See what you just saved with the GET command:
4. Get command
Format:
Get <key>*\r\n
Gets <key>*\r\n
<key>*, which indicates that there can be more than one key, separated by a space between each key.
Executing the command, the server returns 0 or more item, with each item formatted as follows:
VALUE <key> <flags><bytes> [<cas unique>]\r\n
<data block>\r\n
Bytes is the length of the data content, data block is the content of the key corresponding
Example:
5. Delete command
Format: Delete <key> [noreply]\r\n
Noreply parameter to tell the server not to send a response
The result of the command's return may be:
delete\r\n indicates successful deletion
Not_found\r\n does not have a corresponding key
Example:
6. Increase/decrease command
Command format:
INCR <key> <value> [noreply]\r\n or DECR <key> <value> [noreply]\r\n
Value is the number to increase or decrease.
The operation succeeds and the server returns the value after the operation.
For DECR operations, set to 0 if the value after the operation is less than 0
INCR and DECR cannot be used directly, they must be set or add before they are used, and the value is a numeric type, and when added, the stored area expands.
Example:
7. Touch
This command is used to update the expiration time of an existing item, in the following format: (This command is not supported for low versions)
Touch <key> <exptime> [noreply]\r\n
If you return "touched\r\n" after executing the command, the execution succeeds
8. Statistical commands
Command format:
1) stats\r\n View general statistics
2) Stats <args>\r\n
General statistical Information , examples:
Specific meaning:
Name |
Type |
Meaning |
Pid |
32U (32-bit unsigned integer) |
memcached server's PID |
Uptime |
523 |
memcached server self-booting to present time (seconds) |
Time |
523 |
Current Unix Time |
Version |
String |
Version number of the memcached server |
Pointer_size |
32 |
Default pointer size for operating system |
Curr_items |
32u |
The number of item currently stored |
Total_items |
523 |
Total cumulative stored item count from server startup to now |
bytes |
64u |
The current number of bytes spent storing the item |
Curr_connections |
523 |
Current number of client connections |
Total_connections |
523 |
Server cumulative number of client connections from startup to now |
Connection_structures |
523 |
Number of connection structures allocated by the server |
Cmd_get |
64u |
Number of Get |
Cmd_set |
64u |
Set Number of times |
Get_hits |
64u |
Number of Get Hits |
Get_misses |
64u |
Get no hit, number of Miss |
Evictions |
64u |
The number of valid item that was removed for the new item to free memory space. If the cache size is small, the elimination policy often occurs |
Bytes_read |
64u |
The total number of bytes read from the cache |
Bytes_written |
64u |
The total number of bytes written to the cache |
Limit_maxbytes |
523 |
The maximum amount of memory allocated by the memcached server |
9. Item statistic Information
The stats command is followed by the parameters: items. Returns the item information stored in each slab, in the following format:
STAT items:<slabclass>:<stat><value>\r\n
Example:
10. ItemSize Statistical information
The stats command is followed by the parameter sizes, which returns the overall size stored in the cache and the number of item.
Note: This command locks the cache, and it iterates through each item and calculates the size, where we cannot access the server, so use this command with caution.
The format of the returned results is as follows:
<size> <count>\r\n
which
' Size ' is an approximate size of the Item,within-bytes.
' Count ' is the amount of items, Existwithin that 32-byte range.
Example:
11. Slab Statistical information
The stats command takes the parameter slabs and returns information about each slab created during the memcached run.
Data format: STAT <slabclass>:<stat> <value>\r\n
Example:
Name |
Meaning |
Chunk_size |
The size of each block. An item uses a block of appropriate size. |
Chunks_per_page |
The number of blocks on a page, the default size of a page is less than or equal to 1m,,chunks_per_page * chunk_size = 1MB. Slab by page, each page divided into different blocks |
Total_pages |
Number of pages assigned to slab |
Total_chunks |
Number of blocks allocated to slab |
Used_chunks |
Number of blocks already assigned to item |
Free_chunks |
Number of blocks not assigned to item |
Free_chunks_end |
Number of blocks currently available |
mem_requested |
The number of bytes requested to be stored in the slab |
Active_slabs |
Number of slab already allocated |
total_malloced |
Number of bytes already allocated to slab |
Item is stored in slab, and the size of the slab is greater than or equal to the size of the item. Mem_requested represents the size of all item in a slab.
Total_chunks * chunk_size–mem_requested, which represents the amount of memory wasted in a slab. If there is a lot of waste, you need to consider adjusting the slab factor
12. Other Commands
1) Flush_all: Execution of this command will result in the invalidation of all the item that exists in Memcache. You can also specify that it expires after a certain period of time. Example:
2) Version: View the version number of the Memcache
3) Quit: Close connection
Distributed Cache System memcached (12)--Basic configuration and commands