Distributed Cache system Memcached basic configuration and commands

Source: Internet
Author: User
Tags cas control characters memcached

For the convenience of testing, a C client libmemcached link is given: https://launchpad.net/libmemcached/

and Memcacheclient-2.0:http://code.jellycan.com/files/memcacheclient-2.0.zip (generated sln, opened directly with VS in Windows, 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 permission bit information for the//unix socket, the UNIX socket has the same permission bit information as the normal file.
"P:"//memcached listening TCP port value, default is 11211
"s:"//unix socket-Listening socket file path
"U:"//memcached listening UDP port value, default is 11211
"M:" The maximum memory value used by the//memcached, default is 64M
"M"//When the memory of memcached is exhausted, the data is returned without LRU, and the option is to close the LRU
"C:" The maximum number of connections for the//memcached, if not specified, by the maximum value of the system
"K"//whether the memory held by the memcached is locked, if memory is locked, the memory held by other business will be reduced
"HI"//help information
The size of the "R"//core file, if not specified, by the maximum value of the system
"V"//debug information
"D"//set to run in daemon mode
"L:"//bound IP information, if the server has more than one IP, you can start multiple memcached instances on multiple IP, note: This is not the IP address to receive
"U:"//memcached run user, if you start with root, you need to specify the user, otherwise the program error, exit.
"P:"//memcached file path information for PID when running in daemon mode
The "F:"//Memory expansion factor, which relates to a change in the memcached internal initialization space, is explained later in detail
"N:" The Minimum Size (byte) of the//chunk, and subsequent growth is the value *factor to grow
"T:"//number of internal worker threads, default is 4, maximum recommended no more than 64
"D:"//delimiter for internal data storage
"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 memory access efficiency
"R:"//maximum number of requests for a single worker
"C"//Business-disabled CAs, i.e. compare and set
"B:" Number of//listen operation Cache Connections
"B:"//memcached protocol used internally, support for binary protocols and text protocols, early only text protocol, binary protocol is followed by
"I:"//maximum value of single item, default is 1M, can be modified, modified minimum value is 1k, maximum value cannot exceed 128M
"S"//Open SASL Security protocol
"O:"//There are four parameter entries that can be set:
Maxconns_fast (closes the new connection immediately if the number of connections exceeds the maximum number of connections)
Hashpower (the index value of the size of the hash table, is the 1<Slab_reassign (whether to adjust/balance the memory occupied by each slab)
Slab_automove (whether to automatically move individual slab, if this option is turned on, there will be a dedicated thread for slab adjustment)

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

32u

Total cumulative stored item count from server startup to now

bytes

447

The current number of bytes spent storing the item

Curr_connections

32u

Current number of client connections

Total_connections

32u

Server cumulative number of client connections from startup to now

Connection_structures

32u

Number of connection structures allocated by the server

Cmd_get

447

Number of Get

Cmd_set

447

Set Number of times

Get_hits

447

Number of Get Hits

Get_misses

447

Get no hit, number of Miss

Evictions

447

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

447

The total number of bytes read from the cache

Bytes_written

447

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

This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-01/112507.htm

Distributed Cache system Memcached basic configuration and commands

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.