Memcached notes and summaries (1) Linux (CentOS 6.6) and Windows (7) Install and configure Memcached (1.4.24) with Memcached basic commands

Source: Internet
Author: User
Tags value store

Memcached Official website: http://memcached.org/

Currently the latest version (stable stable version) is 1.4.24, source package: http://www.memcached.org/files/memcached-1.4.24.tar.gz

Memcached relies on the libevent Library, which needs to be compiled first libevent,:http://jaist.dl.sourceforge.net/project/levent/libevent /libevent-2.0/libevent-2.0.22-stable.tar.gz

Installation process:

Libevent

① Decompression

[Email protected] src]# TAR-ZXVF libevent-2.0.  A

② Entering the catalogue

[Email protected] src]# CD libevent-2.0. -stable

③ Configuration

can be done by

[Email protected] libevent-2.0. -stable]#./configure-h

To view the compilation options.

Configuration:

[Email protected] libevent-2.0. -stable]#./configure--prefix=/usr/local/libevent

④ Compiling && installing

[Email protected] libevent-2.0. -stable]# make && make install

Memcached

① Decompression:

[Email protected] src]# TAR-ZXVF memcached-1.4.  -

② Entering the catalogue

[Email protected] src]# CD memcached-1.4.  -

③ Configuration

[Email protected] memcached-1.4. ] #./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/libevent

④ Compiling && installing

[Email protected] memcached-1.4. ] # Make && make install

⑤ running memcached

[Email protected] local]# CD memcached/[[email protected] memcached]# ll total dosage Adrwxr-xr-x2Root root4096August -  on: -BINDRWXR-xr-x3Root root4096August -  on: -INCLUDEDRWXR-xr-x3Root root4096August -  on: -Share[[email protected] memcached]# ll bin/Total Dosage292-rwxr-xr-x1Root root298213August -  on: -Memcached[[email protected] memcached]#./bin/memcached-p11211-M --Vvvcan't run as root without the-u switch

The reason why the can ' t run as root without the-u switch could not be run as root user memcached

View Current User:

[Email protected] memcached]# Whoamiroot

Solution, start memcached as nobody:

11211  -U-NOBODY-VVV

Memcached on a virtual machine can be connected locally using Telnet

192.168. 254.100 11211

If you need memcached to execute in the background, you can use

11211  -U-nobody-d

Attached: Common commands for installing memcached (1.4.24) and memcached under Windows (7 64-bit)

: http://wendal.net/2015/04/27.html

Download unzip, copy the home folder to any directory, and rename to "Memcached"

① open "cmd" and enter the directory:

② viewing the memcached version and Help

D:\memcached>memcached-h

③ Installing memcached:

d:\memcached>memcached-d Install

④ Open memcached:

11211 127.0. 0.1  -D start

Or

If you need to output the error message or warning message on the server side, you can:

 - 11211 -VVV

That is, at this point in the client operation, the server side will display information:

Memcached parameter Description

-P: Port number, default 11211

-L: Binding address (default: all allow, regardless of internal or external network or local replacement IP, there is a security risk, if set to 127.0.0.1 can only be native access)

-M: Use the size of the memory, in megabytes

-D Start: Start the memcached service-D Restart: Restart the memcached service-D Stop|shutdown: Close the running memcached service

-C: Maximum number of simultaneous connections, default is 1024

-V: Prompt message (print error/warning message in event loop)-VV: Details (also print client command/response)-VVV: Super Details (also prints changes in the internal state)

-F: Growth factor (ratio of size per two neighboring chunk), default 1.25

⑤ native use Telnet for client testing:

Also open a CMD window

Use CTRL +] to turn on the Echo feature.

Specific operation

Add a key-value store:

Add Syntax:

Add key flag expire length

Flag: You can include integer parameters for key-value pairs, which the client uses to store additional information about key-value pairs (such as distinguishing between saved values as strings, or serialized objects, etc., which can be handled appropriately when values are taken)

Expire: Length of time (in seconds, 0 for non-active cleanup) of key-value pairs in the cache

Length: in bytes

The add already exists key is not successful:

Get (query) the value of a key:get key

The expire can be a number of seconds (after n seconds), or it can be a timestamp (what time is invalid), for example:

Expires in 10 seconds

Again for example the current timestamp is: 1440774503

Set to expire after 100 seconds:

Delete Remove key value

Replace replaces the value with the same argument as add:

Replace cannot modify a nonexistent key:

The set has both add and replace functions, with the same parameters as add:

The set command can either modify the value of a key that already exists or increase the value of a key that does not exist.

incr key N: The value of keys key is increased by N

DECR Key N: The value of keys key is reduced by n

Note: The value of key is a 32-bit unsigned number

DECR application scenario: seconds Kill function

When placing an order, there are a number of database operations, such as writing orders, changing inventory, and transaction requirements, which are stressful for traditional relational databases.

And the operation of the second Kill, is the first second to grab an order number, the rest of the information such as personal receipt fill out the operation can be done in a hurry (can rely on the order number to another page slowly place orders, Zhi ' fu). The operation of the order number can take advantage of Memcached's DECR function:

Stores a key-value pair in memory that count is used to record inventory, seconds to kill N pieces, and the value of Count is N. The second kill, the inventory of the memcached DECR operation, and then to determine the return value, when the return value is greater than 0, the success of the grab orders. When the returned value equals 0, the activity ends.

You can also use the INCR command (seconds to kill n pieces, when the value returned by INCR equals N, the activity ends).

Since the main operation of the grab orders is done in memory, it is fast.

stats : Statistics

statsSTAT pid 5368 #进程号STAT uptime 3949 #持续运行时间STAT Time1440823276STAT version1.4. -STAT libevent2.0. A-Stablestat pointer_size +STAT Rusage_user0.202000STAT Rusage_system0.343000STAT curr_connectionsTenSTAT total_connections OneSTAT connection_structures OneSTAT Reserved_fds -STAT Cmd_get 4STAT Cmd_set3STAT Cmd_flush0STAT Cmd_touch0 stat get_hits 3stat get_misses 1 # get_hits and get_misses can calculate cache hit ratio STAT delete_misses0STAT delete_hits0STAT incr_misses0STAT incr_hits1STAT decr_misses0STAT decr_hits2STAT cas_misses0STAT cas_hits0STAT Cas_badval0STAT touch_hits0STAT touch_misses0STAT Auth_cmds0STAT auth_errors0STAT Bytes_read151STAT Bytes_written172STAT limit_maxbytes67108864STAT Accepting_conns1STAT Listen_disabled_num0STAT Threads4STAT Conn_yields0STAT Hash_power_level -STAT hash_bytes262144STAT hash_is_expanding0STAT malloc_fails0STAT bytes109STAT curr_items 2 #当前存储的键的个数STAT Total_items3STAT expired_unfetched0STAT evicted_unfetched0STAT Evictions0STAT Reclaimed0STAT crawler_reclaimed0STAT crawler_items_checked0STAT lrutail_reflocked0END

Flush_all: Clears all the key values. be used with caution on running systems.

At this point stats view statistics, Curr_items shows that there are currently stored key values, because the expiration of the use of an inert mechanism of failure.

memcached memory allocation mechanism

memcached Mitigation Memory fragmentation using slab allocator mechanism to manage memory: pre-divided memory into a number of slab warehouse, each warehouse cut into different sizes of small pieces, need to determine the content size, according to the size of the content selection of the appropriate warehouse.

Note: If there are more than a byte of data to store, but the 122 byte size of the warehouse chunk full, memcached will not look for a larger warehouse such as 144 byte of the warehouse to store the data, but the 122 Byte warehouse in the data kicked off.

LRU removal mechanism

memcached Expiration data removal mechanism:

When a value expires, it is not removed from memory, so stats statistics, Curr_items still have its information;

When the value is taken, determine whether it expires, if expired, return empty, and empty, at this time curr_items is reduced;

There have been no get before, will not automatically delete;

Use an empty chunk only when a new value occupies its place.

This expiration only lets the user not see the data, and does not delete it from memory in the expiration moment, called the Lazy expriation (lazy invalidation mechanism), the benefit is saves the CPU resources and the detection cost.

Lru:least Recently used is least recently used. Regardless of whether the least recently used value is permanently valid, it will be kicked off when a new value takes its place.

Reference:

Http://www.2cto.com/os/201209/153833.html

http://blog.csdn.net/zzulp/article/details/7823511

Memcached notes and summaries (1) Linux (CentOS 6.6) and Windows (7) Install and configure Memcached (1.4.24) with Memcached basic commands

Related Article

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.