Install and configure memcached Common commands and instructions for using memcached in Windows and Linux

Source: Internet
Author: User
Document directory
  • Windows Installation
  • Linux Installation
  • Install memcached
  • Start memcached
  • Stop memcached (locate the process and kill the process)
  • 1. Add
  •  
  • 2. Delete
  • 1. Get
  • 2. Gets
  • 3. CAS
  • 1. Stats
  • 2. Stats items
  • 3. Stats cachedump slab_id limit_num
  • 4. other stats commands
  • 1. append
  • 2. prepend
  • 3. flush_all
  • 4. Other commands
  • 1. Add
  •  
  • 2. Delete
  • 1. Get
  • 2. Gets
  • 3. CAS
  • 1. Stats
  • 2. Stats items
  • 3. Stats cachedump slab_id limit_num
  • 4. other stats commands
  • 1. append
  • 2. prepend
  • 3. flush_all
  • 4. Other commands
1. install and configure memcachedWindows Installation

1、memcached-1.2.6-win32-bin.zip: http://code.jellycan.com/memcached/, and install memcached.exe-D install.

2. Daemon startup: memcached.exe-M 512-d start
-D: Start the daemon. The default port 11211 cannot be specified.
-M indicates the specified memory size.

3. Specify the port to start memcached.exe-P 33000-M 512.
You can start multiple services. If the command line window is closed, the service is stopped.

Linux Installation

1. Prepare files

: Http://code.google.com/p/memcached/downloads/list

The latest version is memcached-1.4.15.tar.gz (you can wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz)

Here is an example of memcached-1.4.15.tar.gz.

Libevent-2.0.12-stable.tar.gz: http://libevent.org/(the latest version is libevent-2.0.20-stable.tar.gz can be

Wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz)

 

2. Install libevent. First, verify the LS/usr/lib | grep libevent to see if it is installed.

If the following information is displayed

Ls/usr/lib | grep libevent

Libevent-1.4.so.2
Libevent-1.4.so.2.1.3
Libevent-2.0.so.5
Libevent-2.0.so.5.1.8
Libevent.
Libevent_core-1.4.so.2
Libevent_core-1.4.so.2.1.3
Libevent_core-2.0.so.5
Libevent_core-2.0.so.5.1.8
Libevent_core.a
Libevent_core.la
Libevent_core.so
Libevent_extra-1.4.so.2
Libevent_extra-1.4.so.2.1.3
Libevent_extra-2.0.so.5
Libevent_extra-2.0.so.5.1.8
Libevent_extra.a
Libevent_extra.la
Libevent_extra.so
Libevent. La
Libevent_pthreads-2.0.so.5
Libevent_pthreads-2.0.so.5.1.8
Libevent_pthreads.a
Libevent_pthreads.la
Libevent_pthreads.so
Libevent. So

 

# Decompress the installation file using the tar command

[Root @ localhost local] # tar-zxvf libevent-2.0.20-stable.tar.gz

Use the./configure command to configure and check the current environment

[Root @ localhost libevent-2.0.20-stable]./configure-Prefix =/usr

[Root @ localhost libevent-2.0.20-stablemake
[Root @ localhost libevent-2.0.20-stablemake install

 

Install memcached

[Root @ localhost local] # tar-zxvf memcached-1.4.15.tar.gz

[Root @ localhost local] # cd memcached-1.4.15

[Root @ localhost memcached-1.4.15] #./configure

[Root @ localhost memcached-1.4.15] # Make
[Root @ localhost memcached-1.4.15make install

Verify Installation
# Ll/usr/local/bin | grep memcached

-Rwxr-XR-x 1 Root 245683 Sep 25 16:04 memcached

Start memcached

#/Usr/local/bin/memcached-D-M 512-C 512-P 11211-u root-T 10

 

The-D option is to start a daemon.

-M indicates the amount of memory allocated to memcache. The unit is MB.

-U is the user who runs memcache.

-L is the IP address of the listening server. If there are multiple IP addresses, specify the IP address of the listening server.

-P is the port for memcache listening. I have set port 11211 here, preferably over port 1024. Here we use the limit 11-c option to set the maximum number of concurrent connections. The default value is 1024.

-P is the PID file for saving memcache.

-VV is started in very vrebose mode, and debugging information and errors are output to the console.

For more information, see #/usr/local/bin/memcached-H.

Several processes can be started on a machine, such

[Root @ localhost local] # ps-Ef | grep memcach
Root 26928 1 0? 00:00:00/usr/local/bin/memcached-D-M 64-C 64-P 11211-u root-l 10.33.2.75-P/tmp/memcached. PID
Root 26968 1 0? 00:00:00/usr/local/bin/memcached-D-M 64-C 64-P 11212-u root-l 10.33.2.75-P/tmp/memcached2.pid
Root 26975 1 0? 00:00:00/usr/local/bin/memcached-D-M 64-C 64-P 11213-u root-l 10.33.2.75-P/tmp/memcached3.pid

 

 

Note: if everything is okay, congratulations, you can use it normally. Do not worry about the following errors:

Error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: no such file or directory

 

Solution:

1. First find/-name libevent-2.0.so.5 to find the missing link file.

2. ld_debug = libs/usr/local/bin/memcached-V

3. Check the debug information to find the link library for the program. My program here goes to trying file =/usr/lib/libevent-1.4.so.2 while the actual storage location of my Linked Library (according to the machine bit, my is 64-bit) yes/usr/local/lib64/libevent-1.4.so.2 (just select one at the bottom)

4. Make a soft connection ln-S/usr/local/lib64/libevent-2.0.so.5/usr/lib/libevent-2.0.so.5

17768: Trying file =/lib64/tls/x86_64/libevent-2.0.so.5
17768: Trying file =/lib64/tls/libevent-2.0.so.5
17768: Trying file =/lib64/x86_64/libevent-2.0.so.5
17768: Trying file =/lib64/libevent-2.0.so.5
17768: Trying file =/usr/lib64/tls/x86_64/libevent-2.0.so.5
17768: Trying file =/usr/lib64/tls/libevent-2.0.so.5
17768: Trying file =/usr/lib64/x86_64/libevent-2.0.so.5
17768: Trying file =/usr/lib64/libevent-2.0.so.5

Verify whether startup is successful

[Root @ bookedb ~] # Netstat-ntlp | grep memcached

 

Stop memcached (locate the process and kill the process)

[Root @ bookedb ~] # Pgrep-l memcached
16321 memcached

[Root @ bookedb ~] # Kill-9 16321

Or

# Kill '/tmp/memcached. PID' Ii. Test memcached

1. Enter Telnet 192.168.120.28 11211 (the IP address of the host you installed and the memcached startup port) in the command, and then tap the stats command directly.

Stats
Stat PID 16321
Statuptime 2831
Stat time 1322126000
Stat version 1.4.6
Stat libevent 2.0.12-stable
Stat pointer_size 64
Stat rusage_user 0.004999
Stat rusage_system 0.004999
Stat curr_connections 12
Stat total_connections 18
Stat connection_structures 13
Stat performance_get 6
Stat performance_set 7
Stat 1__flush 0
Stat get_hits 5
Stat get_misses 1
Stat delete_misses 0
Stat delete_hits 1
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 5023
Stat bytes_written 7246
Stat limit_maxbytes 536870912
Stat accepting_conns 1
Stat listen_disabled_num 0
Stat threads 4
Stat conn_yields 0
Stat bytes 2479
Stat curr_items 2
Stat total_items 7
Stat evictions 0
Stat reclaimed 0
End

 

Iii. Common memcache commands

Query STATUS Command: stats
Query version number
Exit command quit
Clear cache data (mark all cached data as expired): flush_all

Memcached protocol and data access

The so-called protocol can be understood as a syntax rule for its operations (Data Access). Common commands and parameters for data access are as follows:

Set: store a record
Key: key value of the record
Flags: A decimal int that identifies the client flag used to store records. It is returned when records are retrieved.
Exptim: The data expiration time. 0 indicates that the data is not expired, and other values indicate valid milliseconds. After the data expires, the client cannot obtain this record, expired records in memcached will be cleared or deleted.
Get: Get the value of the key from memcached. If no value exists, the end flag is returned.
Append: indicates that the value corresponding to the key is appended with the input content.
Delete: Delete the value corresponding to the key.

 

Reference from http://cnazure.com/2011/12/windows-linux-memcached-install-configuration/

 

Memcached Common commands and usage instructions 1. Storage commands

Storage command format:

?
12 <command name> <key> <flags> <exptime> <bytes> <data block>

Parameters are described as follows:

<Command name> Set/Add/replace
<Key> Search for keywords
<Flags> The client uses it to store additional information about key-value pairs.
<Exptime> Survival time of the Data. 0 indicates permanent
<Bytes> Storage bytes
<Data block> Stored data blocks (which can be directly understood as values in the key-value structure)
1. Add

(1) Set

The SET command is frequently used in memcached. The SET command can be simply added. If the set key already exists, the command can update the original data corresponding to the key, that is, to update the data.

You can use the "get key name" method to view the added records:

As you know, we can also delete it by using the DELETE command and then add it again.

(2) Only dataDoes not existAdd

(3) only dataExistReplace

2. Delete

As you can see, deleting an existing key value and a record that does not exist can return different results.

 

Ii. read command 1. Get

The key of the GET command can be one or more keys separated by spaces.

2. Gets

As you can see, the gets command returns a number (13) more than the common GET command ). This number can be used to check whether the data has changed. When the data corresponding to the key changes, the multiple returned numbers will also change.

3. CAS

CAS indicates checked and set. It can be stored only when the last parameter matches the parameter obtained by gets. Otherwise, "exists" is returned ".

 

Iii. STATUS Command 1. Stats

 

2. Stats items


Execute stats items to view the stat items line. If memcached stores a lot of content, Many stat items lines will be listed here.

 

3. Stats cachedump slab_id limit_num

Execute the stats cachedump 1 0 command. The effect is as follows:

Here, slab_id is 1, which is determined by the result returned by stats items in 2 (the number following stat items). limit_num seems to be the number of records returned, which is a good guess, however, 0 indicates that all records are displayed, while n (n> 0) indicates that N records are displayed. If N exceeds all records under the slab, the result is the same as that returned by 0.


You can use the stats items, stats cachedump slab_id limit_num, and get commands to traverse memcached records.

 

4. other stats commands

Such as stats slabs, stats sizes, and stats reset.

 

IV. Other Common commands 1. append

Add cached data after existing cached data. If the cached key does not exist, the server returns the not_stored.

 

2. prepend

It is very similar to append, but it is used to add cache data before existing cache data.

 

3. flush_all

This command has an optional numeric parameter. It always runs successfully, and the server sends an "OK \ r \ n" response. The effect is to immediately invalidate an existing project (default) or after the specified time. After the retrieval command is executed, NO content will be returned (unless the same key name is re-stored ). Flush_all does not immediately release the memory occupied by the project, but is executed when new projects are stored in succession (this is determined by the memcached's laziness detection and deletion mechanism ).

The result of flush_all is that all items whose Update Time is earlier than the time set by flush_all are ignored when the retrieval command is executed.

4. Other commands

Memcached also has many commands. For example, you can use the incr/decr command to increase or decrease the storage type. Here, only the frequently used commands in development and O & M are listed.

 

Supplement 1: several caches in. NET Framework

There is no doubt about the importance of caching in websites. I think the cache that many Asp.net developers prioritize when developing web application systems is not a third-party cache solution (such as distributed cache memcached and redis.. NET Framework provides multiple cache solutions. The following describes the knowledge of caching in. NET Framework Based on your development experience.

1. system. Web. caching. Cache

It is estimated that most people who have developed Asp.net have used the cache in this namespace. We can directly use the httpcontext. Current. cache instance without instantiating it. Of course, the cache class in this namespace allows you to instantiate it. If you need to customize your own cache system, you can fully control how to initialize this class. I have seen many articles in the garden about the crud auxiliary class library of cache. Most of them are for system. Web. caching. cache.

You can also use httpruntime. cache in the namespace to implement cache in different forms, such as web, console, and winform, without the need to instantiate the cache by yourself. Httpruntime. cache is a class that I used in my personal development. Now I prefer the enhanced cache class memorycache in. Net framework4.0.

 

2. Output Cache

As we all know, the output cache mainly refers to the paging output cache and partial page cache. To put it bluntly, it is to cache the HTML or part of the HTML of the entire page. There is nothing worth discussing. However, just recently I saw Dudu's blog, and I suddenly found that it was really exquisite to use it, how did I not find this problem before? It seems that the ability to discover and solve problems is equally important. Sometimes, the former is even more important.

 

3. system. runtime. caching

The most frequently used class memorycache in personal development comes from this namespace. before using it, you must reference using system. runtime. caching. Memorycache is inherited from objectcache, ienumerable, and idisposable. objectcache is an abstract class. Anyone who has used memorycache knows that this memorycache has a property called default, which can be used as follows:

private static ObjectCache memCache = MemoryCache.Default;

Of course, we can also use the public memorycache (string name, namevaluecollection Config = NULL) constructor to initialize the cache object.

Next, we can configure the memory usage quota scheme and quota check cycle for each memorycache instance in the web. config file. The following example is as follows:

  <system.runtime.caching>    <memoryCache>      <namedCaches>        <add name="Default" cacheMemoryLimitMegabytes="10"  pollingInterval="00:02:00"/>      </namedCaches>    </memoryCache>  </system.runtime.caching>

These configurations clearly specify the memory usage quota scheme and quota check cycle for each memorycache instance. For example, we can change the memory quota of the memorycache. Default instance on demand through configuration (I don't know the maximum memory available for the cache, maybe it's about MB ). The cache expiration Policy is similar to other cache frameworks. web. caching. the difference between cache is that the name is not cachedependency, but changemonitor, and a cache dependency policy based on files and directories is provided. It is also necessary to discuss cache expiration policies. However, personal development focuses more on data caching and replacement, and there is no perfect solution to cache expiration policies.

 

From http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html

I. Storage commands

Storage command format:

?
12 <command name> <key> <flags> <exptime> <bytes> <data block>

Parameters are described as follows:

<Command name> Set/Add/replace
<Key> Search for keywords
<Flags> The client uses it to store additional information about key-value pairs.
<Exptime> Survival time of the Data. 0 indicates permanent
<Bytes> Storage bytes
<Data block> Stored data blocks (which can be directly understood as values in the key-value structure)
1. Add

(1) Set

The SET command is frequently used in memcached. The SET command can be simply added. If the set key already exists, the command can update the original data corresponding to the key, that is, to update the data.

You can use the "get key name" method to view the added records:

As you know, we can also delete it by using the DELETE command and then add it again.

(2) Only dataDoes not existAdd

(3) only dataExistReplace

2. Delete

As you can see, deleting an existing key value and a record that does not exist can return different results.

 

Ii. read command 1. Get

The key of the GET command can be one or more keys separated by spaces.

2. Gets

As you can see, the gets command returns a number (13) more than the common GET command ). This number can be used to check whether the data has changed. When the data corresponding to the key changes, the multiple returned numbers will also change.

3. CAS

CAS indicates checked and set. It can be stored only when the last parameter matches the parameter obtained by gets. Otherwise, "exists" is returned ".

 

Iii. STATUS Command 1. Stats

 

2. Stats items


Execute stats items to view the stat items line. If memcached stores a lot of content, Many stat items lines will be listed here.

 

3. Stats cachedump slab_id limit_num

Execute the stats cachedump 1 0 command. The effect is as follows:

Here, slab_id is 1, which is determined by the result returned by stats items in 2 (the number following stat items). limit_num seems to be the number of records returned, which is a good guess, however, 0 indicates that all records are displayed, while n (n> 0) indicates that N records are displayed. If N exceeds all records under the slab, the result is the same as that returned by 0.


You can use the stats items, stats cachedump slab_id limit_num, and get commands to traverse memcached records.

 

4. other stats commands

Such as stats slabs, stats sizes, and stats reset.

 

IV. Other Common commands 1. append

Add cached data after existing cached data. If the cached key does not exist, the server returns the not_stored.

 

2. prepend

It is very similar to append, but it is used to add cache data before existing cache data.

 

3. flush_all

This command has an optional numeric parameter. It always runs successfully, and the server sends an "OK \ r \ n" response. The effect is to immediately invalidate an existing project (default) or after the specified time. After the retrieval command is executed, NO content will be returned (unless the same key name is re-stored ). Flush_all does not immediately release the memory occupied by the project, but is executed when new projects are stored in succession (this is determined by the memcached's laziness detection and deletion mechanism ).

The result of flush_all is that all items whose Update Time is earlier than the time set by flush_all are ignored when the retrieval command is executed.

4. Other commands

Memcached also has many commands. For example, you can use the incr/decr command to increase or decrease the storage type. Here, only the frequently used commands in development and O & M are listed.

 

Supplement 1: several caches in. NET Framework

There is no doubt about the importance of caching in websites. I think the cache that many Asp.net developers prioritize when developing web application systems is not a third-party cache solution (such as distributed cache memcached and redis.. NET Framework provides multiple cache solutions. The following describes the knowledge of caching in. NET Framework Based on your development experience.

1. system. Web. caching. Cache

It is estimated that most people who have developed Asp.net have used the cache in this namespace. We can directly use the httpcontext. Current. cache instance without instantiating it. Of course, the cache class in this namespace allows you to instantiate it. If you need to customize your own cache system, you can fully control how to initialize this class. I have seen many articles in the garden about the crud auxiliary class library of cache. Most of them are for system. Web. caching. cache.

You can also use httpruntime. cache in the namespace to implement cache in different forms, such as web, console, and winform, without the need to instantiate the cache by yourself. Httpruntime. cache is a class that I used in my personal development. Now I prefer the enhanced cache class memorycache in. Net framework4.0.

 

2. Output Cache

As we all know, the output cache mainly refers to the paging output cache and partial page cache. To put it bluntly, it is to cache the HTML or part of the HTML of the entire page. There is nothing worth discussing. However, just recently I saw Dudu's blog, and I suddenly found that it was really exquisite to use it, how did I not find this problem before? It seems that the ability to discover and solve problems is equally important. Sometimes, the former is even more important.

 

3. system. runtime. caching

The most frequently used class memorycache in personal development comes from this namespace. before using it, you must reference using system. runtime. caching. Memorycache is inherited from objectcache, ienumerable, and idisposable. objectcache is an abstract class. Anyone who has used memorycache knows that this memorycache has a property called default, which can be used as follows:

private static ObjectCache memCache = MemoryCache.Default;

Of course, we can also use the public memorycache (string name, namevaluecollection Config = NULL) constructor to initialize the cache object.

Next, we can configure the memory usage quota scheme and quota check cycle for each memorycache instance in the web. config file. The following example is as follows:

  <system.runtime.caching>    <memoryCache>      <namedCaches>        <add name="Default" cacheMemoryLimitMegabytes="10"  pollingInterval="00:02:00"/>      </namedCaches>    </memoryCache>  </system.runtime.caching>

These configurations clearly specify the memory usage quota scheme and quota check cycle for each memorycache instance. For example, we can change the memory quota of the memorycache. Default instance on demand through configuration (I don't know the maximum memory available for the cache, maybe it's about MB ). The cache expiration Policy is similar to other cache frameworks. web. caching. the difference between cache is that the name is not cachedependency, but changemonitor, and a cache dependency policy based on files and directories is provided. It is also necessary to discuss cache expiration policies. However, personal development focuses more on data caching and replacement, and there is no perfect solution to cache expiration policies.

 

From http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html

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.